Skip to content

Durview: Reduce (#1) frame redraws - #81

Merged
cmang merged 7 commits into
durdraw:devfrom
tmck-code:durview-reduce-frame-redraws
Dec 1, 2024
Merged

cmang merged 7 commits into
durdraw:devfrom
tmck-code:durview-reduce-frame-redraws

Conversation

@tmck-code

@tmck-code tmck-code commented Dec 1, 2024 •

Copy link
Copy Markdown
Contributor

Context

TL;DR this lowers durview CPU usage (for me)
from 29% to 4%

I love the doge blink animation, and while running it the other day I noticed that durview was using ~30% CPU to render it.

I did some debugging using the line_profiler project and found that most of this CPU time was spent in the refresh (and subsequently, addstr) methods in durdraw/durdraw_ui_curses

In addition to the line_profiler, I then implemented a hacky log that wrote a line to a log file every time that the main while loop in startPlaying iterated

You can see the profiling setup here if you're curious https://github.com/cmang/durdraw/compare/dev...tmck-code:durdraw:profile-durview

(run the profiling with LINE_PROFILE=1 /usr/bin/time -v -o time.txt python3 durview examples/dogeblink.dur, and you can observe the log in another session with tail -F log.txt)

The logs contain a timestamp, the currentFrameNumber that is being drawn, and a counter to indicate the while loop iteration.

refresh logs
2024-12-01 00:50:48.901470 drawing frame 1, loop 0/1000
2024-12-01 00:50:48.921323 drawing frame 1, loop 1/1000
2024-12-01 00:50:48.940473 drawing frame 1, loop 2/1000
2024-12-01 00:50:48.959458 drawing frame 1, loop 3/1000
2024-12-01 00:50:48.978553 drawing frame 1, loop 4/1000
2024-12-01 00:50:48.997347 drawing frame 1, loop 5/1000
2024-12-01 00:50:49.016764 drawing frame 1, loop 6/1000
2024-12-01 00:50:49.035933 drawing frame 1, loop 7/1000
2024-12-01 00:50:49.054773 drawing frame 1, loop 8/1000
2024-12-01 00:50:49.073795 drawing frame 1, loop 9/1000
2024-12-01 00:50:49.092853 drawing frame 1, loop 10/1000
2024-12-01 00:50:49.113493 drawing frame 1, loop 11/1000
2024-12-01 00:50:49.132217 drawing frame 1, loop 12/1000
2024-12-01 00:50:49.150698 drawing frame 1, loop 13/1000
2024-12-01 00:50:49.169333 drawing frame 1, loop 14/1000
2024-12-01 00:50:49.187802 drawing frame 1, loop 15/1000
2024-12-01 00:50:49.206489 drawing frame 1, loop 16/1000
2024-12-01 00:50:49.224954 drawing frame 1, loop 17/1000
2024-12-01 00:50:49.243562 drawing frame 1, loop 18/1000
2024-12-01 00:50:49.262059 drawing frame 1, loop 19/1000
2024-12-01 00:50:49.280669 drawing frame 1, loop 20/1000
2024-12-01 00:50:49.299114 drawing frame 1, loop 21/1000
2024-12-01 00:50:49.317735 drawing frame 1, loop 22/1000
2024-12-01 00:50:49.336204 drawing frame 1, loop 23/1000
2024-12-01 00:50:49.354888 drawing frame 1, loop 24/1000
2024-12-01 00:50:49.373709 drawing frame 1, loop 25/1000
2024-12-01 00:50:49.392539 drawing frame 1, loop 26/1000
2024-12-01 00:50:49.411143 drawing frame 1, loop 27/1000
2024-12-01 00:50:49.429932 drawing frame 1, loop 28/1000
2024-12-01 00:50:49.448452 drawing frame 1, loop 29/1000
2024-12-01 00:50:49.467156 drawing frame 1, loop 30/1000
2024-12-01 00:50:49.485896 drawing frame 1, loop 31/1000
2024-12-01 00:50:49.504534 drawing frame 1, loop 32/1000
2024-12-01 00:50:49.523184 drawing frame 1, loop 33/1000
2024-12-01 00:50:49.541746 drawing frame 1, loop 34/1000
2024-12-01 00:50:49.560402 drawing frame 1, loop 35/1000
2024-12-01 00:50:49.579166 drawing frame 1, loop 36/1000
2024-12-01 00:50:49.597708 drawing frame 1, loop 37/1000
2024-12-01 00:50:49.616428 drawing frame 1, loop 38/1000
2024-12-01 00:50:49.634927 drawing frame 1, loop 39/1000
2024-12-01 00:50:49.653592 drawing frame 1, loop 40/1000
2024-12-01 00:50:49.672306 drawing frame 1, loop 41/1000
2024-12-01 00:50:49.690900 drawing frame 1, loop 42/1000
2024-12-01 00:50:49.709548 drawing frame 1, loop 43/1000
2024-12-01 00:50:49.728100 drawing frame 1, loop 44/1000
2024-12-01 00:50:49.746713 drawing frame 1, loop 45/1000
2024-12-01 00:50:49.765479 drawing frame 1, loop 46/1000
2024-12-01 00:50:49.784038 drawing frame 1, loop 47/1000
2024-12-01 00:50:49.802755 drawing frame 1, loop 48/1000
2024-12-01 00:50:49.821283 drawing frame 1, loop 49/1000
2024-12-01 00:50:49.839960 drawing frame 1, loop 50/1000
2024-12-01 00:50:49.858677 drawing frame 1, loop 51/1000
2024-12-01 00:50:49.877273 drawing frame 1, loop 52/1000
2024-12-01 00:50:49.895931 drawing frame 1, loop 53/1000
2024-12-01 00:50:49.914491 drawing frame 1, loop 54/1000
2024-12-01 00:50:49.933117 drawing frame 1, loop 55/1000
2024-12-01 00:50:49.951866 drawing frame 1, loop 56/1000
2024-12-01 00:50:49.970403 drawing frame 1, loop 57/1000
2024-12-01 00:50:49.989122 drawing frame 1, loop 58/1000
2024-12-01 00:50:50.009298 drawing frame 1, loop 59/1000
2024-12-01 00:50:50.028540 drawing frame 1, loop 60/1000
2024-12-01 00:50:50.047204 drawing frame 1, loop 61/1000
2024-12-01 00:50:50.069784 drawing frame 1, loop 62/1000
2024-12-01 00:50:50.088420 drawing frame 1, loop 63/1000
2024-12-01 00:50:50.107159 drawing frame 1, loop 64/1000
2024-12-01 00:50:50.125749 drawing frame 1, loop 65/1000
2024-12-01 00:50:50.144559 drawing frame 1, loop 66/1000
2024-12-01 00:50:50.163359 drawing frame 1, loop 67/1000
2024-12-01 00:50:50.181996 drawing frame 1, loop 68/1000
2024-12-01 00:50:50.200634 drawing frame 1, loop 69/1000
2024-12-01 00:50:50.219405 drawing frame 1, loop 70/1000
2024-12-01 00:50:50.237962 drawing frame 1, loop 71/1000
2024-12-01 00:50:50.256657 drawing frame 1, loop 72/1000
2024-12-01 00:50:50.275384 drawing frame 1, loop 73/1000
2024-12-01 00:50:50.293984 drawing frame 1, loop 74/1000
2024-12-01 00:50:50.312632 drawing frame 1, loop 75/1000
2024-12-01 00:50:50.331322 drawing frame 1, loop 76/1000
2024-12-01 00:50:50.350295 drawing frame 1, loop 77/1000
2024-12-01 00:50:50.369343 drawing frame 1, loop 78/1000
2024-12-01 00:50:50.388232 drawing frame 1, loop 79/1000
2024-12-01 00:50:50.407057 drawing frame 1, loop 80/1000
2024-12-01 00:50:50.425871 drawing frame 1, loop 81/1000
2024-12-01 00:50:50.444580 drawing frame 1, loop 82/1000
2024-12-01 00:50:50.463243 drawing frame 1, loop 83/1000
2024-12-01 00:50:50.481987 drawing frame 1, loop 84/1000
2024-12-01 00:50:50.500526 drawing frame 1, loop 85/1000
2024-12-01 00:50:50.519217 drawing frame 1, loop 86/1000
2024-12-01 00:50:50.537943 drawing frame 1, loop 87/1000
2024-12-01 00:50:50.556549 drawing frame 1, loop 88/1000
2024-12-01 00:50:50.575226 drawing frame 1, loop 89/1000
2024-12-01 00:50:50.593797 drawing frame 1, loop 90/1000
2024-12-01 00:50:50.612431 drawing frame 1, loop 91/1000
2024-12-01 00:50:50.631206 drawing frame 1, loop 92/1000
2024-12-01 00:50:50.649758 drawing frame 1, loop 93/1000
2024-12-01 00:50:50.668456 drawing frame 1, loop 94/1000
2024-12-01 00:50:50.687195 drawing frame 1, loop 95/1000
2024-12-01 00:50:50.705823 drawing frame 1, loop 96/1000
2024-12-01 00:50:50.724489 drawing frame 1, loop 97/1000
2024-12-01 00:50:50.743086 drawing frame 1, loop 98/1000
2024-12-01 00:50:50.761710 drawing frame 1, loop 99/1000
2024-12-01 00:50:50.780461 drawing frame 1, loop 100/1000
2024-12-01 00:50:50.799022 drawing frame 1, loop 101/1000
2024-12-01 00:50:50.817735 drawing frame 1, loop 102/1000
2024-12-01 00:50:50.836483 drawing frame 1, loop 103/1000
2024-12-01 00:50:50.855113 drawing frame 1, loop 104/1000
2024-12-01 00:50:50.873787 drawing frame 1, loop 105/1000
2024-12-01 00:50:50.892401 drawing frame 1, loop 106/1000
2024-12-01 00:50:50.905984 drawing frame 2, loop 107/1000
2024-12-01 00:50:50.924852 drawing frame 2, loop 108/1000
2024-12-01 00:50:50.943472 drawing frame 2, loop 109/1000
2024-12-01 00:50:50.962205 drawing frame 2, loop 110/1000
2024-12-01 00:50:50.980918 drawing frame 2, loop 111/1000
2024-12-01 00:50:50.999537 drawing frame 2, loop 112/1000
2024-12-01 00:50:51.018242 drawing frame 2, loop 113/1000
2024-12-01 00:50:51.036998 drawing frame 2, loop 114/1000
2024-12-01 00:50:51.055561 drawing frame 2, loop 115/1000
2024-12-01 00:50:51.074261 drawing frame 2, loop 116/1000
2024-12-01 00:50:51.087948 drawing frame 3, loop 117/1000
2024-12-01 00:50:51.106688 drawing frame 3, loop 118/1000
2024-12-01 00:50:51.125408 drawing frame 3, loop 119/1000
2024-12-01 00:50:51.144189 drawing frame 3, loop 120/1000
2024-12-01 00:50:51.162899 drawing frame 3, loop 121/1000
2024-12-01 00:50:51.181723 drawing frame 3, loop 122/1000
2024-12-01 00:50:51.200521 drawing frame 3, loop 123/1000
2024-12-01 00:50:51.219410 drawing frame 3, loop 124/1000
2024-12-01 00:50:51.238046 drawing frame 3, loop 125/1000
2024-12-01 00:50:51.256826 drawing frame 3, loop 126/1000
2024-12-01 00:50:51.270555 drawing frame 4, loop 127/1000
2024-12-01 00:50:51.289416 drawing frame 4, loop 128/1000
2024-12-01 00:50:51.308104 drawing frame 4, loop 129/1000
2024-12-01 00:50:51.326918 drawing frame 4, loop 130/1000
2024-12-01 00:50:51.345705 drawing frame 4, loop 131/1000
2024-12-01 00:50:51.364604 drawing frame 4, loop 132/1000
2024-12-01 00:50:51.383273 drawing frame 4, loop 133/1000
2024-12-01 00:50:51.402113 drawing frame 4, loop 134/1000
2024-12-01 00:50:51.420920 drawing frame 4, loop 135/1000
2024-12-01 00:50:51.439795 drawing frame 4, loop 136/1000

For 1 cycle of 4 frames, there were a total of 137 redraws on my computer (this number varies depending on your hardware)

I believe this number can be lowered from 137 -> 4, it didn't have any noticable visual impact for me when I did so but I'm not across the whole project and its nuances.


Changes

I tried to implement this as surgically as possible, avoiding changing the existing code at this stage.

  • the startPlaying function iterates through a while loop
    • it does some state/user input checks and operations
    • calls self.refresh() to draw the self.mov.currentFrame
    • then finally, does some delay/timing checks
      • and either moves to the next frame with self.mov.nextFrame()
      • OR, sleeps for 0.005 and loops again

This final dot point is the culprit, as self.mov.nextFrame() was not called, the next loop iteration will end up redrawing the current frame again.

In order to adjust this, I converted this delay check into a while True loop, which does the delay timing check, and then sleeps if needed, or breaks if the delay isn't needed.

Results

The number of frames drawn has been reduced by 97.09%

This is the difference in CPU usage for the doge blink animation as reported by /usr/bin/time

version CPU usage
current 29%
this PR 4%

Caveats/Questions

Am I missing anything here? Will this have any negative/unintended consequences?

to help pinpoint cpu usage and where time is spent when running
durview
the startPlaying function has a check at the end of it that does some
timing/frame delay calculations, and then calls self.mov.nextFrame() if
it's time to advance to the next frame

However, if it's not time to go to the next frame, then the current
frame is redrawn again, which has no visual effect.
These redundant redraws have a big effect on the CPU usage of durview,
and removing them has improved this
@tmck-code tmck-code changed the title Durview reduce frame redraws Durview: Reduce frame redraws Dec 1, 2024
@tmck-code
tmck-code marked this pull request as ready for review December 1, 2024 01:42
@tmck-code tmck-code changed the title Durview: Reduce frame redraws Durview: Reduce (#1) frame redraws Dec 1, 2024
@cmang

cmang commented Dec 1, 2024 •

Copy link
Copy Markdown
Member

Bro, this is fire. Thank you for this profiling and optimization work! And thorough explanation. I think you're on the right track trying to minimize unnecessary character and frame updates.

There is a problem, though. I just tested your branches, and if you play the movie while editing (esc-p), the user interface slows to a crawl. Try moving the cursor around while playing. It's faster (still not super fast, but usable) in the dev branch.

It is, of course, useful to be able to edit the animation while it is playing, to keep drawing updates applying across all frames. If that makes sense.

I think if we can fix the user input issue, hopefully this will make Durdraw fly on slower hardware. :)

@cmang

cmang commented Dec 1, 2024 •

Copy link
Copy Markdown
Member

I believe this is because it is stuck in the while True: loop, waiting for the break, before the program loops back around to run getch() and process user input.

Normally the UI is throttled during playback mode, I believe, by the loop and sleep(). It would be nice if we could lower the sleep to make the UI more responsive, while still maintaining your optimizations of only calling refresh() when needed.

@tmck-code

Copy link
Copy Markdown
Contributor Author

I believe this is because it is stuck in the while True: loop, waiting for the break, before the program loops back around to run getch() and process user input.

Yeah you're right! I realised this today, I have an idea to move the check instead to where the self.refresh() is called in the loop.

Additionally, I have to add logic to check whether any characters in the current frame have changed, it won't be enough to just check that the frame number is the same as before

@tmck-code

Copy link
Copy Markdown
Contributor Author

^other options would include

  • making everything async (maybe I can tackle this one day)
  • refactor the startPlaying method, extracting the user input logic out into a separate method so that it can be called in a different sequence more easily
  • some other curses lib magic that I'm not aware of yet

@cmang

cmang commented Dec 1, 2024

Copy link
Copy Markdown
Member

Bless you for wading through and making sense of my clusterfsck code.

If you are feeling ambitious, the other optimization that would make a big difference in speed (while editing) is a more efficient undo/redo system. Currently undo.push() will deepcopy() the entire canvas state on every edit, and rolls back the entire canvas on undo. This is super slow, especially as the canvas size and number of frames grows.

Possible ways to speed this up might be:

  • Manually copy the canvas data instead of relying on deepcopy()
  • Make the undo/redo system work on a diff instead of a full copy

if in editor mode (aka not playOnlyMode), then break out of the delay
loop and check for user input

now that the loop is non blocking, need to add a boolean to indicate if
nextFrame should be called
@cmang

cmang commented Dec 1, 2024 •

Copy link
Copy Markdown
Member

if in editor mode (aka not playOnlyMode), then break out of the delay loop and check for user input

The problem with this is, now the UI in viewer mode is blocked. :) Try Pressing "?" or "i" while the doge playback is going. It will not respond until it's done with its frame delays.

