← Back to Pull requests

perf(bindings): let the compiler inline StringValue #248

Closed
unknown closed this pull request without merging on Nov 18, 2026 15:46 UTC
View on GitHub
unknown unknown commented on Nov 16, 2026 11:22 UTC
Author

What

WAFObject.StringValue copies the string out of C memory, so each call allocates. unwrapWafResult calls it for each key of the WAF result, only to compare the key with a fixed list of names (switch key). This makes 7 allocations for each Run.

This PR splits StringValue into:

  • StringValue, a small wrapper with an inline cost of 70 (the budget is 80);

  • stringBytes, a new unexported method with the old type switch. It returns the bytes without a copy.
  • The compiler inlines StringValue into the caller. When the caller does not keep the result, the compiler puts the copy on the stack, so a key of 32 bytes or less does not allocate. StringValue still returns a copy: the signature, the error texts and the memory ownership do not change. result.go, decoder.go, libddwaf.go and waf_dl.go do not change.

    I did not use an exported accessor that compares in place (KeyEquals), or an unsafe.String view. waf_object.go aliases WAFObject, so an exported method is public API. A view that escapes is a use-after-free on C memory. With this design, a mistake costs 1 allocation and never memory safety.

    Part of DD-49. The decoder keys (decoder.go) are not in this PR. They wait for #246.

    Result

    | | main | this PR |
    |---|---|---|
    | unwrapWafResult, 7 keys | 7 allocs, about 84 ns | 0 allocs, about 43 ns |
    | BenchmarkRunOnly | 73 allocs/op | 66 allocs/op |
    | BenchmarkSubcontextRunOnly | 29 allocs/op | 22 allocs/op |
    | BenchmarkContextRun | 32 allocs/op | 25 allocs/op |

    Guards

  • TestStringValueDoesNotAllocate and TestUnwrapWafResultKeysDoNotAllocate fail if StringValue stops being inlinable. They cover the default build only (not -pgo, not -gcflags=-l). They skip the check on Go devel versions, because the inliner can change in tip.

  • TestStringBytesSourceUse allows exactly 2 uses of stringBytes: its declaration, and 1 direct call in (*WAFObject).StringValue. TestStringBytesNoLinkname reads the real //go:linkname directives in the comments of all .go files of the module (test files, internal/bindings and build-tagged files included), and rejects one that names stringBytes. Doc text and quoted fixtures do not count. Both are a source-use guard, not a lifetime proof: they limit where the view can come from. They do not check what StringValue does with it.

  • TestStringValueReturnsCopy, TestStringValueLocalCopy (values that do not escape, lengths 0 to 128) and TestStringValueCopiesFromUnmappedMemory check that the value is a copy. I made StringValue return an unsafe.String view as a test: the copy tests fail, and the unmapped-memory test crashes with a fault.
  • Commit 1 has the tests that pass on main. Commit 2 has the change and the guards.

    Time

    Protocol, defined before the run (protocol/run.sh and analyze.py are in the files attached to DD-49):

  • arms: commit 1 (main + tests), the same binary under another name (A/A control), and commit 2;

  • 20 rounds. In each round, the CPU settings run in random order, and inside each setting the arms run in random order;

  • each invocation: -test.count=1 -test.benchtime=0.5s -test.benchmem;

  • ratio = change / base for each round. The table gives the median and the 95 % bootstrap interval (10000 resamples);

  • verdict: faster if the upper bound is below 0 %, slower if the lower bound is above 0 %, else inconclusive. There is no margin. An inconclusive result is not a pass.
  • Host: darwin/arm64, Apple M5 Pro, go1.27.4, normal build (no PGO). The host was not quiet. The 1-minute load average before the rounds was 4.0 to 7.5 (median 6.2). Linux was not measured: Docker was not available on the host.

    BenchmarkDecodeDiagnostics is not on main and PR 1 does not change it, so it is not in this run.

    This PR (median ratio [95 % interval, in %] verdict):

    | Benchmark | -test.cpu=1 | default (18 CPUs) | allocs/op |
    |---|---|---|---|
    | RunOnly | +0.22 % [-1.55, +3.33] inconclusive | +0.74 % [-5.46, +3.22] inconclusive | 73 → 66 |
    | SubcontextRunOnly | -0.51 % [-3.15, +2.25] inconclusive | -2.53 % [-5.28, +2.49] inconclusive | 29 → 22 |
    | ContextRun | -1.96 % [-3.12, -0.01] faster | +0.12 % [-4.41, +2.79] inconclusive | 32 → 25 |
    | UnwrapWafResult | -49.37 % [-50.22, -48.23] faster | -44.18 % [-45.48, -42.92] faster | 7 → 0 |

    A/A control (the same binary run twice): all 8 results are inconclusive.

    | Benchmark | -test.cpu=1 | default (18 CPUs) | allocs/op |
    |---|---|---|---|
    | RunOnly | +0.63 % [-1.10, +3.49] inconclusive | -0.20 % [-1.73, +0.68] inconclusive | 73 → 73 |
    | SubcontextRunOnly | -0.74 % [-2.82, +0.72] inconclusive | -1.01 % [-5.63, +0.54] inconclusive | 29 → 29 |
    | ContextRun | -1.00 % [-3.20, +1.92] inconclusive | +0.50 % [-3.64, +2.01] inconclusive | 32 → 32 |
    | UnwrapWafResult | -0.53 % [-1.78, +0.23] inconclusive | +0.14 % [-2.37, +3.30] inconclusive | 7 → 7 |

    Reading:

  • unwrapWafResult is faster by 44 % to 49 %, in both CPU settings.

  • ContextRun is faster with -test.cpu=1. The upper bound is −0.01 %, so this is close to the line.

  • The other Run benchmarks are inconclusive. No interval is above 0 %, and the upper bounds go up to +3.3 %. "No time regression" is not proved end to end on this host: a Run has about 40 ns less work out of 33 µs, which this host cannot resolve.

  • No result is slower.
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: eliottness Review duration: 3143m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    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.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #248"
    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: 52% (MEDIUM) · Higher = larger blast radius
    52%
    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: 20.5% · Safe to merge
    20.5%