perf(common): compile LLM-JSON code-fence regexp once
ParseLLMJsonResponse recompiled its ```json fence-extraction regexp on every call that fell through the direct-unmarshal fast path — i.e. every fenced LLM response on the graph-extraction path. Hoist it to a package-level var. Adds a test (none existed) covering direct, fenced, no-lang and prose-wrapped inputs, plus a benchmark. Benchmark, fenced payload (-benchmem): before: 3205 ns/op, 5712 B/op, 54 allocs/op after: 1315 ns/op, 693 B/op, 15 allocs/op Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -96,6 +96,10 @@ func DeduplicateWithScore[T ScoreComparable, K comparable](keyFunc func(T) K, it
|
||||
// {"key": "value"}
|
||||
// ```
|
||||
// or regular JSON responses directly.
|
||||
// jsonCodeFenceRE extracts a JSON payload wrapped in a Markdown code fence.
|
||||
// Compiled once: ParseLLMJsonResponse runs on the graph-extraction path.
|
||||
var jsonCodeFenceRE = regexp.MustCompile("```(?:json)?\\s*([\\s\\S]*?)```")
|
||||
|
||||
func ParseLLMJsonResponse(content string, target interface{}) error {
|
||||
// First, try to parse directly as JSON
|
||||
err := json.Unmarshal([]byte(content), target)
|
||||
@@ -104,8 +108,7 @@ func ParseLLMJsonResponse(content string, target interface{}) error {
|
||||
}
|
||||
|
||||
// If direct parsing fails, try to extract JSON from code blocks
|
||||
re := regexp.MustCompile("```(?:json)?\\s*([\\s\\S]*?)```")
|
||||
matches := re.FindStringSubmatch(content)
|
||||
matches := jsonCodeFenceRE.FindStringSubmatch(content)
|
||||
if len(matches) >= 2 {
|
||||
// Extract the JSON content within the code block
|
||||
jsonContent := strings.TrimSpace(matches[1])
|
||||
|
||||
Reference in New Issue
Block a user