⚡️ Speed up method FunctionOptimizer._get_java_sources_root by 52% in PR #1199 (omni-java)
#1374
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.📄 52% (0.52x) speedup for
FunctionOptimizer._get_java_sources_rootincodeflash/optimization/function_optimizer.py⏱️ Runtime :
202 microseconds→133 microseconds(best of25runs)📝 Explanation and details
The optimized code achieves a 51% speedup (202μs → 133μs) by reducing redundant work in the
_get_java_sources_rootmethod through three key improvements:1. Single-pass loop structure
The original code used two separate loops over the same
partstuple. The optimized version merges these into a single loop, eliminating redundant iteration. Sincepartscan have hundreds of components (as shown in the large path tests), this avoids processing up to 1000+ elements twice.2. Deferred Path construction
The optimized code only creates the
Path(*parts[:i])object when actually needed (when a standard prefix is found ati > 0). Line profiler shows this Path construction takes 40% of total time in the optimized version, so avoiding unnecessary constructions in fallback paths saves significant cycles.3. Conditional debug logging
By wrapping debug statements with
if logger.isEnabledFor(logging.DEBUG):, the code avoids constructing expensive f-strings when debug logging is disabled (the common case in production). The original code always built these strings even when they wouldn't be logged.Test Results Analysis:
The optimization maintains exact functional behavior: it returns the same path boundaries for standard package prefixes (prioritizing them over 'java' directories) and falls back to
tests_rootidentically to the original. This is particularly important since the function helps locate Java source roots in Maven/Gradle project structures, where correctness is critical for test file resolution.Impact on workloads: Since this method is called during function optimizer initialization for Java projects, the speedup reduces setup overhead proportionally to path complexity, with larger/deeper project structures seeing the most benefit.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1199-2026-02-04T08.13.40and push.