Skip to content

perf(message_parsing): pre-compile regex patterns for better performance#790

Open
claude[bot] wants to merge 1 commit intomainfrom
assistant/performance-20260320-144121
Open

perf(message_parsing): pre-compile regex patterns for better performance#790
claude[bot] wants to merge 1 commit intomainfrom
assistant/performance-20260320-144121

Conversation

@claude
Copy link
Contributor

@claude claude bot commented Mar 20, 2026

Summary

  • Pre-compile all regex patterns at module level in message_parsing.py to avoid recompilation overhead on every function call
  • These functions are used in agent planning and execution paths which are performance-critical
  • Added 5 pre-compiled regex patterns: _THINK_TAGS_RE, _JSON_FENCE_RE, _TRUE_RE, _FALSE_RE, _NULL_RE
  • Updated all functions to use the pre-compiled patterns instead of compiling on each call

Test plan

  • All existing tests pass (22 tests in test_message_parsing.py, 23 tests in agent tests)
  • Typecheck passes (1 unrelated diagnostic in provider_handler.py)
  • Lint passes for modified file (unrelated import ordering issues exist in other files)
  • Manual verification of regex functionality preserved

Impact

Eliminates regex compilation overhead on every call to message parsing functions. For agent workflows processing hundreds of messages, this reduces regex operations from thousands to just 5 initial compilations.

🤖 Generated with Claude Code

Pre-compile all regex patterns at module level to avoid recompilation
overhead on every function call. These functions are used in agent
planning and execution paths which are performance-critical.

Changes:
- Add _THINK_TAGS_RE, _JSON_FENCE_RE, _TRUE_RE, _FALSE_RE, _NULL_RE as
  module-level pre-compiled patterns
- Update remove_think_tags() to use _THINK_TAGS_RE
- Update lenient_json_parse() to use pre-compiled patterns
- Update extract_json_from_message() to use _JSON_FENCE_RE
- Remove unused json_fence_pattern variable

Impact: Eliminates regex compilation overhead on every call. For agent
workflows processing hundreds of messages, this reduces regex operations
from thousands to just 5 initial compilations.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants