perf: fast-path small candid numbers#709
Merged
Merged
Conversation
Encode and decode Nat and Int values through native LEB128 when they fit in machine integers so common small numbers avoid bigint work.
Click to see raw report |
Mirror the u64-accumulation fast path already added to Nat::decode. Accumulate bits into a u64 with the same overflow guard, then sign-extend at the last byte to produce an i64. Values that fit in i64 skip BigInt allocation entirely; values in [2^63, 2^64-1] (valid Int but not i64) still use BigInt correctly. The BigInt fallback for truly large numbers mirrors the structure of Nat::decode's fallback. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Cover the exact values where the u64 fast path hands off to BigInt: - Nat: u64::MAX (last fast-path value) and u64::MAX+1 (first BigInt), plus a sweep of values near the boundary. - Int: i64::MAX and i64::MAX+1 (positive overflow branch), i64::MIN (sign-extension fast path) and i64::MIN-1 (BigInt negative branch). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Add inline comments explaining why the `shift < 64` branch must be tested before evaluating `1u64 << (64 - shift)`: the expression would panic in debug mode if shift were >= 64, so the else-false arm acts as both the panic guard and the BigInt-fallback trigger. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The identical 7-line fits_u64 guard existed verbatim in both Nat::decode and Int::decode. Extract it into a shared inline helper function. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
lwshang
approved these changes
Mar 15, 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.
Overview
Performance improvement
Requirements
Preserve existing wire compatibility and keep record decoding behavior unchanged.
Solution
Encode and decode Nat and Int values through native LEB128 when they fit in machine integers so common small numbers avoid bigint work.
Considerations
I expect performance improvement and full forward and backward compatibility
Ref. #710