perf(de): inline LEB128 and add map fast paths#722
Merged
Conversation
Four decode-side optimizations, all behavior-preserving: 1. Nat/Int deserialization bypass: for values fitting u64/i64, read LEB128 directly and call visitor.visit_u64/i64, avoiding the BigUint/BigInt → bytes → BigUint round-trip (saves 3 allocations per value). 2. BigNum vector fast path: batch cost tracking and skip per-element type cloning/checking for Vec<Nat>, Vec<Int>, and Vec<Int> with Nat wire type, mirroring the existing primitive vec fast path. 3. PrimitiveVecAccess with IntoDeserializer: on LE platforms, decode primitive vectors via a lightweight SeqAccess that reads directly from the input byte slice using serde's IntoDeserializer, bypassing the full Deserializer and Cursor overhead. 4. Borrowed string deserialization: use visit_borrowed_str instead of copying bytes, enabling zero-copy for &str targets. Benchmark improvements (decode, vs previous optimized baseline): vec_nat: 910M → 300M (-67%) vec_nat32: 406M → 247M (-39%) vec_nat64: 411M → 255M (-38%) vec_int16: 411M → 251M (-39%) btreemap: 13.3B → 11.2B (-16%) option_list: 23M → 18M (-20%) variant_list: 21M → 17M (-21%) Made-with: Cursor
### Motivation - Remove external `binary_parser` dependency for hot deserialization paths. ### Solution - Inline LEB128 u64/i64, length, and bool reading directly in `Deserializer`. - Add `text_fast_path` flag to skip type checks when key types are known during map deserialization. - Apply fast path to map keys/values and bignum handling. ### Meta - Removed unused `BoolValue` import.
Click to see raw report |
…st paths Introduce `try_read_leb_u64` and `try_read_leb_i64` returning `Ok(None)` on overflow and `Err` on genuine I/O errors (e.g. unexpected EOF). The bignum fallback callers in `deserialize_int` and `deserialize_nat` now use these, so a truncated input correctly surfaces an error rather than silently falling through to a secondary bignum decode failure. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Keep inlined LEB128/bool/len readers and map fast paths from PR - Keep try_read_leb_* error-propagation fix - Take master's deserialize_string -> deserialize_str delegation - Take master's PrimitiveVecAccess cursor fix (advance by access.offset) - Take master's SeqAccess expect/wire_type assignment and if !is_fast restructure Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
lwshang
approved these changes
Mar 18, 2026
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.
Motivation
binary_parserdependency for hot deserialization paths.Solution
Deserializer.text_fast_pathflag to skip type checks when key types are known during map deserialization.Meta
BoolValueimport.