← Back to Pull requests

refactor(encoder): reduce complexity of encode, depthOf and the scalar fast path (maat #2) #247

Closed
unknown closed this pull request without merging on Nov 18, 2026 14:43 UTC
View on GitHub
unknown unknown commented on Nov 16, 2026 10:00 UTC
Author

Summary

encoder.go had one critical CC hit: encode had a cyclomatic complexity (CC) of 30. depthOf, tryEncodeScalarFastPath and Encode also 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.go is the only production file that changes.

Part of DD-29 (maat assessment), item 2.

| Commit | Change |
|---|---|
| 1 | Lock tests for the evaluation order, the depth re-measure, the timer checks and depthOf, plus BenchmarkEncoderPaths. No production change. The tests pass on the base code. |
| 2 | encode chooses between encodeContainerValue and encodeLeafValue with one inlined kind test. The nil and byte checks are inline expressions, and isValueNil is removed. A container level, and a string, bool or nil leaf, get no added call. A number leaf gets one (encodeNumberValue). |
| 3 | tryEncodeScalarFastPath groups the 12 number cases in 3 case clauses, with 3 small inlined converters. |
| 4 | depthOf stays one function. Each loop stops at the first error with err == nil in its condition. |
| 5 | The depth re-measure of Encode moves to remeasureObjectTooDeep. The typed or reflect choice moves to encodeTypedOrReflect. |

Why depthOf is not split further


A first version of this change split encode and depthOf into one helper for each step and each container kind. It was measured: BenchmarkEncoderPaths/remeasure/deep was 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"; encodeContainerValue stays on the call stack during the recursion, so the stack use is not the same as before). depthOf keeps 3 loops in one function. The low-cost designs that were tested did not reach a cognitive complexity of 10 or less on depthOf, so depthOf keeps 4 maat hits (see below).

Complexity, before and after

CC from gocyclo, cognitive complexity from gocognit (same values as maat).

| 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 |

maat

Score 90 → 91. encoder.go file score 28 → 66. Test file 100, no hit.

Removed hits (17):

encode: MAAT-CC-001 (critical), MAAT-FUNC-002 (critical), MAAT-IMPL-003 (critical), MAAT-FUNC-003 (error), MAAT-FUNC-006 (error), MAAT-MI-001 (error), MAAT-CC-002 (warning)
tryEncodeScalarFastPath: MAAT-CC-001 (error), MAAT-MI-001 (error), MAAT-FUNC-003 (warning)
Encode: MAAT-CC-002, MAAT-HALSTEAD-002, MAAT-MI-001 (warnings)
depthOf: MAAT-CC-002 (error, cognitive 23), MAAT-FUNC-003, MAAT-FUNC-006, MAAT-SLOC-001 (warnings)

Added hit: 1. depthOf MAAT-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, getFieldNameFromType and encodeStruct are 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:

    --- FAIL: TestDepthOfBranches/D11_array_loop_keeps_the_depth_of_the_failing_child (0.00s)
    --- FAIL: TestDepthOfBranches/D9_map_loop_keeps_the_depth_of_the_failing_child (0.00s)
    --- FAIL: TestDepthOfBranches/D12_map_loop_stops_at_the_first_error (0.00s)
    --- FAIL: TestDepthOfBranches/D10_struct_loop_keeps_the_depth_of_the_failing_child (0.00s)
    --- FAIL: TestDepthOfBranches/D8_array_loop_stops_at_the_first_error (0.00s)
    --- FAIL: TestDepthOfBranches/D12_map_loop_stops_at_the_first_error (0.00s)
    --- FAIL: TestDepthOfBranches/D10_struct_loop_keeps_the_depth_of_the_failing_child (0.00s)

Inlining

go build -gcflags='-m=2', commit 1 against commit 5 (functions of encoder.go that the compiler can inline).

comm -23 before after (lost): empty.
comm -13 before after (gained):

(*encoder).unsupportedLeafValue
floatScalarAsFloat64
isContainerKind
isInvalidValue
signedScalarAsInt64
unsignedScalarAsUint64

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 of before, after = commit 5) run in rotated order for 21 rounds in each of 2 stages. The before2 column 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 before2 column. long-strings/16384 is flagged once (+2.00%, stage 2 only). |
| Exact rows (allocs/op of all rows, B/op of the 11 BenchmarkEncoderPaths rows) | All equal, in before2 and in after, in the 6 tables |
| Noisy rows, after column | No increase flagged in any table. Flagged decreases: SubcontextRunOnly (-0.86%, stage 2) and typed/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 run depthOf. The second set (16 new rounds) flagged no row, so the commit passed.

