From 9147f61b40c63d287791555cabc536f52570dd83 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Mon, 20 Oct 2025 17:22:41 +0000 Subject: [PATCH] Optimize fix_json_escape_char **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. --- pr_agent/algo/utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pr_agent/algo/utils.py b/pr_agent/algo/utils.py index 72a0624c51..9c231b0b23 100644 --- a/pr_agent/algo/utils.py +++ b/pr_agent/algo/utils.py @@ -653,9 +653,9 @@ def fix_json_escape_char(json_message=None): # Find the offending character index: idx_to_replace = int(str(e).split(' ')[-1].replace(')', '')) # Remove the offending character: - json_message = list(json_message) - json_message[idx_to_replace] = ' ' - new_message = ''.join(json_message) + msg_bytes = bytearray(json_message, 'utf-8') + msg_bytes[idx_to_replace] = ord(' ') + new_message = msg_bytes.decode('utf-8') return fix_json_escape_char(json_message=new_message) return result