refactor(encoder): reduce complexity of encode, depthOf and the scalar fast path (maat #2) #247
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 #247"
general
weight: 0.35
eliottness
Summary
encoder.gohad one critical CC hit:encodehad a cyclomatic complexity (CC) of 30.depthOf,tryEncodeScalarFastPathandEncodealso had hits. This PR splits these functions. The encoding output, the truncation records, the number of timer checks and the public API do not change.encoder.gois the only production file that changes.Part of DD-29 (
maatassessment), item 2.| Commit | Change |
|---|---|
| 1 | Lock tests for the evaluation order, the depth re-measure, the timer checks and
depthOf, plusBenchmarkEncoderPaths. No production change. The tests pass on the base code. || 2 |
encodechooses betweenencodeContainerValueandencodeLeafValuewith one inlined kind test. The nil and byte checks are inline expressions, andisValueNilis removed. A container level, and a string, bool or nil leaf, get no added call. A number leaf gets one (encodeNumberValue). || 3 |
tryEncodeScalarFastPathgroups the 12 number cases in 3 case clauses, with 3 small inlined converters. || 4 |
depthOfstays one function. Each loop stops at the first error witherr == nilin its condition. || 5 | The depth re-measure of
Encodemoves toremeasureObjectTooDeep. The typed or reflect choice moves toencodeTypedOrReflect. |Why
depthOfis not split furtherA first version of this change split
encodeanddepthOfinto one helper for each step and each container kind. It was measured:BenchmarkEncoderPaths/remeasure/deepwas about 20% slower (confirmed in 2 stages and in the pooled data). The extra calls on the container path were the cause. This version adds no call on the container path ("no added call on the container path";encodeContainerValuestays on the call stack during the recursion, so the stack use is not the same as before).depthOfkeeps 3 loops in one function. The low-cost designs that were tested did not reach a cognitive complexity of 10 or less ondepthOf, sodepthOfkeeps 4maathits (see below).Complexity, before and after
CC from
gocyclo, cognitive complexity fromgocognit(same values asmaat).| Function | CC | Cognitive |
|---|---|---|
|
encode| 30 → 9 | 15 → 9 ||
tryEncodeScalarFastPath| 17 → 8 | 1 → 1 ||
depthOf| 13 → 13 | 23 → 17 ||
Encode| 10 → 2 | 11 → 1 || new
encodeContainerValue/encodeLeafValue| 9 / 9 | 7 / 2 || new
unsupportedLeafValue,encodeNumberValue,isInvalidValue,isContainerKind| 2 / 3 / 2 / 2 | 1 || new
signedScalarAsInt64/unsignedScalarAsUint64/floatScalarAsFloat64| 6 / 6 / 3 | 1 || new
encodeTypedOrReflect/remeasureObjectTooDeep| 3 / 3 | 2 / 2 |maatScore 90 → 91.
encoder.gofile score 28 → 66. Test file 100, no hit.Removed hits (17):
Added hit: 1.
depthOfMAAT-CC-002 is now a warning (cognitive 17), where it was an error (23).Hits that stay on
depthOf: MAAT-CC-001 warning (CC 13), MAAT-FUNC-002 warning (CC 13), MAAT-CC-002 warning (cognitive 17), MAAT-MI-001 error (MI 47; it was 44). The file-size hits (MAAT-MOD-003, MAAT-SLOC-002) and the hits on the typed fast paths,getFieldNameFromTypeandencodeStructare out of scope.Mutation check of
depthOf(local, not committed)Each edit fails the named subtests of
TestDepthOfBranches:| # | Edit | Failing subtests |
|---|---|---|
| 1 | array loop: error check before
max|D11_array_loop_keeps_the_depth_of_the_failing_child|| 2 | map loop: error check before
max|D9_map_loop_keeps_the_depth_of_the_failing_child,D12_map_loop_stops_at_the_first_error|| 3 | struct loop: error check before
max|D10_struct_loop_keeps_the_depth_of_the_failing_child|| 4 | array loop: remove
err == nil &&|D8_array_loop_stops_at_the_first_error|| 5 | map loop: remove
err == nil &&|D12_map_loop_stops_at_the_first_error|| 6 | struct loop: remove
err == nil &&|D10_struct_loop_keeps_the_depth_of_the_failing_child|The failure lines:
Inlining
go build -gcflags='-m=2', commit 1 against commit 5 (functions ofencoder.gothat the compiler can inline).comm -23 before after(lost): empty.comm -13 before after(gained):Benchmarks
Verdict: PASS, meaning no confirmed regression on the tested fixtures. This is not a claim that the performance is the same. The error rates of the protocol (about 4% false FAIL, about 7.5% false "A/A not valid") are estimates under noise assumptions, not guarantees.
Protocol: 3 binaries (
before= commit 1,before2= a copy ofbefore,after= commit 5) run in rotated order for 21 rounds in each of 2 stages. Thebefore2column is the A/A comparison. A noisy row is confirmed when it is flagged in stage 1 and in stage 2 with the same sign, or when it is flagged in the pooled data (alpha 0.002, 42 samples for each side).| Check | Result |
|---|---|
| A/A valid | Yes: no noisy row is confirmed in the
before2column.long-strings/16384is flagged once (+2.00%, stage 2 only). || Exact rows (allocs/op of all rows, B/op of the 11
BenchmarkEncoderPathsrows) | All equal, inbefore2and inafter, in the 6 tables || Noisy rows,
aftercolumn | No increase flagged in any table. Flagged decreases:SubcontextRunOnly(-0.86%, stage 2) andtyped/request(-3.18%, stage 2). ||
remeasure/deep|~in the 3 tables (pooled median 1.555 µs before, 1.564 µs after). The first version had +20% here. |Per-commit screening (16 interleaved rounds, each commit against commit 1): commits 2, 3 and 5 had no increase flagged. Commit 4 had 2 flagged rows in the first set (
scalar/float64+8.12%,scalar/string+3.46%). The scalar path does not rundepthOf. The second set (16 new rounds) flagged no row, so the commit passed.Load averages (
uptime, 1, 5 and 15 minutes):BenchmarkEncoderB/op medians (pooled, diagnostic only; the benchmark shares onePinner, so its B/op varies for the same code):| Row | before | before2 | after |
|---|---|---|---|
| 1024 | 8261.5 | 8287 | 8361.5 |
| 4096 | 8651 | 8804.5 | 8828.5 |
| 8192 | 8725.5 | 8801 | 8750 |
| 16384 | 7321.5 | 7330 | 7329 |
The 6 benchstat tables (ns/op section; the B/op and allocs/op sections and all raw files are attached on DD-32):
gate, stage 1 (sec/op)
gate, stage 2 (sec/op)
gate, pooled (alpha 0.002) (sec/op)
paths, stage 1 (sec/op)
paths, stage 2 (sec/op)
paths, pooled (alpha 0.002) (sec/op)