Load averages (uptime, 1, 5 and 15 minutes):

stage 1 start: 10:24 load averages: 6.02 6.42 8.55
stage 1 end:   10:42 load averages: 5.28 7.63 10.80
stage 2 start: 10:42 load averages: 5.18 7.57 10.76
stage 2 end:   10:59 load averages: 6.38 6.21 7.60

BenchmarkEncoder B/op medians (pooled, diagnostic only; the benchmark shares one Pinner, 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)

                     │   before    │               before2               │               after                │
                     │   sec/op    │    sec/op     vs base               │   sec/op     vs base               │
RunOnly-18             32.00µ ± 1%   32.23µ ±  2%       ~ (p=0.732 n=21)   32.16µ ± 2%       ~ (p=0.881 n=21)
SubcontextRunOnly-18   13.00µ ± 1%   13.06µ ±  2%       ~ (p=0.280 n=21)   13.05µ ± 3%       ~ (p=0.654 n=21)
ContextRun-18          1.747µ ± 1%   1.741µ ±  4%       ~ (p=0.799 n=21)   1.747µ ± 2%       ~ (p=0.906 n=21)
SubcontextRun-18       1.901µ ± 2%   1.899µ ±  3%       ~ (p=0.808 n=21)   1.908µ ± 3%       ~ (p=0.481 n=21)
Encoder/1024-18        6.248µ ± 4%   6.357µ ±  3%       ~ (p=0.601 n=21)   6.379µ ± 8%       ~ (p=0.060 n=21)
Encoder/4096-18        6.602µ ± 4%   6.393µ ±  8%       ~ (p=0.857 n=21)   6.535µ ± 5%       ~ (p=0.760 n=21)
Encoder/8192-18        6.879µ ± 5%   6.655µ ± 24%       ~ (p=0.184 n=21)   6.754µ ± 7%       ~ (p=0.803 n=21)
Encoder/16384-18       4.804µ ± 3%   4.839µ ±  3%       ~ (p=0.946 n=21)   4.763µ ± 2%       ~ (p=0.343 n=21)
geomean                6.087µ        6.061µ        -0.42%                  6.083µ       -0.06%

gate, stage 2 (sec/op)

                     │    before    │               before2               │               after                │
                     │    sec/op    │    sec/op     vs base               │   sec/op     vs base               │
RunOnly-18             32.04µ ±  1%   31.95µ ±  1%       ~ (p=0.672 n=21)   31.80µ ± 1%       ~ (p=0.104 n=21)
SubcontextRunOnly-18   13.10µ ±  1%   13.07µ ±  1%       ~ (p=0.803 n=21)   12.99µ ± 1%  -0.86% (p=0.017 n=21)
ContextRun-18          1.756µ ±  1%   1.744µ ±  1%       ~ (p=0.247 n=21)   1.744µ ± 1%       ~ (p=0.407 n=21)
SubcontextRun-18       1.881µ ±  1%   1.873µ ±  2%       ~ (p=0.529 n=21)   1.875µ ± 2%       ~ (p=0.828 n=21)
Encoder/1024-18        6.057µ ±  3%   6.086µ ±  2%       ~ (p=0.789 n=21)   6.100µ ± 2%       ~ (p=0.571 n=21)
Encoder/4096-18        6.175µ ±  5%   6.358µ ±  4%       ~ (p=0.636 n=21)   6.408µ ± 4%       ~ (p=0.263 n=21)
Encoder/8192-18        6.355µ ± 19%   6.410µ ± 22%       ~ (p=0.439 n=21)   6.556µ ± 5%       ~ (p=0.369 n=21)
Encoder/16384-18       4.701µ ± 59%   4.561µ ±  7%       ~ (p=0.220 n=21)   4.580µ ± 1%       ~ (p=0.067 n=21)
geomean                5.940µ         5.937µ        -0.04%                  5.957µ       +0.29%

gate, pooled (alpha 0.002) (sec/op)

                     │   before    │               before2               │               after                │
                     │   sec/op    │    sec/op     vs base               │   sec/op     vs base               │
