Skip to content

Conversation

@chopan050
Copy link
Contributor

Overview: What does this pull request change?

Inside the method Mobject.become(self, mobject, ...) where self morphs into a target mobject, the latter was always copied before the morphing. Now, it's copied only when it's explicitly altered (when stretch or match_[height|width|depth|center] is True), which is rarely the case. This optimizes Mobject.become() in most cases and, therefore, leads to a speedup when using always_redraw(), which under the hood creates a Mobject with a become() 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 specifying stretch or match_[height|width|depth|center] which are all False by default. In that case, the target mobject is not modified at all, so it's not necessary to copy it before making self morph into it.

By removing the copy in that case, Mobject.become() gets a lot faster. By profiling the following code:

if __name__ == "__main__":
    with tempconfig({"quality": "low_quality"}):
        SineCurveUnitCircle().render()

where SineCurveUnitCircle is a scene from the example gallery, I get the following results showing around 4x speedup in Mobject.become():

Before After
imagen imagen

Links to added or changed documentation pages

Further Information and Comments

Reviewer Checklist

  • The PR title is descriptive enough for the changelog, and the PR is labeled correctly
  • If applicable: newly added non-private functions and classes have a docstring including a short summary and a PARAMETERS section
  • If applicable: newly added functions and classes are tested

@github-project-automation github-project-automation bot moved this to 🆕 New in Dev Board Jul 29, 2025
@chopan050 chopan050 changed the title Optimize always_redraw() by removing unnecessary Mobject copy in Mobject.become() Optimize always_redraw() by reducing unnecessary Mobject copy in Mobject.become() Jul 29, 2025
@chopan050 chopan050 changed the title Optimize always_redraw() by reducing unnecessary Mobject copy in Mobject.become() Optimize always_redraw() by reducing Mobject copying in Mobject.become() Jul 29, 2025
Copy link
Member

@JasonGrace2282 JasonGrace2282 left a comment

Choose a reason for hiding this comment

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

LGTM

@github-project-automation github-project-automation bot moved this from 🆕 New to 👍 To be merged in Dev Board Jul 29, 2025
@chopan050 chopan050 merged commit 7ea765a into ManimCommunity:main Jul 29, 2025
23 checks passed
@github-project-automation github-project-automation bot moved this from 👍 To be merged to ✅ Done in Dev Board Jul 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants