⚡️ Speed up function get_optimized_code_for_module by 2,599% in PR #1199 (omni-java)
#1240
+33
−13
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.
⚡️ This pull request contains optimizations for PR #1199
If you approve this dependent PR, these changes will be merged into the original PR branch
omni-java.📄 2,599% (25.99x) speedup for
get_optimized_code_for_moduleincodeflash/code_utils/code_replacer.py⏱️ Runtime :
24.9 milliseconds→922 microseconds(best of30runs)📝 Explanation and details
This optimization achieves a 26x speedup (2598% improvement) by eliminating expensive logging operations that dominated the original runtime.
Key Performance Improvements
1. Conditional Logging Guard (95% of original time eliminated)
The original code unconditionally formatted expensive log messages even when logging was disabled:
This single operation consumed 111ms out of 117ms total runtime (95%).
The optimization adds a guard check:
This prevents string formatting and object serialization when the log message won't be emitted, dramatically reducing overhead in production scenarios where warning-level logging may be disabled.
2. Eliminated Redundant Path Object Creation
The original created
Pathobjects repeatedly during filename matching:The optimized version uses string operations:
This removes overhead from Path instantiation (1.16ms → 44µs in the profiler).
3. Minor Cache Lookup Optimization
Changed from
self._cache.get("file_to_path") is not Noneto"file_to_path" in self._cacheand hoisted the dict assignment to avoid inline mutation, providing small gains in the caching path.4. String Conversion Hoisting
Pre-computed
relative_path_str = str(relative_path)to avoid repeated conversions.Test Case Performance Patterns
test_empty_code_strings: 1.03ms → 12.9µs (7872% faster)test_no_match_multiple_blocks: 1.28ms → 16.3µs (7753% faster)test_many_code_blocks_no_match: 20.5ms → 107µs (18985% faster)The optimization particularly benefits scenarios where file path mismatches occur, as these trigger the expensive warning path in the original code. For the common case of exact matches, the improvements are modest but consistent.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1199-2026-02-01T22.01.32and push.