Skip to content

perf(ascii-anim): row-buffered output + skip per-frame tmp file#1830

Merged
krystophny merged 2 commits intomainfrom
feature/ascii-animation-perf
May 7, 2026
Merged

perf(ascii-anim): row-buffered output + skip per-frame tmp file#1830
krystophny merged 2 commits intomainfrom
feature/ascii-animation-perf

Conversation

@krystophny
Copy link
Copy Markdown
Collaborator

Summary

  • ASCII canvas rows were emitted as one 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 single write. Speedup applies to all ASCII output (file + terminal).
  • save_ascii_animation no longer round-trips each frame through a temporary file. After savefig_with_status populates the figure's ASCII canvas, the new ascii_context%save_to_unit dumps it straight into the open animation output unit.
  • New test_ascii_anim_perf guards 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 and save_ascii_animation still 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 through fortplot_play_ascii.

krystophny added 2 commits May 7, 2026 14:01
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.
@krystophny krystophny merged commit 85d3f69 into main May 7, 2026
5 checks passed
@krystophny krystophny deleted the feature/ascii-animation-perf branch May 7, 2026 12:08
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.

1 participant