⚡️ Speed up method JavaScriptSupport._find_referenced_globals by 31% in PR #1335 (gpu-flag)
#1360
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 #1335
If you approve this dependent PR, these changes will be merged into the original PR branch
gpu-flag.📄 31% (0.31x) speedup for
JavaScriptSupport._find_referenced_globalsincodeflash/languages/javascript/support.py⏱️ Runtime :
2.27 milliseconds→1.74 milliseconds(best of66runs)📝 Explanation and details
The optimization achieves a 30% runtime reduction (from 2.27ms to 1.74ms) by consolidating multiple tree-sitter parse operations for helper functions into a single parse.
What changed:
Instead of calling
analyzer.find_referenced_identifiers()separately for each helper function in a loop (original approach), the optimized code combines all helper source code into a single string and performs one parse operation.Why this is faster:
Tree-sitter parsing has significant overhead. The line profiler shows this clearly:
for helper in helpers:with individualfind_referenced_identifiers()calls consumed 28.6% of total execution time (2.55ms across 65 hits)This represents a ~9x reduction in parsing overhead for the helper function analysis portion.
How it works:
Since
find_referenced_identifiers()returns a set of all identifiers found, combining sources doesn't change the result—the union of individual identifier sets equals the identifiers from the combined source.Test results show consistent improvements:
All test cases demonstrate faster execution (0.9% to 5% improvements), with the most significant gains in tests involving multiple helpers or larger codebases, confirming the optimization scales well with helper count.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1335-2026-02-04T01.36.54and push.