perf(bindings): let the compiler inline StringValue #248
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
eliottness
What
WAFObject.StringValuecopies the string out of C memory, so each call allocates.unwrapWafResultcalls 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 eachRun.This PR splits
StringValueinto: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
StringValueinto 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.StringValuestill returns a copy: the signature, the error texts and the memory ownership do not change.result.go,decoder.go,libddwaf.goandwaf_dl.godo not change.I did not use an exported accessor that compares in place (
KeyEquals), or anunsafe.Stringview.waf_object.goaliasesWAFObject, 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
TestStringValueDoesNotAllocateandTestUnwrapWafResultKeysDoNotAllocatefail ifStringValuestops being inlinable. They cover the default build only (not-pgo, not-gcflags=-l). They skip the check on Godevelversions, because the inliner can change intip.TestStringBytesSourceUseallows exactly 2 uses ofstringBytes: its declaration, and 1 direct call in(*WAFObject).StringValue.TestStringBytesNoLinknamereads the real//go:linknamedirectives in the comments of all.gofiles of the module (test files,internal/bindingsand build-tagged files included), and rejects one that namesstringBytes. 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 whatStringValuedoes with it.TestStringValueReturnsCopy,TestStringValueLocalCopy(values that do not escape, lengths 0 to 128) andTestStringValueCopiesFromUnmappedMemorycheck that the value is a copy. I madeStringValuereturn anunsafe.Stringview 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.shandanalyze.pyare in the files attached to DD-49):main+ tests), the same binary under another name (A/A control), and commit 2;-test.count=1 -test.benchtime=0.5s -test.benchmem;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.
BenchmarkDecodeDiagnosticsis not onmainand 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:
unwrapWafResultis faster by 44 % to 49 %, in both CPU settings.ContextRunis faster with-test.cpu=1. The upper bound is −0.01 %, so this is close to the line.Runbenchmarks 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: aRunhas about 40 ns less work out of 33 µs, which this host cannot resolve.