refactor: split decodeDiagnostics and decodeFeature #246
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 · 644 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.387
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #246"
general
weight: 0.35
eliottness
Why
maatflagsdecodeDiagnostics(CC 17) anddecodeFeature(CC 12) indecoder.go. This is item #4 of the DD-29 assessment. This PR splits them. The highest CC goes from 17 to 12 (decodeDiagnosticsEntry), and every other function has CC ≤ 10. The decoded output, the error texts and the allocations do not change.What changed
Commit 1
test: pin decoder behavior before the refactoradds tests that pass on the olddecoder.go, and they pass unchanged on the new one:decoder_test.go(new): 2 golden tests (every key goes to the right field, unknown keys are ignored), 2 error tables with exact error texts (14 + 9 cases), andBenchmarkDecodeDiagnostics. Two cases need a map with size 1 and a nil pointer. The public setters cannot build it, so the test writes the size field withunsafe(offset 2 of the Cddwaf_object). It passes under-race.decoder_unknown_field_test.go: comment fix only. The old text said thatdecodeFeaturereturns an error on an unknown field. It does not.Commit 2
refactor: split decodeDiagnostics and decodeFeaturechangesdecoder.goonly. Commit 3refactor: use one switch for the diagnostics keysmerges the 2 diagnosticsswitchhelpers into 1 (review feedback). Result:| Function | CC | Role |
|---|---|---|
|
mapEntries(new) | 3 | SharedIsMap+MapEntriesguard, same error texts ||
decodeDiagnostics| 5 (was 17) | Loop, key decode, error wrap ||
decodeDiagnosticsEntry(new) | 12 | Oneswitchon the 10 feature keys +ruleset_version, unknown keys ignored ||
decodeFeature| 2 (was 12) |mapEntries, thendecodeFeatureEntries||
decodeFeatureEntries(new) | 10 | The old loop and 6-caseswitch, unchanged |No exported symbol changes. No new type, package variable or function value.
Why a
switchand not a tableA first design used a
mapof setters and one helper for each feature field. Measured on this benchmark, it made the decode about 15 % slower. Themaplookup and indirect call cost most of it (+14.5 %). One extra call for each of the 50 feature fields cost +2.2 %. This is why the code usesswitchstatements with direct calls only, and a loop by index. Please do not add a table or a per-field helper without a new benchmark.Performance
BenchmarkDecodeDiagnostics,-count=10,-benchmem. The "before" and "after" test binaries ran one after the other, in turns, so machine load affects both in the same way.Final head (3 commits) vs
main's decoder (the commit 1 binary):Commit 3 (1
switch) vs commit 2 (2switchhelpers):Before commit 3, commit 2 vs
main's decoder also had no significant difference in 2 full runs (sec/op~, p=0.393 and p=0.529).No significant difference in sec/op, B/op or allocs/op.
go build -gcflags=-m .: the onlymoved to heapline indecoder.goisfeature(decoder.go:129). It is the same as onmain.maatmaat analyze ./... --detail full --output json,main→ this PR:| Scope | Before | After |
|---|---|---|
| Overall | 90 (
base_score89.54) | 90 (base_score89.80) || Root module
github.com/DataDog/go-libddwaf/v5| 79 (81.65) | 80 (82.19) ||
decoder.go| 87 | 98 ||
decoder_test.go| – | 100, 0 hits |The 8 in-scope rule hits of
mainare gone:decodeDiagnostics(line 49):MAAT-CC-001(CC 17),MAAT-FUNC-002(CC 17),MAAT-FUNC-003(38 SLOC),MAAT-SLOC-001(38 SLOC),MAAT-MI-001(MI 42.8, error).decodeFeature(line 100):MAAT-CC-001(CC 12),MAAT-FUNC-002(CC 12),MAAT-MI-001(MI 47.4, error).4 warnings remain in
decoder.go(9 hits → 4, and no error). 3 are in scope, and 1 is out of scope and does not change:MAAT-CC-001warning ondecodeDiagnosticsEntry(CC 12, threshold 10), andMAAT-MI-001warning on the same function (MI 50.4, threshold 53). They come from the singleswitchwith 11 cases. This is a choice made after review: 1switchis easier to read than 2.decodeDiagnosticsstays at CC 5.MAAT-MI-001warning ondecodeFeatureEntries(MI 50.1). The olddecodeFeaturehad an error here (MI 47.4). To remove the hit, theswitchmust leave the loop, which needs one call for each field (+2.6 % to +6.4 % time, measured). Romain chose to keep the hit and have no time regression.MAAT-MI-001warning ondecodeStringArray(MI 52.4). It is out of scope for DD-33 and does not change.Notes
golangci-lint run ./...: 0 issues.WAFObject.StringValuemakes 66 % of the allocated objects, and it needs a change outsidedecoder.go.