RunOnly-18             32.02µ ± 1%   31.97µ ±  1%       ~ (p=0.979 n=42)   31.89µ ± 1%       ~ (p=0.283 n=42)
SubcontextRunOnly-18   13.04µ ± 0%   13.07µ ±  1%       ~ (p=0.474 n=42)   13.00µ ± 1%       ~ (p=0.325 n=42)
ContextRun-18          1.749µ ± 1%   1.743µ ±  1%       ~ (p=0.543 n=42)   1.746µ ± 1%       ~ (p=0.677 n=42)
SubcontextRun-18       1.885µ ± 1%   1.877µ ±  1%       ~ (p=0.844 n=42)   1.890µ ± 1%       ~ (p=0.813 n=42)
Encoder/1024-18        6.191µ ± 2%   6.184µ ±  2%       ~ (p=0.802 n=42)   6.236µ ± 2%       ~ (p=0.212 n=42)
Encoder/4096-18        6.393µ ± 3%   6.367µ ±  3%       ~ (p=0.950 n=42)   6.451µ ± 2%       ~ (p=0.707 n=42)
Encoder/8192-18        6.573µ ± 3%   6.432µ ± 14%       ~ (p=0.603 n=42)   6.677µ ± 2%       ~ (p=0.462 n=42)
Encoder/16384-18       4.757µ ± 3%   4.752µ ±  3%       ~ (p=0.350 n=42)   4.665µ ± 2%       ~ (p=0.028 n=42)
geomean                6.010µ        5.984µ        -0.43%                  6.015µ       +0.08%

paths, stage 1 (sec/op)

                                │   before    │              before2               │               after                │
                                │   sec/op    │   sec/op     vs base               │   sec/op     vs base               │
EncoderPaths/scalar/int64         39.86n ± 3%   39.75n ± 4%       ~ (p=0.789 n=21)   39.54n ± 2%       ~ (p=0.285 n=21)
EncoderPaths/scalar/float64       39.41n ± 5%   40.10n ± 5%       ~ (p=0.906 n=21)   39.22n ± 5%       ~ (p=0.760 n=21)
EncoderPaths/scalar/string        40.89n ± 4%   42.33n ± 4%       ~ (p=0.294 n=21)   41.08n ± 8%       ~ (p=0.497 n=21)
EncoderPaths/reflect/strings      5.444µ ± 6%   5.525µ ± 4%       ~ (p=0.521 n=21)   5.438µ ± 3%       ~ (p=0.741 n=21)
EncoderPaths/reflect/structs      14.19µ ± 3%   14.67µ ± 5%       ~ (p=0.132 n=21)   14.21µ ± 4%       ~ (p=0.425 n=21)
EncoderPaths/typed/request        1.932µ ± 4%   1.954µ ± 4%       ~ (p=0.605 n=21)   1.911µ ± 2%       ~ (p=0.131 n=21)
EncoderPaths/remeasure/deep       1.571µ ± 2%   1.579µ ± 2%       ~ (p=0.554 n=21)   1.567µ ± 2%       ~ (p=0.649 n=21)
EncoderPaths/long-strings/1024    7.224µ ± 3%   7.263µ ± 5%       ~ (p=0.985 n=21)   7.238µ ± 6%       ~ (p=0.921 n=21)
EncoderPaths/long-strings/4096    7.220µ ± 7%   7.445µ ± 4%       ~ (p=0.240 n=21)   7.340µ ± 3%       ~ (p=0.369 n=21)
EncoderPaths/long-strings/8192    7.285µ ± 3%   7.495µ ± 5%       ~ (p=0.052 n=21)   7.331µ ± 3%       ~ (p=0.343 n=21)
EncoderPaths/long-strings/16384   7.326µ ± 5%   7.334µ ± 7%       ~ (p=0.709 n=21)   7.410µ ± 5%       ~ (p=0.330 n=21)
geomean                           1.404µ        1.428µ       +1.64%                  1.406µ       +0.13%

paths, stage 2 (sec/op)

                                │   before    │              before2               │               after                │
                                │   sec/op    │   sec/op     vs base               │   sec/op     vs base               │
