1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-01 17:38:41 +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

@ -36,19 +36,12 @@ ActiveObjectMgr::~ActiveObjectMgr()
void ActiveObjectMgr::clearIf(const std::function<bool(ServerActiveObject *, u16)> &cb)
{
// Make a defensive copy of the ids in case the passed callback changes the
// set of active objects.
// The callback is called for newly added objects iff they happen to reuse
// an old id.
std::vector<u16> ids = getAllIds();
for (u16 id : ids) {
auto it = m_active_objects.find(id);
if (it == m_active_objects.end())
continue; // obj was already removed
if (cb(it->second.get(), id)) {
// erase by id, `it` can be invalid now
removeObject(id);
for (auto &it : m_active_objects.iter()) {
if (!it.second)
continue;
if (cb(it.second.get(), it.first)) {
// Remove reference from m_active_objects
m_active_objects.remove(it.first);
}
}
}
@ -56,17 +49,16 @@ void ActiveObjectMgr::clearIf(const std::function<bool(ServerActiveObject *, u16
void ActiveObjectMgr::step(
float dtime, const std::function<void(ServerActiveObject *)> &f)
{
g_profiler->avg("ActiveObjectMgr: SAO count [#]", m_active_objects.size());
size_t count = 0;
// See above.
std::vector<u16> ids = getAllIds();
for (u16 id : ids) {
auto it = m_active_objects.find(id);
if (it == m_active_objects.end())
continue; // obj was removed
f(it->second.get());
for (auto &ao_it : m_active_objects.iter()) {
if (!ao_it.second)
continue;
count++;
f(ao_it.second.get());
}
g_profiler->avg("ActiveObjectMgr: SAO count [#]", count);
}
bool ActiveObjectMgr::registerObject(std::unique_ptr<ServerActiveObject> obj)
@ -99,12 +91,17 @@ bool ActiveObjectMgr::registerObject(std::unique_ptr<ServerActiveObject> obj)
return false;
}
auto obj_p = obj.get();
m_active_objects[obj->getId()] = std::move(obj);
auto obj_id = obj->getId();
m_active_objects.put(obj_id, std::move(obj));
auto new_size = m_active_objects.size();
verbosestream << "Server::ActiveObjectMgr::addActiveObjectRaw(): "
<< "Added id=" << obj_p->getId() << "; there are now "
<< m_active_objects.size() << " active objects." << std::endl;
<< "Added id=" << obj_id << "; there are now ";
if (new_size == decltype(m_active_objects)::unknown)
verbosestream << "???";
else
verbosestream << new_size;
verbosestream << " active objects." << std::endl;
return true;
}
@ -112,17 +109,13 @@ void ActiveObjectMgr::removeObject(u16 id)
{
verbosestream << "Server::ActiveObjectMgr::removeObject(): "
<< "id=" << id << std::endl;
auto it = m_active_objects.find(id);
if (it == m_active_objects.end()) {
// this will take the object out of the map and then destruct it
bool ok = m_active_objects.remove(id);
if (!ok) {
infostream << "Server::ActiveObjectMgr::removeObject(): "
<< "id=" << id << " not found" << std::endl;
return;
}
// Delete the obj before erasing, as the destructor may indirectly access
// m_active_objects.
it->second.reset();
m_active_objects.erase(id); // `it` can be invalid now
}
void ActiveObjectMgr::getObjectsInsideRadius(const v3f &pos, float radius,
@ -130,8 +123,10 @@ void ActiveObjectMgr::getObjectsInsideRadius(const v3f &pos, float radius,
std::function<bool(ServerActiveObject *obj)> include_obj_cb)
{
float r2 = radius * radius;
for (auto &activeObject : m_active_objects) {
for (auto &activeObject : m_active_objects.iter()) {
ServerActiveObject *obj = activeObject.second.get();
if (!obj)
continue;
const v3f &objectpos = obj->getBasePosition();
if (objectpos.getDistanceFromSQ(pos) > r2)
continue;
@ -145,8 +140,10 @@ void ActiveObjectMgr::getObjectsInArea(const aabb3f &box,
std::vector<ServerActiveObject *> &result,
std::function<bool(ServerActiveObject *obj)> include_obj_cb)
{
for (auto &activeObject : m_active_objects) {
for (auto &activeObject : m_active_objects.iter()) {
ServerActiveObject *obj = activeObject.second.get();
if (!obj)
continue;
const v3f &objectpos = obj->getBasePosition();
if (!box.isPointInside(objectpos))
continue;
@ -167,7 +164,7 @@ void ActiveObjectMgr::getAddedActiveObjectsAroundPos(const v3f &player_pos, f32
- discard objects that are found in current_objects.
- add remaining objects to added_objects
*/
for (auto &ao_it : m_active_objects) {
for (auto &ao_it : m_active_objects.iter()) {
u16 id = ao_it.first;
// Get object