⚡️ Speed up method JavaScriptTracer._get_function_alias by 18% in PR #1377 (js-trace)
#1378
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 #1377
If you approve this dependent PR, these changes will be merged into the original PR branch
js-trace.📄 18% (0.18x) speedup for
JavaScriptTracer._get_function_aliasincodeflash/languages/javascript/tracer.py⏱️ Runtime :
294 microseconds→250 microseconds(best of250runs)📝 Explanation and details
The optimization achieves a 17% runtime improvement by eliminating redundant regex compilation overhead.
Key Changes:
r"[^a-zA-Z0-9]") from inside_get_function_aliasto a module-level constant_NON_ALNUM_REre.sub(r"[^a-zA-Z0-9]", "_", module_path)to_NON_ALNUM_RE.sub("_", module_path)Why This Is Faster:
In the original code,
re.sub()compiles the regex pattern on every function call. Python'sre.sub()internally compiles the pattern, checks a cache, and performs the substitution. Even with caching, this lookup and compilation check adds overhead.By pre-compiling the pattern once at module load time, we eliminate this repeated compilation overhead. The line profiler data confirms this - the regex substitution line dropped from 9.45ms (82.3% of time) to 4.03ms (66.7% of time), cutting the bottleneck in half.
Performance Characteristics:
The annotated tests show consistent improvements across all scenarios:
The optimization is most effective for typical usage patterns with moderate path complexity, where the regex compilation overhead is a significant fraction of total execution time. For extremely long inputs where string manipulation dominates, the relative benefit decreases but remains positive.
Impact on Workloads:
Since
_get_function_aliasis called 3,341 times in the profiling run and is used to generate function aliases for JavaScript imports/tracing, this optimization provides compounding benefits in real-world scenarios where many functions are being traced or instrumented. The cumulative time savings scale linearly with call frequency.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1377-2026-02-04T10.33.39and push.