Skip to content

perf(encode): bypass token creation for primitives and arrays#163

Merged
rvagg merged 1 commit intomasterfrom
rvagg/direct-encode
Jan 20, 2026
Merged

perf(encode): bypass token creation for primitives and arrays#163
rvagg merged 1 commit intomasterfrom
rvagg/direct-encode

Conversation

@rvagg
Copy link
Copy Markdown
Owner

@rvagg rvagg commented Jan 20, 2026

Implement direct encoding path that writes CBOR bytes without intermediate Token allocation for most value types:

  • Primitives: null, boolean, number, bigint, string, Uint8Array
  • Arrays: header + recursive direct encode of elements
  • Maps: fall back to token-based encoding (required for key sorting)
  • Custom typeEncoders: checked per-value, fall back to tokens as needed

Benchmark results (dag-cbor mode):

  • Integers: +25-66% encode throughput
  • Bytes: +17-20%
  • Strings: +15-17%
  • Overall w/ current benchmark: ~3% average improvement

Implement direct encoding path that writes CBOR bytes without
intermediate Token allocation for most value types:
- Primitives: null, boolean, number, bigint, string, Uint8Array
- Arrays: header + recursive direct encode of elements
- Maps: fall back to token-based encoding (required for key sorting)
- Custom typeEncoders: checked per-value, fall back to tokens as needed

Benchmark results (dag-cbor mode):
- Integers: +25-66% encode throughput
- Bytes: +17-20%
- Strings: +15-17%
- Overall w/ current benchmark: ~3% average improvement
@rvagg rvagg merged commit d79b602 into master Jan 20, 2026
16 checks passed
@rvagg rvagg deleted the rvagg/direct-encode branch January 20, 2026 11:25
@github-actions
Copy link
Copy Markdown

🎉 This PR is included in version 4.5.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant