Optimize gojq middleware: pre-compile query for 1.8-5.25x speedup #360
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The jqschema middleware was parsing the jq query on every request, violating gojq's documented best practice of "compile once, run many."
Changes
Pre-compile query at initialization
init()with fail-fast error handling*gojq.Codein package-level variable for reuseParse() + Run()pattern withCompile() + RunWithContext()Before:
After:
Add context support
context.Contextfrom HTTP handler to query executionRunWithContext()Enhance error handling
*gojq.HaltErrorto distinguish clean halts from failuresBenchmark suite
Performance Impact
Original prompt
This section details on the original issue you should resolve
<issue_title>[go-fan] Go Module Review: gojq</issue_title>
<issue_description># 🐹 Go Fan Report: gojq
Module Overview
gojq is a pure Go implementation of jq, the powerful JSON query and transformation language. It provides both a command-line tool and a Go library for parsing and executing jq queries, enabling JSON schema inference and transformation without external dependencies.
Current Version: v0.12.18 (latest, released Dec 1, 2025)
License: MIT
Stars: 3,666+
Maintenance: Active (last push 12 days ago)
Current Usage in gh-aw-mcpg
The project uses gojq in the jqschema middleware (
internal/middleware/jqschema.go) to transform JSON responses into type schemas.gojq.Parse(query)- Parse jq query stringsquery.Run(input)- Execute queries against JSON dataiter.Next()Current Pattern:
This parses the same jq query on every request which is a significant performance bottleneck.
Research Findings
Recent Updates
v0.12.18 (Dec 1, 2025) - Latest release includes:
trimstr/1,toboolean/0gojq.NewIteras generic functionBest Practices from Maintainers
The gojq documentation strongly recommends a compile-once pattern for repeated queries:
Key recommendations:
RunWithContextfor timeout/cancellationgojq.HaltError,ParseErrorImprovement Opportunities
🏃 Quick Wins
1. Compile Query Once (CRITICAL - High Impact, Low Effort)
Current Problem: Query is parsed on every request
Impact: 3-10x performance improvement
Effort: 10 minutes
Risk: Very low
Implementation:
2. Add Context Support (Medium Impact, Low Effort)
Current Problem: No timeout/cancellation support
Impact: Better resource management, prevents runaway queries
Effort: 5 minutes
Risk: Very low
Implementation:
3. Improve Error Handling (Low Impact, Low Effort)
Current Problem: Generic error checking
Impact: Better debugging experience
Effort: 15 minutes
Risk: Very low
Implementation:
✨ Feature Opportunities
Custom Functions for Schema Inference
gojq.WithFunctionto add custom schema functionsPerformance Monitoring
Query Validation at Startup
Multiple Query Support
📐 Best Practice Alignment
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.