-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Improve Updater
signature checks for substantial speedups, add UpdaterWrappers
and more Updater
aliases
#3807
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Haven't looked through the whole thing, but just off a glance through, I left some comments
Hopefully, this makes the updater situation better in future versions of manim :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CodeQL found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.
Closes #321
General description
In the issue described, Leo mentions that calling
inspect.signature()
every time we need to compute whether anUpdater
is time-based or not, takes a considerable amount of time when there are a lot of updaters in a Scene.Leo proposed that every updater must always have two parameters and made a PR about it, but it was rejected.
Given an updater
func
, my first attempt was accessingfunc.__code__.co_varnames
(local variable names in the function code) and slice it from 0 tofunc.__code__.co_argcount
(number of positional parameters) to get the names of positional parameters.However, as JasonGrace commented his concerns about more advanced use cases, I needed something more.
Therefore, in this PR I introduce a
MobjectUpdaterWrapper
, which takes an updater function, saves it in its.updater
attribute, and usesinspect.signature()
on it to calculate whether it's time-based or not. It stores the result in its.is_time_based
, and this computation only happens once (when instantiatingMobjectUpdaterWrapper
), rather than every time the updater is called, leading to a substantial speedup when updating Mobjects.In
MobjectUpdaterWrapper
I also address the concerns raised by JasonGrace, Leo and myself:dt
in the signature?dt
value?self
?self
anyways?which is why I decided to make a separate class in the first place: the logic which attempts to address all these concerns is very long and shouldn't bloat the
Mobject.add_updater()
code. Plus, I wanted to replicate the same forOpenGLMobject
as well.Because I wanted to make the same changes in
OpenGLMobject
, the best course of action was taking all this code into a different module, specificallymanim.utils.updaters
.I also noticed that the relatively obscure classes
Object3D
and its subclassMesh
also have updaters which work in a very similar fashion, so I added theMeshUpdater
type alias and theMeshUpdaterWrapper
class as well.Finally,
Scene
also has updaters, but very different ones (they always accept adt: float
and returnNone
), so I also added aSceneUpdater
type alias.Profiling
An example scene with many updaters:
Notice how the
inspect.signature()
block completely disappears from theMobject.update()
time:Links to added or changed documentation pages
https://manimce--3807.org.readthedocs.build/en/3807/reference/manim.utils.updaters.html
Further Information and Comments
Reviewer Checklist