← Back to Pull requests

Use a scoped zeroing allocation for the HMAC pad #490

Closed
unknown closed this pull request without merging on Oct 22, 2026 08:36 UTC
View on GitHub
unknown unknown commented on Oct 21, 2026 14:49 UTC
Author

Motivation:

Currently HMAC heap allocates twice per construction for temporary storage. HMAC.init(key:) builds the inner and outer pads using two SecureBytes allocations. This is a bottleneck for both HMAC and HKDF, which constructs an HMAC per output block. Both pads never escape the initializer, so neither needs to be on the heap at all. A scoped temporary allocation that zeroes after use does the same job with no allocations.

Modifications:

  • Fix the Benchmarks package build (missing Sendable annotation)

  • Add benchmarks for HMAC and HKDF

  • Add scoped zeroing temporary allocation helpers

  • Use a scoped zeroing allocation for the HMAC pad
  • Result:

  • HMAC<SHA256>.authenticationCode: was 5 allocations, now 3, ~20% faster.

  • HKDF<SHA256>.deriveKey, 1 KiB output: was 170 allocations, now 104, ~30% faster.
  • Benchmarks:

    hmac-sha256-authenticate (before)
    ╒═════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕
    │ Metric                  │        p0 │       p25 │       p50 │       p75 │       p90 │       p99 │      p100 │   Samples │
    ╞═════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡
    │ Malloc (total) *        │         5 │         5 │         5 │         5 │         5 │         5 │         5 │         5 │
    ├─────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤
    │ Time (total CPU) (ns) * │      1170 │      1251 │      1251 │      1271 │      1340 │      1340 │      1340 │         5 │
    ╘═════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛
    
    hmac-sha256-authenticate (after)
    ╒═════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕
    │ Metric                  │        p0 │       p25 │       p50 │       p75 │       p90 │       p99 │      p100 │   Samples │
    ╞═════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡
    │ Malloc (total) *        │         3 │         3 │         3 │         3 │         3 │         3 │         3 │         5 │
    ├─────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤
    │ Time (total CPU) (ns) * │       870 │       910 │       970 │      1030 │      1030 │      1030 │      1030 │         5 │
    ╘═════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛
    
    hkdf-sha256-derive (before)
    ╒═════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕
    │ Metric                  │        p0 │       p25 │       p50 │       p75 │       p90 │       p99 │      p100 │   Samples │
    ╞═════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡
    │ Malloc (total) *        │       170 │       170 │       170 │       170 │       170 │       170 │       170 │        10 │
    ├─────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤
    │ Time (total CPU) (μs) * │        20 │        20 │        30 │        30 │        30 │        30 │        30 │        10 │
    ╘═════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛
    
    hkdf-sha256-derive (after)
    ╒═════════════════════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╤═══════════╕
    │ Metric                  │        p0 │       p25 │       p50 │       p75 │       p90 │       p99 │      p100 │   Samples │
    ╞═════════════════════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡
    │ Malloc (total) *        │       104 │       104 │       104 │       104 │       104 │       104 │       104 │        10 │
    ├─────────────────────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┼───────────┤
    │ Time (total CPU) (μs) * │        10 │        20 │        20 │        20 │        20 │        20 │        20 │        10 │
    ╘═════════════════════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╧═══════════╛
    

    Credits:

    This PR is a re-work of the original contribution by @ptoffy. The merge-commit will show him as a co-author.

    Diff: 580 lines (+435 / -145) across 7 files Reviewer: Lukasa Review duration: 1067m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 580 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.381
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #490"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 49.9% (MEDIUM) · Higher = larger blast radius
    49.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.7% · Safe to merge
    19.7%