perf: midi-file Writer uses preallocated Uint8Array - #80
Closed
elicwhite wants to merge 1 commit into
Closed
Conversation
Replace Writer's backing Array<number> with a growable Uint8Array +
manual cursor. Benefits:
- Raw bytes instead of boxed JS numbers — ~8× less memory per byte
and much less GC pressure.
- writeBytes on a Uint8Array source is a single .set() call instead
of a push loop.
- writeVarInt no longer allocates a temp Array to hold the bytes
then .reverse()s it — the 1-5 byte tail is written directly into
the buffer.
API shape preserved for callers:
- Inner sub-writers expose .used() which returns a subarray view.
- Top-level writeMidi returns the used subarray; scan-chart's
`new Uint8Array(writeMidi(midiData))` still works (copies the
subarray into a dedicated Uint8Array), and the few other direct
callers in spotify-clonehero-next that wrap the result in
Uint8Array also still work.
Measured on writer autoresearch bench (2000 charts, 8 workers):
previous (post-writeBytes fix): 6.721 ms mean, 25.263 ms p99, 77 ms max, 5.496s wall
this patch: 5.710 ms mean, 22.576 ms p99, 42 ms max, 5.132s wall
delta: -15.0% mean, -10.6% p99, -45% max, -6.6% wall
0 hash mismatches, 442/442 tests still green.
This was referenced Apr 20, 2026
Owner
Author
This was referenced Apr 20, 2026
elicwhite
marked this pull request as ready for review
April 20, 2026 16:09
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.

Replace Writer's backing Array with a growable Uint8Array +
manual cursor. Benefits:
and much less GC pressure.
of a push loop.
then .reverse()s it — the 1-5 byte tail is written directly into
the buffer.
API shape preserved for callers:
new Uint8Array(writeMidi(midiData))still works (copies thesubarray into a dedicated Uint8Array), and the few other direct
callers in spotify-clonehero-next that wrap the result in
Uint8Array also still work.
Measured on writer autoresearch bench (2000 charts, 8 workers):
previous (post-writeBytes fix): 6.721 ms mean, 25.263 ms p99, 77 ms max, 5.496s wall
this patch: 5.710 ms mean, 22.576 ms p99, 42 ms max, 5.132s wall
delta: -15.0% mean, -10.6% p99, -45% max, -6.6% wall
0 hash mismatches, 442/442 tests still green.