Initial testing of that last patch to fix the UI blocking, though, shows some big improvements:

Before your changes:
Playback edit mode and playback viewer mode both around 40% cpu.

Now: Viewer playback mode 0-2% cpu, Edit playback mode: 6-10%.

I am not sure if there are any other issues, besides the slow UI in viewer playback mode. Non-viewer playback is looking good.

- extract logic for input char handling into a method
- call it like normal in the loop
- additionally, call it in the loop that waits for the next frame to be
  ready to render
@tmck-code

tmck-code commented Dec 1, 2024 •

Copy link
Copy Markdown
Contributor Author

Bless you for wading through and making sense of my clusterfsck code.

Thanks for all your explanations! Really helped clarify some things that I wasn't quite understanding or seeing. I think this is a really cool app and an impressive undertaking for a single-person project. I very much understand what it's like to be in the weeds with your own code.

I'm trying to start off slow rather than bolt headfirst into proposing an abstraction or improvement that doesn't gel with your approach, I do love performance and organisation so I'm slowly formulating some ideas. I definitely think that the CPU usage of this app can be <1-2%, in almost all scenarios if the right plan is enacted

If you are feeling ambitious, the other optimization that would make a big difference in speed (while editing) is a more efficient undo/redo system. Currently undo.push() will deepcopy() the entire canvas state on every edit, and rolls back the entire canvas on undo. This is super slow, especially as the canvas size and number of frames grows.

