Avoid copying unused slice capacity

This commit is contained in:
2026-11-20 16:22:09 +00:00
parent eae4d18a9c
commit bd6b2848bb
3 changed files with 38 additions and 4 deletions

View File

@@ -154,8 +154,11 @@ options, schema guarantees, or additional runtime validation.
## 4. P3 — Allocate Trusted Slice Copies By Length, Not Spare Capacity
Status: open; efficiency improvement for consideration. Identified by the
algorithmic audit. Relevant stages: 1 and 6.
Status: complete. Identified by the algorithmic audit. Relevant stages: 1 and 6.
Resolution: trusted slice copies allocate backing storage from the populated
length. Internal regressions cover interface, string, byte, named, and nonnil
empty slices with spare capacity; existing nil/empty and ownership tests remain.
Location: `internal/jsonvalue/trusted.go:50` in `cloneTrustedReflect`.
@@ -217,7 +220,8 @@ On the reviewed commit, using Go 1.27.4 with `GOTOOLCHAIN=local` and `GOWORK=off
- The separate public-API reproduction above confirmed the ownership bug not
covered by the passing suite.
This review changes no implementation code.
The review itself changed no implementation code. Completed remediations are
recorded under the findings above.
### Follow-Up Integration Audit
@@ -253,3 +257,11 @@ checks and compiler behavior, and existing hash algorithms were not reopened.
No additional ordinary correctness issue was found beyond the recorded findings.
No real-world performance data, timing measurements, or profiling were collected;
the earlier successful suite/vet results apply to unchanged implementation code.
## Remediation Validation
All four findings are complete. Focused ownership, schema, requirements,
selection, and smoke tests passed during the corresponding fixes. The assembled
remediations passed `go test -race ./...` (including maintained examples),
`go vet ./...`, and `git diff --check`, using Go 1.27.4 with `GOTOOLCHAIN=local`
and `GOWORK=off`.

View File

@@ -47,7 +47,7 @@ func cloneTrustedReflect(value reflect.Value, active map[cloneReference]reflect.
case reflect.Map:
out = reflect.MakeMapWithSize(value.Type(), value.Len())
case reflect.Slice:
out = reflect.MakeSlice(value.Type(), value.Len(), value.Cap())
out = reflect.MakeSlice(value.Type(), value.Len(), value.Len())
}
active[key] = out
defer delete(active, key)

View File

@@ -8,6 +8,28 @@ import (
type trustedNamed string
func TestCloneTrustedDoesNotCopySpareCapacity(t *testing.T) {
type namedSlice []string
for name, original := range map[string]any{
"any": make([]any, 1, 64),
"empty": make([]any, 0, 64),
"strings": make([]string, 1, 64),
"bytes": make([]byte, 1, 64),
"named": make(namedSlice, 1, 64),
} {
t.Run(name, func(t *testing.T) {
copied := CloneTrusted(original)
if !reflect.DeepEqual(copied, original) {
t.Fatalf("copy changed slice contents or type: %#v", copied)
}
value := reflect.ValueOf(copied)
if value.Cap() != value.Len() {
t.Fatalf("copy reserves unused storage: len=%d cap=%d", value.Len(), value.Cap())
}
})
}
}
func TestCloneTrustedPreservesTypesAndDetaches(t *testing.T) {
named := trustedNamed("value")
original := map[string]any{"": map[string]any{"number": json.Number("9007199254740993")}, "typed": []string{"original"}, "pointer": &named, "nilmap": map[string]any(nil), "nilslice": []string(nil), "empty": []any{}}