Skip to content

Mobject.remove() repeatedly checks for list membership #306

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

Closed
leotrs opened this issue Aug 16, 2020 · 3 comments
Closed

Mobject.remove() repeatedly checks for list membership #306

leotrs opened this issue Aug 16, 2020 · 3 comments

Comments

@leotrs
Copy link
Contributor

leotrs commented Aug 16, 2020

This is a fairly edge-case optimization, so please feel free to close this if it's deemed to niche.

Currently, the definition of Mobject.remove does this:

    def remove(self, *mobjects):
        for mobject in mobjects:
            if mobject in self.submobjects:
                self.submobjects.remove(mobject)
        return self

At each removed mobject, the code mobject in self.submobjects is executed and performs linear search over a list, which is far from optimal if self.submobjects is large (even if mobjects is small). It would be more efficient to use an object that allows for O(1) membership checks, like a dict or a set (or an OrderedDict).

@huguesdevimeux
Copy link
Member

I guess that there is no case of purposeful duplicate within self.submobjects, as if we use set they will be deleted.

@leotrs
Copy link
Contributor Author

leotrs commented Aug 16, 2020

On the one hand, there are no repeated Mobjects in a scene, Scene.mobjects could be a set, not a lost. On the other hand, the code originally used the position of each Mobject in that list to determine the drawing order. But now that is done via Mobject.z_index.

So, in principle, I think we could change Scene.mobjects to be a set, and delete a while bunch of legacy code. Can anyone think of objections?

@leotrs
Copy link
Contributor Author

leotrs commented Nov 20, 2020

We are thinking of not using z_index anymore, so my last comment doesn't work anymore.

On the topic of this issue: I think there are deeper refactorings to be done with submobjects (#358), so whatever is done to fix the present issues would be temporary at best. Closing for now.

@leotrs leotrs closed this as completed Nov 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants