⚡️ Speed up method Messages.format by 527%
#64
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.
📄 527% (5.27x) speedup for
Messages.formatinguardrails/prompt/messages.py⏱️ Runtime :
57.2 milliseconds→9.11 milliseconds(best of58runs)📝 Explanation and details
The optimization achieves a 527% speedup by addressing two major performance bottlenecks identified in the line profiler results:
1. Template Variable Extraction (32% faster)
collections.defaultdictwithTemplate.safe_substitute()to extract variables, creating unnecessary dictionary objects and performing full template substitution_TEMPLATE_VAR_PATTERN = re.compile(r"\${([_a-zA-Z][_a-zA-Z0-9]*)")2. Kwargs Filtering (88% faster)
{k: v for k, v in kwargs.items() if k in vars}performed O(n×m) operations, iterating through all kwargs for each message{k: kwargs[k] for k in vars if k in kwargs_keys}with pre-computedkwargs_keys = set(kwargs.keys())reduces to O(n) set lookupskwargs.items()iterationPerformance Impact by Test Scale:
The optimizations are particularly effective for applications processing many messages with multiple template variables, where the O(n×m) complexity reduction and template parsing efficiency gains provide exponential benefits.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
⏪ Replay Tests and Runtime
test_pytest_testsunit_teststest_guard_log_py_testsintegration_teststest_guard_py_testsunit_testsvalidator__replay_test_0.py::test_guardrails_prompt_messages_Messages_formatTo edit these changes
git checkout codeflash/optimize-Messages.format-mh2jlxnjand push.