This is definitely up my alley, will have a look into this part after this improvement attempt :shipit:

One question - at some point I think it would be good to add some tests (unit/integration etc), so that behaviour changes during refactors can be noticed more easily. Do you have any preference for testing approaches? (I'm a fan of pytest personally but open to anything). I'm an experienced/OK test writer so I'm happy to tackle this at some point if you'd like?

@cmang

cmang commented Dec 1, 2024 •

Copy link
Copy Markdown
Member

I think this is a really cool app and an impressive undertaking for a single-person project. I very much understand what it's like to be in the weeds with your own code.

Thank you. I really appreciate that. This is the largest program I have ever made by myself, and it is really cool to get some help with the code.

I'm trying to start off slow rather than bolt headfirst into proposing an abstraction or improvement that doesn't gel with your approach, I do love performance and organisation so I'm slowly formulating some ideas.

durdraw_ui_curses.py is a monster. I am definitely open to suggestions, refactoring and breaking it up. :)

You should take a look at the todo wishlist I have going in the Wiki.

I love that you are working on performance. It is not my area of expertise.

I definitely think that the CPU usage of this app can be <1-2%, in almost all scenarios if the right plan is enacted

That would be awesome!

One question - at some point I think it would be good to add some tests (unit/integration etc), so that behaviour changes during refactors can be noticed more easily. Do you have any preference for testing approaches? (I'm a fan of pytest personally but open to anything). I'm an experienced/OK test writer so I'm happy to tackle this at some point if you'd like?

I agree. I think this would help organize the code better, and could have saved me from introducing bugs in releases.

Something I forgot to add to CONTRIBUTING.md: Durdraw should always be able to run using the Python standard library, without any dependencies, even if some features are unavailable. That level of portability is important to me (or at least is something I aim for), and I think is part of its appeal. I am only willing to break it if there is a very good reason.

Since tests are mainly used for development, though, it might be fine to use a dependency for that.

@cmang

cmang commented Dec 1, 2024

Copy link
Copy Markdown
Member

I am not a super experienced test writer, so outside of preferring to lean on the standard library (especially for end users), I don't have a preference.

If the standard library's unittest really sucks that much.. we don't have to use it. lol

@tmck-code

Copy link
Copy Markdown
Contributor Author

I am not a super experienced test writer, so outside of preferring to lean on the standard library (especially for end users), I don't have a preference.

If the standard library's unittest really sucks that much.. we don't have to use it. lol

I agree with this approach of having no dependencies, my only 2 exceptions to this rule usually are requests and pytest as the standard lib equivalents aren't as good to work with (dateutil used to be another before it was incorporated into the standard lib).

Also, as you say, end users won't have to install pytest to run durdraw, so it would remain 0-dep from that perspective

I'm happy to mock up some competing test examples whenever I end up embarking on that area, so you can make a call 🤙

@cmang cmang left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

k-rad elite

@cmang
cmang merged commit 543ba1f into durdraw:dev Dec 1, 2024
@tmck-code
tmck-code deleted the durview-reduce-frame-redraws branch December 2, 2024 01:33
cmang added a commit that referenced this pull request Dec 11, 2024
@cmang

cmang commented Dec 11, 2024

Copy link
Copy Markdown
Member

Heads up, this seems to have introduced a few bugs.

1: Using - and + or = to change the FPS in viewer mode was no longer changing the framerate in viewer mode in real-time. The FPS value would update, but not the actual playback speed, until viewer mode is restarted. This is because sleep_time's scope was split into 2 functions when handlePlayOnlyModeInput() was refactored out from startPlaying(). I fixed this by making sleep_time a member variable of appState.

2: The playback speed seems to top out at 25 FPS now. You can increase it to 50 FPS, but it's not actually playing back any faster. (This used to work.) I'm not quite sure why yet.

3: Keyboard commands in viewer mode used to work whether or not you accessed them with alt. Eg: alt-i would show info, alt-= would increase the speed, etc.

@cmang

cmang commented Dec 11, 2024

Copy link
Copy Markdown
Member

2: The playback speed seems to top out at 25 FPS now. You can increase it to 50 FPS, but it's not actually playing back any faster. (This used to work.) I'm not quite sure why yet.

This should be better now. Tom, your patch changed the sleep time in the "cpu resting loop" from 0.005 to 0.04, which caused the 25 FPS ceiling. Since we have artists drawing 30 FPS ansis with Durdraw, that was a problem. I changed it to 0.009 as a compromise.

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