Skip to content

Comments

perf: reduce allocation churn in operator serialization#31

Merged
Mythie merged 3 commits intoLibPDF-js:mainfrom
ntedvs:perf/operator-serialization
Feb 19, 2026
Merged

perf: reduce allocation churn in operator serialization#31
Mythie merged 3 commits intoLibPDF-js:mainfrom
ntedvs:perf/operator-serialization

Conversation

@ntedvs
Copy link
Contributor

@ntedvs ntedvs commented Feb 19, 2026

Summary

Each Operator.toBytes() call created ~10+ intermediate Uint8Array allocations (one per operand, one per space separator, one concat result). Then serializeOperators() did the same pattern again at a higher level with newlines. For 100 rectangles, that's ~7,600 throwaway allocations per iteration.

Added Operator.writeTo(writer: ByteWriter) so operators write directly into a shared buffer instead of building intermediate arrays. Rewrote serializeOperators() to use a single ByteWriter instead of collecting per-operator byte arrays and concatenating.

Also removed byteLength() (dead code, zero production callers).

Numbers

Drawing benchmarks (absolute throughput):

Benchmark Before After Speedup
draw 100 rectangles 1,528 ops/s 3,635 ops/s 2.38x
draw 100 circles 756 ops/s 2,161 ops/s 2.86x
draw 100 lines 1,787 ops/s 3,921 ops/s 2.19x
draw 100 text lines 507 ops/s 1,026 ops/s 2.02x
10 pages mixed content 532 ops/s 1,396 ops/s 2.63x

Comparison against pdf-lib (draw 50 rectangles):

Before After
libpdf vs pdf-lib 1.75x faster 3.56x faster

Test plan

  • All existing tests pass (operators, drawing, integration)
  • Typecheck clean

@vercel
Copy link
Contributor

vercel bot commented Feb 19, 2026

@ntedvs is attempting to deploy a commit to the mythie's projects Team on Vercel.

A member of the Team first needs to authorize it.

@Mythie Mythie merged commit 1cab448 into LibPDF-js:main Feb 19, 2026
3 of 6 checks passed
@Mythie
Copy link
Contributor

Mythie commented Feb 19, 2026

Great work! I had similar changes on a branch but never got back to it 🙌🏻

@ntedvs
Copy link
Contributor Author

ntedvs commented Feb 19, 2026

Thank you! Any tips for contributing going forward?

@ntedvs
Copy link
Contributor Author

ntedvs commented Feb 21, 2026

@Mythie ?

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