When optimizing performance, avoid whole-object work and unbounded resource growth. Apply these standards:
1) Targeted extraction/rewrites (no full parsing)
Example (safe “model” rewrite):
func rewriteTopLevelModel(body []byte, alias, modelID string) []byte {
trimmed := bytes.TrimLeft(body, " \t\r\n")
if len(trimmed) == 0 || trimmed[0] != '{' { // multipart/form-data etc.
return body
}
cur := gjson.GetBytes(body, "model")
if cur.Type != gjson.String || !strings.EqualFold(cur.Str, alias) {
return body
}
out, err := sjson.SetBytes(body, "model", modelID)
if err != nil { return body }
return out
}
2) Bound buffering and “perpetual due” logic
3) Coalesce duplicate concurrent work
4) Pooling hygiene (prevent allocation retention)
These practices together reduce CPU spent on unnecessary parsing/scanning, prevent memory blowups, and eliminate wasted concurrent work—directly improving throughput and latency.
Enter the URL of a public GitHub repository