perf(ascii-anim): row-buffered output + skip per-frame tmp file#1830
Merged
krystophny merged 2 commits intomainfrom May 7, 2026
Merged
perf(ascii-anim): row-buffered output + skip per-frame tmp file#1830krystophny merged 2 commits intomainfrom
krystophny merged 2 commits intomainfrom
Conversation
Two stacked wins on the ASCII animation save path: * output_to_file/output_to_terminal previously wrote each canvas cell with a separate `write(unit, '(A)', advance='no')`. For an 80x30 canvas that's 2400 syscalls per frame and was the dominant cost in ASCII rendering. Replace with a per-row buffer assembled in memory and one write per row. Also benefits plain savefig to .txt. * save_ascii_animation no longer reads the per-frame ASCII back from a temporary file. After savefig populates the figure's ASCII canvas it asks the backend to dump straight into the already-open animation output unit (new ascii_context%save_to_unit method). Net: a 1000-frame perf microbenchmark drops from 0.44 s to 0.18 s (~2.4x), with the per-frame I/O overhead going from ~0.10 s/1000fr to near zero. test_ascii_anim_perf guards the regression at 5 s on CI hardware.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
write(advance='no')per cell. For an 80x30 canvas that is 2400 syscalls per frame, dominating ASCII render cost. Build each row in a buffer and emit it with a singlewrite. Speedup applies to all ASCII output (file + terminal).save_ascii_animationno longer round-trips each frame through a temporary file. Aftersavefig_with_statuspopulates the figure's ASCII canvas, the newascii_context%save_to_unitdumps it straight into the open animation output unit.test_ascii_anim_perfguards the path with a 5 s wall-clock budget for 1000 frames.Verification
Test fails on main
Microbenchmark on main:
```
per-frame savefig only: 0.339 s for 1000 frames
save_animation took 0.437 s for 1000 frames
```
Per-cell
advance='no'writes are pathological under streaming workloads andsave_ascii_animationstill pays the open/close/copy cost for every frame.Test passes after fix
```
$ fpm test test_ascii_anim_perf
per-frame savefig only: 0.176 s for 1000 frames
save_animation took 0.181 s for 1000 frames
PASS test_ascii_anim_perf
```
Roughly 2.4x faster end-to-end on a Lissajous-style 200-point 3D animation. The frame envelope (
=== Frame N ===) and player/replay continue to round-trip cleanly throughfortplot_play_ascii.