Skip to content

perf(flags): Avoid BigInt allocations in identity hashing - #187

Draft
jaydgoss wants to merge 3 commits into
mainfrom
jaygoss/optimize-feature-hash-arithmetic
Draft

jaydgoss wants to merge 3 commits into
mainfrom
jaygoss/optimize-feature-hash-arithmetic

Conversation

@jaydgoss

@jaydgoss jaydgoss commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

Summary

Cut the digest-to-remainder step from about 61 ns to 2 ns by replacing BigInt with fixed-width arithmetic. These are isolated local release-mode measurements on an Apple M4 Max, not full feature-check or request timings.

This removes a repeated cost in #184: new flags calculate a feature-specific hash on each check instead of reusing the cached identity-only hash.

Replace the BigInt conversion in FeatureContext::compute_id() with fixed-width arithmetic. This change preserves the SHA-1 input and the resulting bucket IDs.

  • Read the 20-byte digest as five big-endian 32-bit words.
  • Use a plain loop: multiply the running remainder by 2³², add the next word, then take the remainder modulo 1_000_000_000. Reducing after each word gives the same result as converting the whole digest first.
  • Each intermediate value is below 1_000_000_000 × 2³² < 2⁶², so it fits in a u64 without heap allocation.
  • Move num from runtime dependencies to test dependencies.
  • Compare 10,000 generated identities of different lengths with the original BigInt calculation through the existing FeatureContext::id() API.

This draft is a possible future optimization for #184, not its base or a release dependency. #184 keeps the existing BigInt calculation and only removes the temporary feature-name prefix allocation. This PR does not add feature-specific hashing, cutoff handling, or a new cache. The existing ID cache is unchanged, so repeated checks that already use a cached ID do not gain this optimization.

Validation

  • cargo test -p sentry-options --lib: 97 tests passed.
  • cargo fmt --all -- --check: passed.
  • cargo clippy --workspace --all-features --tests -- -D clippy::all: passed.
  • git diff --check: passed.

@jaydgoss
jaydgoss marked this pull request as draft September 25, 2026 01:58
@jaydgoss
jaydgoss removed this pull request from stack #188 September 25, 2026 02:10
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.

1 participant