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

Optimized manim.utils.bezier.get_smooth_cubic_bezier_handle_points() #3767

Merged

Conversation

chopan050
Copy link
Contributor

@chopan050 chopan050 commented May 16, 2024

Overview: What does this pull request change?

Related PR: #3281

  • Optimized get_smooth_cubic_bezier_handle_points()
  • Added auxiliar functions get_smooth_cubic_bezier_handle_points_for_open_curve() and get_smooth_cubic_bezier_handle_points_for_closed_curve()
  • Deleted duplicated get_smooth_handle_points() function
  • Removed the auxiliar function diag_to_matrix() and the scipy import

Motivation and Explanation: Why and how do your changes improve the library?

I had a test scene with more than 100 ParametricFunctions in it, which were all being updated. More than half of the render time was spent on ParametricFunction.generate_points(), and one of the 3 main culprits was VMobject.make_smooth(), which made slow calls to get_smooth_handle_points().

The main bottleneck for this function is the call to scipy.linalg.solve_banded(). Apparently, it spends more time validating the arrays passed as a parameter, than actually solving the system of equations.

That's why I decided to manually implement the algorithm for solving these equations. But also, all the matrices follow the same pattern and their coefficients are pretty much always the same, varying only in size, so the diagonals can be hardcoded in the algorithm rather than stored explicitly. Some of them can even be memoized, which is what I did.

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

@chopan050 chopan050 changed the title Optimized manim.utils.get_smooth_cubic_bezier_handle_points() Optimized manim.utils.bezier.get_smooth_cubic_bezier_handle_points() May 16, 2024
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.

Most of the changes look good to me - it's nice to have doc strings for more Bézier stuff as well :)
I did notice we don't have any tests for get_smooth_cubic_bezier_handle_points, so it would be a good idea to add tests for the three functions you added :)

manim/utils/bezier.py Show resolved Hide resolved
@chopan050
Copy link
Contributor Author

I did notice we don't have any tests for get_smooth_cubic_bezier_handle_points, so it would be a good idea to add tests for the three functions you added :)

I just added some tests :)

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, thanks!

tests/module/utils/test_bezier.py Outdated Show resolved Hide resolved
@chopan050 chopan050 enabled auto-merge (squash) June 26, 2024 16:24
@chopan050 chopan050 merged commit 64ab9ad into ManimCommunity:main Jun 26, 2024
17 of 18 checks passed
@chopan050 chopan050 deleted the optimize-get-smooth-cubic-handles branch June 26, 2024 16:36
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