Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveObjectMgr fixes #13560

Merged
merged 6 commits into from
Oct 9, 2023
Merged

ActiveObjectMgr fixes #13560

merged 6 commits into from
Oct 9, 2023

Conversation

Desour
Copy link
Member

@Desour Desour commented Jun 3, 2023

This is a bugfix PR.

These are the ActiveObjectMgr related changes from #13275.

Fixes some iterator invalidation issues, as well as a memleak that you can get by adding an object in on_destruct.

To do

This PR is a Ready for Review.

How to test

  • For testing the memleak, you can use this mod:
minetest.register_entity("test_ent_add_on_death:ent1", {
	collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
	visual = "cube",
	visual_size = {x = 1, y = 1},
	textures = {},
	static_save = true, -- looks like on_deactivate is not called if this is false

	on_activate = function(self, staticdata, dtime_s)
		minetest.log("[ent1] on_activate")
	end,
	on_deactivate = function(self, removal)
		minetest.log("[ent1] on_deactivate")
		minetest.add_entity(self.object:get_pos(), "test_ent_add_on_death:ent2")
		--~ error("myerror")
	end,
	on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
		minetest.log("[ent1] on_punch")
		self.object:remove()
	end,
})

minetest.register_entity("test_ent_add_on_death:ent2", {
	collisionbox = {-0.5, -0.5, -0.5, 0.5, 0.5, 0.5},
	visual = "cube",
	visual_size = {x = 2, y = 0.5},
	textures = {},
	static_save = false,

	on_activate = function(self, staticdata, dtime_s)
		minetest.log("[ent2] on_activate")
	end,
	on_deactivate = function(self, removal)
		minetest.log("[ent2] on_deactivate")
	end,
	on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir)
		minetest.log("[ent2] on_punch")
		self.object:remove()
	end,
})

And spawn an ent1.
Note that there's a warning at shutdown, and no abort. This is expected.

  • Otherwise just make sure objects are doing weird stuff.
  • You can also run with sanitizers, if you want. (Hasn't detected any issue with this PR for me.)

@Desour Desour added Maintenance Tasks to keep the codebase and related parts in order, including architectural improvements Bugfix 🐛 PRs that fix a bug labels Jun 3, 2023
@lhofhansl
Copy link
Contributor

I like using unique_ptr. Looks good after a quick glance. I also agree that this is a holy mess!

@JosiahWI
Copy link
Contributor

Should these all be unique_ptrs? It looks like the objects may be a shared resource and should use shared_ptr.

src/activeobjectmgr.h Show resolved Hide resolved
src/client/activeobjectmgr.h Outdated Show resolved Hide resolved
@JosiahWI
Copy link
Contributor

Agree that unique_ptr is good. Nothing to indicate that any of the pointers returned from the object manager are owning.

@Desour
Copy link
Member Author

Desour commented Jun 11, 2023

Should these all be unique_ptrs? It looks like the objects may be a shared resource and should use shared_ptr.

Using weak_ptrs could make sense I guess. But I don't want to do that change here.

Nothing to indicate that any of the pointers returned from the object manager are owning.

They aren't owning. They're owned by the aomgr.


Addresses comments and added test instructions. Please review.

@Desour Desour marked this pull request as ready for review June 11, 2023 13:39
@Desour
Copy link
Member Author

Desour commented Jul 15, 2023

Rebased.

auto it = m_active_objects.find(id);
if (it == m_active_objects.end())
continue; // obj was removed
f(it->second.get());
Copy link
Member

@sfan5 sfan5 Oct 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from an efficiency POV this is quite bad
the loop goes from O(n) to O(n * log n) just for lookup (and this runs every step!)

if we set entries to nullptr and postpone deletion wouldn't that solve the problem? IIRC it was just about iterator invalidation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The currently iterated obj isn't necessarily deleted here, and neither in clearIf.

if we set entries to nullptr and postpone deletion

Do you mean in removeObject? That's an interesting idea.
f can also insert new objects. Insertion doesn't invalidate iterators, but it means some of the new objects are immediately iterated through here and others aren't. So I'm not sure about the correctness.

IIRC, these functions are only called once per client or server step. So until this really turns out to be a performance bottleneck, I'd rather opt for the current more naive solution than to attempting premature optimization.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean in removeObject? That's an interesting idea.

Yes.

f can also insert new objects. Insertion doesn't invalidate iterators, but it means some of the new objects are immediately iterated through here and others aren't. So I'm not sure about the correctness.

hmmm

IIRC, these functions are only called once per client or server step. So until this really turns out to be a performance bottleneck, I'd rather opt for the current more naive solution than to attempting premature optimization.

IMO we already have enough issues with entity performance and shouldn't be looking to make it worse.

@Zughy Zughy added the Action / change needed Code still needs changes (PR) / more information requested (Issues) label Oct 2, 2023
@Desour Desour removed the Action / change needed Code still needs changes (PR) / more information requested (Issues) label Oct 2, 2023
@sfan5
Copy link
Member

sfan5 commented Oct 3, 2023

Idea that's hopefully not too complicated:
A second map is added, let's call it m_new.
If an object is deleted during iteration it is merely set to nullptr (occasional GC is done).
If an object is added during iteration it is added to m_new (getActiveObject will also check here).
After iteration ends m_new is merged into m_active_objects
advantages: No more copying to iterate safely, virtually no extra cost unless the edge cases actually happen.

Edit: We should of course merge these fixes first to get the safety.

Copy link
Member

@sfan5 sfan5 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

f(ao_it.second);

// Same as in server activeobjectmgr.
std::vector<u16> ids = getAllIds();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with many entites you will copy a huge memory block. Use ref as parameter to write result on

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean something like void getAllIds(std::vector<u16> &ret)? That's not more efficient. Function calls are prvalue expressions (see value categories), and there's copy elision. So, there should only be move constructor calls, no copying.

{
auto obj = object.get();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aren't we defeating the std::unique_ptr with this method, where we move the unique_ptr then we always have the pointer here and we are playing with it ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if I understand the question correctly.

obj is a non-owning ref/ptr. object owns. After the registerObject call, someone else owns the object, but references (i.e. obj) are still valid (at least if registerObject returns true).
(Before, there was just the raw ptr object. It was an owning raw ptr when we got it, but after the registerObject call it suddenly was no longer owning.)
The point of unique ptrs is not that you don't use other references on the object anymore.

@@ -404,12 +401,12 @@ void ClientEnvironment::addActiveObject(u16 id, u8 type,
<<std::endl;
}

u16 new_id = addActiveObject(obj);
u16 new_id = addActiveObject(std::move(obj));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i checked this function signature, i think we can diverge from ServerEnvironment, as there is no inheritance.
this will prevent the just next line getter. Can se return directly the CAO pointer instead (nullptr if register failed) ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by preventing that we will improve performance if there is huge amount of entities, removing one lookup

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that would be possible.
But I'd not like to stuff more changes into this PR.

@sfan5 sfan5 added this to the 5.8.0 milestone Oct 6, 2023
@sfan5 sfan5 merged commit 11ec75c into minetest:master Oct 9, 2023
13 checks passed
@Desour Desour deleted the activeobjmgr_ihateyou branch October 10, 2023 14:20
kawogi pushed a commit to kawogi/minetest that referenced this pull request Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bugfix 🐛 PRs that fix a bug Maintenance Tasks to keep the codebase and related parts in order, including architectural improvements One approval ✅ ◻️
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants