Repository navigation
perf(mcp): Copy tool output on write when replacing lone surrogates - #1798
Merged
Merged
Conversation
toWellFormedOutput rebuilt every array and object in every tool response, even when nothing needed replacing. Return the input as-is unless a string or key changes, and copy only the containers on the path to it. Co-Authored-By: BYK <126780+BYK@users.noreply.github.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub. 1 Skipped Deployment
|
BYK
approved these changes
Nov 2, 2026
This branch was previously deployed
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.
Follow-up to #1790.
toWellFormedOutputruns on every tool response, and it rebuilt every array (map) and object (Object.entries→map→Object.fromEntries), even when there was nothing to replace. Almost no response contains a lone surrogate, so nearly all of that copying was wasted.It now copies on write. It returns the input unchanged unless a string or key actually changes, and then copies only the arrays and objects on the path to that string. Objects are walked with
for...in+Object.hasOwn, so the common path allocates nothing. Output is the same as before.Benchmark
Node 22.23, median time per call and bytes allocated per call (large semi-space so no GC runs inside the measured loop). "Structured" rows are issue-shaped objects with 12 fields, a nested project object, and a tags array.
JSON.stringifyfor scalecontent[], 1 × 45 KB textcontent[]output was already cheap. V8 returns the same string fromtoWellFormed()when the string is well formed, so most of the time left is the scan itself, which can't be skipped.JSON.stringifyonce and walking only when the JSON contains a\uD8xx–\uDFxxescape. It was slower than the walk in every case, so it isn't used.Tests
The structured case in
server.test.tsnow has a lone surrogate inside a nested array, next to strings that don't change. That covers the partial-copy path; if the copy drops the entries before the changed one, the test fails.via @BYK.
--
View Session in Sentry