Optimize always_redraw() by reducing Mobject copying in Mobject.become()
#4357
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview: What does this pull request change?
Inside the method
Mobject.become(self, mobject, ...)whereselfmorphs into a targetmobject, the latter was always copied before the morphing. Now, it's copied only when it's explicitly altered (whenstretchormatch_[height|width|depth|center]isTrue), which is rarely the case. This optimizesMobject.become()in most cases and, therefore, leads to a speedup when usingalways_redraw(), which under the hood creates aMobjectwith abecome()updater attached to it.Motivation and Explanation: Why and how do your changes improve the library?
Copying Mobjects is expensive, so it should be done only when necessary. In the case of
Mobject.become(), one usually calls it without specifyingstretchormatch_[height|width|depth|center]which are allFalseby default. In that case, the targetmobjectis not modified at all, so it's not necessary to copy it before makingselfmorph into it.By removing the copy in that case,
Mobject.become()gets a lot faster. By profiling the following code:where
SineCurveUnitCircleis a scene from the example gallery, I get the following results showing around 4x speedup inMobject.become():Links to added or changed documentation pages
Further Information and Comments
Reviewer Checklist