Repository navigation
Durview: Reduce (#1) frame redraws - #81
Conversation
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
|
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. :) |
|
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. |
Yeah you're right! I realised this today, I have an idea to move the check instead to where the 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 |
|
^other options would include
|
|
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:
|
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
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: 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
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
This is definitely up my alley, will have a look into this part after this improvement attempt 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 |
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.
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.
That would be awesome!
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. |
|
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 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 🤙 |
|
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. |
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. |
Context
I love the doge blink animation, and while running it the other day I noticed that
durviewwas using ~30% CPU to render it.I did some debugging using the
line_profilerproject and found that most of this CPU time was spent in therefresh(and subsequently,addstr) methods indurdraw/durdraw_ui_cursesIn 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 instartPlayingiteratedYou can see the profiling setup here if you're curious
https://github.com/cmang/durdraw/compare/dev...tmck-code:durdraw:profile-durviewThe logs contain a timestamp, the
currentFrameNumberthat is being drawn, and a counter to indicate the while loop iteration.refresh logs
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.
startPlayingfunction iterates through awhileloopself.refresh()to draw theself.mov.currentFrameself.mov.nextFrame()0.005and loops againThis 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 Trueloop, which does the delay timing check, and then sleeps if needed, or breaks if the delay isn't needed.Results
This is the difference in CPU usage for the doge blink animation as reported by
/usr/bin/timeCaveats/Questions
Am I missing anything here? Will this have any negative/unintended consequences?