perf: fast-path sanitize_for_rich to skip per-call allocation#2572
Open
Vinv-AI wants to merge 1 commit into
Open
perf: fast-path sanitize_for_rich to skip per-call allocation#2572Vinv-AI wants to merge 1 commit into
Vinv-AI wants to merge 1 commit into
Conversation
sanitize_for_rich rebuilt every value character-by-character into a list and joined it, even in the common case of text with no control characters. Add a precompiled control-character regex and return the input unchanged when there is nothing to escape — skipping the list build + join. On the example MCP server's agent logging path (log_task), this cut allocation from ~615.7 KB to ~125 B across 3 calls, with byte-identical output. Co-Authored-By: VinvAI <support@vinv.ai>
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.
What
sanitize_for_richrebuilds every value character-by-character into alistand"".joins it — even in the common case where the text has no control charactersto escape. This adds a precompiled control-character regex and returns the input
unchanged when there's nothing to escape, skipping the list build + join:
Why
sanitize_for_richruns on every logged task / subtitle / error(
AgentLogger.log_task,.log_error, …). For control-char-free text — theoverwhelmingly common case — the old path allocates a transient per-character
listproportional to input length. On long agent runs this churn adds up.Proof (reproducible — script at the bottom)
1. Byte-identical output vs the previous implementation across 2,015 inputs
(strings, unicode,
bytes/bytearray/memoryview,None, ints, control-charmixes, empty, brackets, and 2,000 fuzzed random strings):
2. Transient per-call allocation (
tracemallocpeak during one call, control-char-free input):The saving scales with payload size (the old cost is one small
strobject percharacter). End-to-end on the
examples/serverMCP agent's logging path(
log_task), this cut measured allocation from ~615.7 KB → ~125 B across 3 calls.Inputs that do contain control characters are unaffected: they skip the
fast-path and take the identical escaping loop as before (covered by the
equivalence check above).
Reproduction script (
python prove_sanitize.py)Behavior
Output is identical — the fast-path only skips work when the regex confirms there
are no control characters; anything with control chars takes the existing path.