Files
CLIProxyAPI/internal/util/sjson_delete.go
JohnsonRan 6b56ce6c28 perf(thinking): skip document copies when deleting absent fields
sjson.DeleteBytes copies the whole document even when the path is
missing: appendRawPaths builds a prefix copy that is discarded, then
set() copies the input again before SetBytes returns the original
slice. The summary config and the Gemini/Antigravity level and budget
appliers defensively delete several alias spellings that are almost
always absent, so each request paid several full-body copies.

Add util.DeleteJSONBytesIfExists, which checks the path with
GetGJSONBytesNoCopy first, and use it at those call sites. Output is
unchanged.

On a 1 MiB Antigravity body, applySummaryConfigForProvider drops from
7.4 MB to 1.1 MB allocated per call. In a production heap profile these
deletes were 35 GB of 192 GB allocated over 72 minutes.
2026-10-25 03:42:05 +09:00

22 lines
913 B
Go

package util
import (
"github.com/tidwall/sjson"
)
// DeleteJSONBytesIfExists behaves like sjson.DeleteBytes but returns data
// unchanged when path is absent. sjson.DeleteBytes copies the document at
// least once even when there is nothing to delete (a partial prefix copy is
// built and discarded, then the whole input is copied again before sjson
// returns the original slice), which dominates allocation when callers
// defensively delete alias spellings that are usually missing. path must be a
// nonempty literal dotted path (gjson-compatible escapes like \. are
// supported); sjson's "-1" last-element delete and ":" forced-object-key syntax
// are not supported here. Behavior otherwise matches sjson.DeleteBytes.
func DeleteJSONBytesIfExists(data []byte, path string) ([]byte, error) {
if !GetGJSONBytesNoCopy(data, path).Exists() {
return data, nil
}
return sjson.DeleteBytes(data, path)
}