⚡️ Speed up function fix_json_escape_char by 343%#40
Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
Open
⚡️ Speed up function fix_json_escape_char by 343%#40codeflash-ai[bot] wants to merge 1 commit intomainfrom
fix_json_escape_char by 343%#40codeflash-ai[bot] wants to merge 1 commit intomainfrom
Conversation
**Optimizations applied:**
- **Convert to `bytearray` instead of `list` for string mutation:**
Using `bytearray` is much more efficient for single-character replacements and for joining back to a string, especially for typical ASCII JSON strings.
The common pattern (`list(str)` with per-recursion `''.join`) is replaced with in-place byte mutation followed by a single `.decode('utf-8')`.
**Behavior is preserved:**
- Exception extraction and character replacement logic are unchanged.
- Recursion pattern is preserved.
- No changes to comments except to clarify the bytearray usage.
If your JSON contains *non-ASCII* characters, you may need to adjust the encoding handling, but for most JSON logs/messages, this is significantly faster and avoids repeatedly copying large lists and strings.
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.
📄 343% (3.43x) speedup for
fix_json_escape_charinpr_agent/algo/utils.py⏱️ Runtime :
9.87 milliseconds→2.23 milliseconds(best of30runs)📝 Explanation and details
Optimizations applied:
bytearrayinstead oflistfor string mutation:Using
bytearrayis much more efficient for single-character replacements and for joining back to a string, especially for typical ASCII JSON strings.The common pattern (
list(str)with per-recursion''.join) is replaced with in-place byte mutation followed by a single.decode('utf-8').Behavior is preserved:
If your JSON contains non-ASCII characters, you may need to adjust the encoding handling, but for most JSON logs/messages, this is significantly faster and avoids repeatedly copying large lists and strings.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
unittest/test_fix_json_escape_char.py::TestFixJsonEscapeChar.test_multiple_control_charsunittest/test_fix_json_escape_char.py::TestFixJsonEscapeChar.test_single_control_charunittest/test_fix_json_escape_char.py::TestFixJsonEscapeChar.test_valid_json🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-fix_json_escape_char-mgzelfq8and push.