[Repo Assist] perf(middleware): skip json.Marshal for small text responses (fast path)#7094
Merged
lpcox merged 5 commits intoJun 6, 2026
Conversation
… path) For the common case where a tool response fits comfortably within the payload size threshold, WrapToolHandler was performing a full json.Marshal of the response data on every call solely to measure the serialised byte count, then discarding the allocation. When the backend handler (or response filter) sets result.Content[0] to a *sdk.TextContent, the text length is a reliable lower bound for the total serialised size. The outer JSON envelope (content array wrapper, isError, any DIFC notice items) adds at most a few hundred bytes. Using a conservative 4 KiB overhead margin (fastPathOverheadBound), we can safely return the original result without marshalling when: len(tc.Text) <= sizeThreshold - fastPathOverheadBound This avoids the heap allocation for the large majority of tool calls whose payloads are well within the 512 KiB default threshold. The guard "sizeThreshold > fastPathOverheadBound" prevents the fast path from triggering in small-threshold test scenarios (e.g., threshold=0 or threshold=100 in unit tests), preserving exact existing test behaviour. Also adds: - BenchmarkWrapToolHandler_FastPath in jqschema_bench_test.go to quantify the improvement across 1 KB / 10 KB / 100 KB payload sizes - TestWrapToolHandler_FastPath_SkipsMarshal to verify the optimisation returns the original result unchanged and creates no payload files - TestWrapToolHandler_FastPath_FallsThroughAtBoundary to verify the fast path does not fire when the threshold is too small Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a performance optimization in the jqschema middleware (WrapToolHandler) to avoid calling json.Marshal(data) for the common “small text response” case, reducing unnecessary allocations when the payload is clearly under the configured size threshold.
Changes:
- Adds a fast-path heuristic in
wrapToolHandlerto skipjson.Marshalwhen the first content item is text and its size is comfortably below the threshold. - Adds tests intended to validate the fast-path behavior and boundary fall-through.
- Adds a benchmark comparing fast-path vs marshal path.
Show a summary per file
| File | Description |
|---|---|
internal/middleware/jqschema.go |
Adds fastPathOverheadBound constant and a fast-path early return in wrapToolHandler to avoid json.Marshal in common cases. |
internal/middleware/jqschema_test.go |
Adds new unit tests for the fast path and a non-fast-path scenario. |
internal/middleware/jqschema_bench_test.go |
Adds benchmark intended to compare fast-path vs marshal-based sizing. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 3/3 changed files
- Comments generated: 4
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This was referenced Jun 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
🤖 This is an automated PR from Repo Assist.
Summary
Skips the
json.Marshalcall inWrapToolHandlerfor the common case where a tool response's text content is clearly within the payload size threshold, avoiding a large heap allocation that was always discarded for typical responses.Motivation
Every tool call through
WrapToolHandlertriggers ajson.Marshal(data)on the full response envelope — even for small responses that return without disk storage — solely to measure the serialised byte count. For the 512 KB default threshold and typical responses well under 1 KB, this allocation serves no purpose.Approach
When the backend handler (or filter) sets
result.Content[0]to a*sdk.TextContent, itsTextfield byte length is a reliable lower bound for the total serialised size. The outer JSON envelope adds at most a few hundred bytes (contentarray wrapper,isError, optional DIFC notice items). Using a conservativefastPathOverheadBound = 4 KiBmargin:This fires for the vast majority of tool calls (typical responses 1–100 KB; default threshold 512 KB), while falling through to the existing full-marshal path for any response that could be close to the threshold.
Safety
sizeThreshold > fastPathOverheadBoundprevents the optimisation from triggering in small-threshold unit test scenarios (threshold=0, threshold=100), so all existing tests pass unchanged.Files Changed
internal/middleware/jqschema.gofastPathOverheadBoundconstant + fast-path check inwrapToolHandlerclosureinternal/middleware/jqschema_test.goTestWrapToolHandler_FastPath_SkipsMarshalandTestWrapToolHandler_FastPath_FallsThroughAtBoundaryinternal/middleware/jqschema_bench_test.goBenchmarkWrapToolHandler_FastPathcomparing fast path vs. full marshal at 1/10/100 KB payload sizesTest Status
gofmtcheck: ✅ no formatting issuesproxy.golang.orgis blocked by the sandbox firewall, so module downloads and test execution could not complete locally. The CI pipeline on this PR will run the full test suite.Content, so the fast path never triggers for any existing test case.Warning
Firewall blocked 1 domain
The following domain was blocked by the firewall during workflow execution:
proxy.golang.orgSee Network Configuration for more information.
Add this agentic workflows to your repo
To install this agentic workflow, run