EncoderPaths/scalar/int64         39.30n ± 2%   39.11n ± 4%       ~ (p=0.896 n=21)   39.18n ± 1%       ~ (p=0.658 n=21)
EncoderPaths/scalar/float64       38.52n ± 2%   38.44n ± 8%       ~ (p=0.965 n=21)   38.75n ± 1%       ~ (p=0.847 n=21)
EncoderPaths/scalar/string        40.72n ± 2%   40.82n ± 4%       ~ (p=0.529 n=21)   40.37n ± 1%       ~ (p=0.333 n=21)
EncoderPaths/reflect/strings      5.311µ ± 2%   5.269µ ± 4%       ~ (p=0.579 n=21)   5.305µ ± 2%       ~ (p=0.823 n=21)
EncoderPaths/reflect/structs      13.84µ ± 3%   14.04µ ± 2%       ~ (p=0.915 n=21)   13.83µ ± 2%       ~ (p=0.407 n=21)
EncoderPaths/typed/request        1.917µ ± 3%   1.905µ ± 3%       ~ (p=0.876 n=21)   1.856µ ± 2%  -3.18% (p=0.005 n=21)
EncoderPaths/remeasure/deep       1.548µ ± 2%   1.545µ ± 4%       ~ (p=0.546 n=21)   1.550µ ± 1%       ~ (p=0.808 n=21)
EncoderPaths/long-strings/1024    7.219µ ± 2%   7.189µ ± 3%       ~ (p=0.867 n=21)   7.118µ ± 2%       ~ (p=0.153 n=21)
EncoderPaths/long-strings/4096    7.114µ ± 3%   7.191µ ± 2%       ~ (p=0.672 n=21)   7.074µ ± 1%       ~ (p=0.132 n=21)
EncoderPaths/long-strings/8192    7.112µ ± 2%   7.231µ ± 2%       ~ (p=0.141 n=21)   7.127µ ± 2%       ~ (p=0.842 n=21)
EncoderPaths/long-strings/16384   7.183µ ± 2%   7.327µ ± 4%  +2.00% (p=0.019 n=21)   7.232µ ± 2%       ~ (p=0.517 n=21)
geomean                           1.383µ        1.387µ       +0.33%                  1.376µ       -0.45%

paths, pooled (alpha 0.002) (sec/op)

                                │   before    │              before2               │               after                │
                                │   sec/op    │   sec/op     vs base               │   sec/op     vs base               │
EncoderPaths/scalar/int64         39.49n ± 2%   39.39n ± 3%       ~ (p=0.943 n=42)   39.33n ± 1%       ~ (p=0.238 n=42)
EncoderPaths/scalar/float64       38.91n ± 1%   39.11n ± 4%       ~ (p=0.964 n=42)   38.92n ± 1%       ~ (p=0.788 n=42)
EncoderPaths/scalar/string        40.73n ± 2%   41.18n ± 3%       ~ (p=0.202 n=42)   40.62n ± 1%       ~ (p=0.925 n=42)
EncoderPaths/reflect/strings      5.350µ ± 2%   5.409µ ± 2%       ~ (p=0.854 n=42)   5.338µ ± 2%       ~ (p=0.954 n=42)
EncoderPaths/reflect/structs      14.07µ ± 2%   14.29µ ± 2%       ~ (p=0.341 n=42)   13.95µ ± 2%       ~ (p=0.190 n=42)
EncoderPaths/typed/request        1.923µ ± 1%   1.933µ ± 2%       ~ (p=0.642 n=42)   1.874µ ± 2%       ~ (p=0.003 n=42)
EncoderPaths/remeasure/deep       1.555µ ± 2%   1.562µ ± 1%       ~ (p=0.820 n=42)   1.564µ ± 1%       ~ (p=0.851 n=42)
EncoderPaths/long-strings/1024    7.221µ ± 1%   7.245µ ± 1%       ~ (p=0.840 n=42)   7.185µ ± 1%       ~ (p=0.383 n=42)
EncoderPaths/long-strings/4096    7.167µ ± 2%   7.231µ ± 3%       ~ (p=0.152 n=42)   7.170µ ± 2%       ~ (p=1.000 n=42)
EncoderPaths/long-strings/8192    7.167µ ± 2%   7.254µ ± 3%       ~ (p=0.024 n=42)   7.207µ ± 2%       ~ (p=0.546 n=42)
EncoderPaths/long-strings/16384   7.203µ ± 2%   7.331µ ± 2%       ~ (p=0.058 n=42)   7.264µ ± 2%       ~ (p=0.312 n=42)
geomean                           1.391µ        1.402µ       +0.84%                  1.387µ       -0.25%

Diff: 650 lines (+487 / -163) across 8 files Reviewer: eliottness Review duration: 3163m
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 #247"
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%