Skip to content

Size the hash staging buffer to the input and cache the HMAC pads - #103

Merged
Xor-el merged 1 commit into
masterfrom
perf/reduce-alloc-churn
Aug 26, 2026
Merged

Size the hash staging buffer to the input and cache the HMAC pads#103
Xor-el merged 1 commit into
masterfrom
perf/reduce-alloc-churn

Conversation

@Xor-el

@Xor-el Xor-el commented Aug 26, 2026

Copy link
Copy Markdown
Owner

Both hot paths allocated more than the work required. TransformUntyped and TransformStream sized their staging buffer to the full 64 KB default regardless of input, so hashing a one-byte message (as HKDF does, byte by byte) still cleared a 64 KB buffer; a shared StagingSize helper now clamps the buffer to the actual input length, honouring a user-set BufferSize as the chunk maximum. THMACNotBuildInAdapter recomputed ipad/opad and copied the key on every Initialize — and HKDF re-inits the HMAC for every Extract and Expand block — so an FPadsValid flag now caches the pads and rebuilds them only when the key changes, invalidated on both SetKey and Clear.

Both hot paths allocated more than the work required. TransformUntyped
and TransformStream sized their staging buffer to the full 64 KB default
regardless of input, so hashing a one-byte message (as HKDF does, byte by
byte) still cleared a 64 KB buffer; a shared StagingSize helper now clamps
the buffer to the actual input length, honouring a user-set BufferSize as
the chunk maximum. THMACNotBuildInAdapter recomputed ipad/opad and copied
the key on every Initialize — and HKDF re-inits the HMAC for every Extract
and Expand block — so an FPadsValid flag now caches the pads and rebuilds
them only when the key changes, invalidated on both SetKey and Clear.
@Xor-el
Xor-el merged commit 4f720dd into master Aug 26, 2026
28 checks passed
@Xor-el
Xor-el deleted the perf/reduce-alloc-churn branch August 26, 2026 00:21
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