Avoid copying unused slice capacity
This commit is contained in:
@@ -154,8 +154,11 @@ options, schema guarantees, or additional runtime validation.
|
|||||||
|
|
||||||
## 4. P3 — Allocate Trusted Slice Copies By Length, Not Spare Capacity
|
## 4. P3 — Allocate Trusted Slice Copies By Length, Not Spare Capacity
|
||||||
|
|
||||||
Status: open; efficiency improvement for consideration. Identified by the
|
Status: complete. Identified by the algorithmic audit. Relevant stages: 1 and 6.
|
||||||
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`.
|
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
|
- The separate public-API reproduction above confirmed the ownership bug not
|
||||||
covered by the passing suite.
|
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
|
### 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 additional ordinary correctness issue was found beyond the recorded findings.
|
||||||
No real-world performance data, timing measurements, or profiling were collected;
|
No real-world performance data, timing measurements, or profiling were collected;
|
||||||
the earlier successful suite/vet results apply to unchanged implementation code.
|
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`.
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ func cloneTrustedReflect(value reflect.Value, active map[cloneReference]reflect.
|
|||||||
case reflect.Map:
|
case reflect.Map:
|
||||||
out = reflect.MakeMapWithSize(value.Type(), value.Len())
|
out = reflect.MakeMapWithSize(value.Type(), value.Len())
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
out = reflect.MakeSlice(value.Type(), value.Len(), value.Cap())
|
out = reflect.MakeSlice(value.Type(), value.Len(), value.Len())
|
||||||
}
|
}
|
||||||
active[key] = out
|
active[key] = out
|
||||||
defer delete(active, key)
|
defer delete(active, key)
|
||||||
|
|||||||
@@ -8,6 +8,28 @@ import (
|
|||||||
|
|
||||||
type trustedNamed string
|
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) {
|
func TestCloneTrustedPreservesTypesAndDetaches(t *testing.T) {
|
||||||
named := trustedNamed("value")
|
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{}}
|
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{}}
|
||||||
|
|||||||
Reference in New Issue
Block a user