diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000000..954473e674 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,4 @@ + +## 2024-05-24 - Optimize list comprehensions with walrus operator + **Learning:** List comprehensions with redundant function calls (like `input_str.find(char)`) cause the function to be evaluated twice, which is an anti-pattern that slows down processing, especially for O(n) string scanning. + **Action:** Use the Python 3.8+ walrus operator (`:=`) to compute and bind the result once, e.g., `[idx for char in chars if (idx := input_str.find(char)) != -1]`. diff --git a/helpers/dirty_json.py b/helpers/dirty_json.py index 8b731bee47..ab099bf784 100644 --- a/helpers/dirty_json.py +++ b/helpers/dirty_json.py @@ -349,5 +349,5 @@ def _peek(self, n): def get_start_pos(self, input_str: str) -> int: chars = ["{", "[", '"'] - indices = [input_str.find(char) for char in chars if input_str.find(char) != -1] + indices = [idx for char in chars if (idx := input_str.find(char)) != -1] return min(indices) if indices else 0