1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Custom data structure for active objects to get performance *and* safety (#13880)

This commit is contained in:
sfan5 2024-01-17 20:04:56 +01:00 committed by GitHub
parent 08ee6d8d4b
commit 0383c44f0d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 434 additions and 78 deletions

View file

@ -19,10 +19,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
#include <map>
#include <memory>
#include <vector>
#include "debug.h"
#include "util/container.h"
#include "irrlichttypes.h"
#include "util/basic_macros.h"
@ -52,24 +51,19 @@ public:
void clear()
{
while (!m_active_objects.empty())
removeObject(m_active_objects.begin()->first);
// on_destruct could add new objects so this has to be a loop
do {
for (auto &it : m_active_objects.iter()) {
if (!it.second)
continue;
m_active_objects.remove(it.first);
}
} while (!m_active_objects.empty());
}
T *getActiveObject(u16 id)
{
auto it = m_active_objects.find(id);
return it != m_active_objects.end() ? it->second.get() : nullptr;
}
std::vector<u16> getAllIds() const
{
std::vector<u16> ids;
ids.reserve(m_active_objects.size());
for (auto &it : m_active_objects) {
ids.push_back(it.first);
}
return ids;
return m_active_objects.get(id).get();
}
protected:
@ -88,11 +82,9 @@ protected:
bool isFreeId(u16 id) const
{
return id != 0 && m_active_objects.find(id) == m_active_objects.end();
return id != 0 && !m_active_objects.get(id);
}
// ordered to fix #10985
// Note: ActiveObjects can access the ActiveObjectMgr. Only erase objects using
// removeObject()!
std::map<u16, std::unique_ptr<T>> m_active_objects;
// Note that this is ordered to fix #10985
ModifySafeMap<u16, std::unique_ptr<T>> m_active_objects;
};