Skip to content

v5: split into PbfReader/PbfWriter; 25% faster#143

Merged
mourner merged 11 commits into
mainfrom
v5
May 26, 2026
Merged

v5: split into PbfReader/PbfWriter; 25% faster#143
mourner merged 11 commits into
mainfrom
v5

Conversation

@mourner
Copy link
Copy Markdown
Member

@mourner mourner commented May 22, 2026

Breaking change: the single Pbf class is split into PbfReader and PbfWriter, allowing readers and writers to be tree-shaken independently in bundled apps.

Removed: the legacy readVarint64 method (replaced by readVarint(true) for signed 64-bit reads).

Performance: several targeted optimizations to both read and write hot paths, validated against bench/bench-tiles.js:

  • makeRoomForExtraLength uses Uint8Array.copyWithin instead of a manual byte-shift loop (biggest write win).
  • writeVarint gets a single-byte fast path, skipping the multi-byte branches and full realloc(4) for the common case (tags, small ints).
  • readVarint gets a matching single-byte fast path.
  • writeBytes uses typedArray.set instead of a manual loop.
    README updated with fresh medians (3 runs of bench/bench-tiles.js, Node v26).
  • ~18% faster decode for the Mapbox vector-tile workload (~236 → ~200 ms, 159 → 187 MB/s) by rewriting reader codegen from a callback-style pbf.readFields(cb, obj, end) into inlined while loops with field-number dispatch. The win comes from eliminating megamorphic callback dispatch — V8 can now fully inline each generated reader.

End-to-end on the tile benchmark: write throughput improved from ~169 MB/s to ~215 MB/s (~27% faster), and read throughput from ~150 MB/s to ~187 MB/s (~25% faster).

stepankuzmin
stepankuzmin previously approved these changes May 25, 2026
Copy link
Copy Markdown

@stepankuzmin stepankuzmin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@mourner mourner changed the title v5: split into PbfReader and PbfWriter v5: split into PbfReader and PbfWriter; 25% faster encode & decode May 25, 2026
@mourner mourner changed the title v5: split into PbfReader and PbfWriter; 25% faster encode & decode v5: split into PbfReader/PbfWriter; 25% faster May 25, 2026
stepankuzmin
stepankuzmin previously approved these changes May 26, 2026
Copy link
Copy Markdown

@stepankuzmin stepankuzmin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Impressive!

Copy link
Copy Markdown

@stepankuzmin stepankuzmin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great success!

@mourner mourner merged commit b920044 into main May 26, 2026
6 checks passed
@mourner mourner deleted the v5 branch May 26, 2026 13:43
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