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

Performance improvement for most scenes #974

Merged
merged 1 commit into from
Apr 25, 2020
Merged

Conversation

mikemag
Copy link
Contributor

@mikemag mikemag commented Apr 12, 2020

tl;dr: this is a significant performance improvement for many scenes. 1.7x - 2.6x improvement in animation it/s.

This is a small change to some of the hotest paths in rendering objects. The biggest win comes from not using np.allclose() to check if two points are close enough. In general, NumPy is awesome at operating on large arrays, but overkill for very tiny questions like this. Created a small function to determine if two points are close using the same algorithm, and limited it to 2D points since that's all we need in set_cairo_context_path().

A couple of other minor tweaks to reduce or eliminate other uses of NumPy in this path.

In general, it is better to avoid wrapping lists in np.array when a real NumPy array isn't actually needed.

Added a new file for performance test scenes, with a single scene from the end of a video I've been working on.

Data:

MacBook Pro (16-inch, 2019)
macOS Catalina 10.15.4
2.4 GHz 8-Core Intel Core i9
64 GB 2667 MHz DDR4
Python 3.7.3 (default, Mar 6 2020, 22:34:30)

Profiler: yappi under Pycharm.

Using the scene Perf1 from the included perf_scenes.py, averaged over 5 runs and rendered with:
manim.py perf_scenes.py Perf1 -pl --leave_progress_bars

Before:
Animation 0: FadeInTextMobject, etc.: 8.93it/s
Animation 1: ShowCreationParametricFunction, etc.: 84.66it/s

Profiler shows 48.5% of the run spent under Camera.set_cairo_context_path()

After
Animation 0: FadeInTextMobject, etc.: 23.45it/s -- 2.63x improvement
Animation 1: ShowCreationParametricFunction, etc.: 149.62it/s -- 1.77x improvement

Profiler shows 19.9% of the run spent under Camera.set_cairo_context_path()

Less improvement with production-quality renders, and percent improvement varies with scene of course. This appears to be a good win for every scene I'm working on though. I hope it will be for others, too.

NB: there are more perf improvements to be had, of course, but this is the best one I currently have.

Thanks for contributing to manim!

Please ensure that your pull request works with the latest version of manim.
You should also include:

  1. The motivation for making this change (or link the relevant issues)
  2. How you tested the new behavior (e.g. a minimal working example, before/after
    screenshots, gifs, commands, etc.) This is rather informal at the moment, but
    the goal is to show us how you know the pull request works as intended.

tl;dr: this is a significant performance improvement for many scenes. 1.7x - 2.6x improvement in animation it/s.

This is a small change to some of the hotest paths in rendering objects. The biggest win comes from not using np.allclose() to check if two points are close enough. In general, NumPy is awesome at operating on large arrays, but overkill for very tiny questions like this. Created a small function to determine if two points are close using the same algorithm, and limited it to 2D points since that's all we need in set_cairo_context_path().

A couple of other minor tweaks to reduce or eliminate other uses of NumPy in this path.

In general, it is better to avoid wrapping lists in np.array when a real NumPy array isn't actually needed.

Added a new file for performance test scenes, with a single scene from the end of a video I've been working on.

Data:

MacBook Pro (16-inch, 2019)
macOS Catalina 10.15.4
2.4 GHz 8-Core Intel Core i9
64 GB 2667 MHz DDR4
Python 3.7.3 (default, Mar  6 2020, 22:34:30)

Profiler: yappi under Pycharm.

Using the scene Perf1 from the included perf_scenes.py, averaged over 5 runs and rendered with:
manim.py perf_scenes.py Perf1 -pl --leave_progress_bars

Before:
Animation 0: FadeInTextMobject, etc.:               8.93it/s
Animation 1: ShowCreationParametricFunction, etc.: 84.66it/s

Profiler shows 48.5% of the run spent under Camera.set_cairo_context_path()

After
Animation 0: FadeInTextMobject, etc.:               23.45it/s  -- 2.63x improvement
Animation 1: ShowCreationParametricFunction, etc.: 149.62it/s  -- 1.77x improvement

Profiler shows 19.9% of the run spent under Camera.set_cairo_context_path()

Less improvement with production-quality renders, and percent improvement varies with scene of course. This appears to be a good win for every scene I'm working on though. I hope it will be for others, too.

NB: there are more perf improvements to be had, of course, but this is the best one I currently have.
@mikemag mikemag marked this pull request as ready for review April 12, 2020 08:28
@eulertour eulertour merged commit af65c9d into 3b1b:master Apr 25, 2020
@eulertour
Copy link
Collaborator

Alright, looks good!

TonyCrane added a commit to manim-kindergarten/manim that referenced this pull request May 21, 2020
* latex can't recognize path seperator \\ in windows

Modify path before transport it to system command

* Re-add documentation link

* Update README.md

* Remove livestreaming option

* Display help if no input file is specified

* Revise config again (3b1b#987)

* Fix Text wrong display (3b1b#889)

* Performance improvement for most scenes (3b1b#974)

* Fix three bugs(shaders/stroke/size) of Text Class (text_mobject.py)  (3b1b#1030)

* fix: remove the last M tag in svg files to make Text run in shaders branch

* fix: close the svg path manually to fix the bug of wrong stroke

* fix: remove Space mobjects in Text to fix the bug of TransformText

* feat: make Text and TextMobject equal in size

* this will lead to more bugs of setting color, so I delete it

* Fix space characters problem of Text (3b1b#1035)

* Fix Text wrong display

* Fix space characters problem of Text

* Fix a potential bug using height or width in Text

* Update Text

* Fix typo in text_mobject.py

* Remove versions from requirements.txt

* Fixed ffmpeg 'Impossible to Open' and 'Protocol not found'  (3b1b#1057)

* Code() in file Code_mobject.py to display code with color highlighted added Paragraph() and "exact_spaces" parameter to Text()  (3b1b#1036)

* Added DocStrings for all methods in Container, Scene, GraphScene, MovingCameraScene, SceneFileWriter, ThreeDScene, SpecialThreeDScene, ZoomedScene, VectorScene, and LinearTransformationScene. (3b1b#1040)

* Added DocStrings for methods in Container and Scene.
Removed 2 unused imports from scene.py.

* Added DocStrings for all methods in GraphScene, MovingCameraScene, SceneFileWriter, ThreeDScene, SpecialThreeDScene and ZoomedScene.

* Added DocStrings for all methods in `VectorScene` and LinearTransformationScene...
...except `position_x_coordinate` and `position_y_coordinate`

Co-authored-by: Aathish Sivasubrahmanian <aathishs@Aathishs-MacBook-Air.local>

* Remove sanim directory

* Fix "itslef" to "itself"

I know this is trivial, but I had to fix it.

* Add missing import to brace.py

Without `import copy` lines 128 to 131 will throw an error. I've tested this edit in my local version of the library and it fixes the bug.

* Delete perf_scenes.py

Co-authored-by: zombie110year <zombie110year@outlook.com>
Co-authored-by: Devin Neal <devin@eulertour.com>
Co-authored-by: XiaoYoung <47266984+xy-23@users.noreply.github.com>
Co-authored-by: Mike Magruder <mikemag@users.noreply.github.com>
Co-authored-by: NavpreetDevpuri <30471072+NavpreetDevpuri@users.noreply.github.com>
Co-authored-by: Aathish <aathish04@gmail.com>
Co-authored-by: Aathish Sivasubrahmanian <aathishs@Aathishs-MacBook-Air.local>
Co-authored-by: applemonkey496 <55333787+applemonkey496@users.noreply.github.com>
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

Successfully merging this pull request may close these issues.

2 participants