settings.parse ran json.Unmarshal on every Load, and one gateway request
called Load six or seven times (Provider.Efforts, ImageOverride,
masking.options, withWires, redactionWanted, recordConversation,
namesMember). parse now keeps the last parsed value keyed by the file's
bytes, and hands Load a deep clone (internal/settings/clone.go), so every
caller still owns what it gets. settings.Shared() returns one read-only
value for the request path, its slices held at len==cap so an append
reallocates instead of reaching the shared array; the seven callers above
moved to it after checking none writes what it reads. A file that doesn't
parse is not memoized: it falls back as before (lastgood), and Shared
then returns that result, never the last good parse, so a failed read
can't keep a looser list. LoadChecked callers (allow list, image fetch
guard, disabled) stay on Load.
Measured with a 200-request probe through the gateway (5 providers, a
~9.9KB settings.json), before (origin/main 2201b6628) -> after:
chat 7.1 parses/request, 570-571 KB/request -> 0.0, 216-242 KB
messages 6.1-6.2 parses, 537-556 KB -> 0.0, 255-261 KB
responses 6.2-6.3 parses, 569-574 KB -> 0.0, 268-277 KB
Tests: TestRequestsParseUnchangedSettingsOnce (gateway, 30 requests must
parse fewer than 30 times), TestUnchangedSettingsParsedOnce,
TestLoadIsTheCallersOwn, TestSharedAppendReallocates,
TestUnreadableSettingsNotTheLastParsed, TestCloneSharesNothing. Broken by
hand, each went red:
- old code: "settings.json parsed 226 times for 30 requests with the
file unchanged"
- memo hit off: "an unchanged settings.json was parsed 200 more times
in 300 reads"
- shallow clone: TestLoadIsTheCallersOwn, TestSharedAppendReallocates,
TestCloneSharesNothing ("Settings.RedactKinds is shared", ...)
- Shared keeping the last parse on a bad file: "Shared kept the last
file's list: [10.0.0.1]"
- no len==cap clone: "appends reached the shared list"
Run at this commit's tree (go1.26.8 darwin/arm64, sandbox HOME):
gofmt -l .: nothing
GOOS=linux go vet -tags nogui ./...: ok
GOOS=windows go vet -tags nogui ./...: ok
go test -tags nogui -race -timeout 45m ./internal/settings
./internal/masking ./internal/provider ./internal/gateway: ok
(settings 388s, provider 836s, gateway 2081s)
set -o pipefail; go test -tags nogui -timeout 45m ./...: ok, every
package (66 ok, 20 with no test files)
110 lines
2.6 KiB
Go
110 lines
2.6 KiB
Go
package settings
|
|
|
|
import "reflect"
|
|
|
|
// clone is a deep copy of s: every map, slice and pointer it holds is its
|
|
// own, so a caller changing what Load gave it changes nothing another
|
|
// caller holds, nor the settings parse keeps for the file's bytes.
|
|
func (s Settings) clone() Settings {
|
|
var out Settings
|
|
deepCopy(reflect.ValueOf(&out).Elem(), reflect.ValueOf(s))
|
|
return out
|
|
}
|
|
|
|
// deepCopy sets dst, a settable value of src's type, to a copy of src
|
|
// sharing no map, slice or pointer with it. A nil map or slice stays nil
|
|
// and an empty one stays empty, as normal and Save tell the two apart.
|
|
func deepCopy(dst, src reflect.Value) {
|
|
switch src.Kind() {
|
|
case reflect.Map:
|
|
if src.IsNil() {
|
|
return
|
|
}
|
|
t := src.Type()
|
|
m := reflect.MakeMapWithSize(t, src.Len())
|
|
// one key and value, set from each entry in turn: SetMapIndex
|
|
// copies them into the map
|
|
k, v := reflect.New(t.Key()).Elem(), reflect.New(t.Elem()).Elem()
|
|
var kc, vc reflect.Value
|
|
if !plain(t.Key()) {
|
|
kc = reflect.New(t.Key()).Elem()
|
|
}
|
|
if !plain(t.Elem()) {
|
|
vc = reflect.New(t.Elem()).Elem()
|
|
}
|
|
it := src.MapRange()
|
|
for it.Next() {
|
|
k.SetIterKey(it)
|
|
v.SetIterValue(it)
|
|
key, val := k, v
|
|
if kc.IsValid() {
|
|
kc.SetZero()
|
|
deepCopy(kc, k)
|
|
key = kc
|
|
}
|
|
if vc.IsValid() {
|
|
vc.SetZero()
|
|
deepCopy(vc, v)
|
|
val = vc
|
|
}
|
|
m.SetMapIndex(key, val)
|
|
}
|
|
dst.Set(m)
|
|
case reflect.Slice:
|
|
if src.IsNil() {
|
|
return
|
|
}
|
|
n := reflect.MakeSlice(src.Type(), src.Len(), src.Len())
|
|
if plain(src.Type().Elem()) {
|
|
reflect.Copy(n, src)
|
|
} else {
|
|
for i := range src.Len() {
|
|
deepCopy(n.Index(i), src.Index(i))
|
|
}
|
|
}
|
|
dst.Set(n)
|
|
case reflect.Pointer:
|
|
if src.IsNil() {
|
|
return
|
|
}
|
|
p := reflect.New(src.Type().Elem())
|
|
deepCopy(p.Elem(), src.Elem())
|
|
dst.Set(p)
|
|
case reflect.Interface:
|
|
if src.IsNil() {
|
|
return
|
|
}
|
|
v := reflect.New(src.Elem().Type()).Elem()
|
|
deepCopy(v, src.Elem())
|
|
dst.Set(v)
|
|
case reflect.Struct:
|
|
if plain(src.Type()) {
|
|
dst.Set(src)
|
|
return
|
|
}
|
|
for i := range src.NumField() {
|
|
deepCopy(dst.Field(i), src.Field(i))
|
|
}
|
|
default:
|
|
dst.Set(src)
|
|
}
|
|
}
|
|
|
|
// plain is whether a value of t holds nothing shared: no map, slice,
|
|
// pointer or interface anywhere in it, so a plain assignment copies it.
|
|
func plain(t reflect.Type) bool {
|
|
switch t.Kind() {
|
|
case reflect.Map, reflect.Slice, reflect.Pointer, reflect.Interface, reflect.Chan, reflect.Func, reflect.UnsafePointer:
|
|
return false
|
|
case reflect.Array:
|
|
return plain(t.Elem())
|
|
case reflect.Struct:
|
|
for i := range t.NumField() {
|
|
if !plain(t.Field(i).Type) {
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
}
|