⚡️ Speed up function _build_code_strings_for_language by 28% in PR #1473 (refactor/tree-sitter-instrumentation)#1475
Closed
codeflash-ai[bot] wants to merge 1 commit into
Conversation
This optimization achieves a **27% runtime improvement** (51.7ms → 40.4ms) by eliminating redundant work in path resolution and regex compilation—two operations that were consuming over 70% of the original runtime. **Key optimizations:** 1. **Precompiled regex pattern** (`_RE_JAVADOC`): The original code imported `re` and compiled the Javadoc pattern on every call to `_strip_javadoc_comments`. By moving the regex compilation to module scope, we eliminate repeated compilation overhead. Line profiler shows this function dropping from 444ns to 22ns per call—a ~20× improvement. 2. **Cached path resolution**: The original code called `.resolve()` repeatedly inside the helper loop (line taking 70.9% of total time). The optimization resolves `project_root_path` once upfront and reuses `project_root_resolved` throughout. For helpers, it now resolves each `file_path` once and reuses `helper_file_resolved`, avoiding 1,208+ redundant resolve operations per invocation. 3. **Hoisted target file resolution**: Similar to the project root, the target file path is resolved once and stored in `target_file_resolved`, eliminating duplicate work when computing `target_relative_path`. 4. **List comprehensions in joins**: Changed generator expressions to list comprehensions in `"\n\n".join()` calls. While this has minimal performance impact for small collections, it can improve performance for larger helper lists by avoiding iterator overhead. **Why this matters:** - Path resolution involves filesystem syscalls and is expensive—the profiler shows the original helper loop spending 125ms on path operations alone - The optimization is particularly effective for the large-scale test case (1000 helpers), which sees a **39.4% speedup** (32.3ms → 23.1ms) - Tests with Javadoc stripping also benefit significantly (15.2% faster), as regex compilation overhead is eliminated - The cross-file helpers test improves by 9.83% due to reduced path resolution overhead **Trade-offs:** Minor increases in some small test cases (1-4% slower) are within measurement noise and acceptable given the substantial gains in realistic workloads with multiple helpers and path operations.
5 tasks
Contributor
Author
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 #1473
If you approve this dependent PR, these changes will be merged into the original PR branch
refactor/tree-sitter-instrumentation.📄 28% (0.28x) speedup for
_build_code_strings_for_languageincodeflash/context/code_context_extractor.py⏱️ Runtime :
51.7 milliseconds→40.4 milliseconds(best of74runs)📝 Explanation and details
This optimization achieves a 27% runtime improvement (51.7ms → 40.4ms) by eliminating redundant work in path resolution and regex compilation—two operations that were consuming over 70% of the original runtime.
Key optimizations:
Precompiled regex pattern (
_RE_JAVADOC): The original code importedreand compiled the Javadoc pattern on every call to_strip_javadoc_comments. By moving the regex compilation to module scope, we eliminate repeated compilation overhead. Line profiler shows this function dropping from 444ns to 22ns per call—a ~20× improvement.Cached path resolution: The original code called
.resolve()repeatedly inside the helper loop (line taking 70.9% of total time). The optimization resolvesproject_root_pathonce upfront and reusesproject_root_resolvedthroughout. For helpers, it now resolves eachfile_pathonce and reuseshelper_file_resolved, avoiding 1,208+ redundant resolve operations per invocation.Hoisted target file resolution: Similar to the project root, the target file path is resolved once and stored in
target_file_resolved, eliminating duplicate work when computingtarget_relative_path.List comprehensions in joins: Changed generator expressions to list comprehensions in
"\n\n".join()calls. While this has minimal performance impact for small collections, it can improve performance for larger helper lists by avoiding iterator overhead.Why this matters:
Trade-offs:
Minor increases in some small test cases (1-4% slower) are within measurement noise and acceptable given the substantial gains in realistic workloads with multiple helpers and path operations.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1473-2026-02-13T00.58.09and push.