⚡️ Speed up method JavaScriptTracer._get_function_alias by 18% in PR #1377 (js-trace)#1378
Closed
codeflash-ai[bot] wants to merge 6 commits intojs-tracefrom
Closed
⚡️ Speed up method JavaScriptTracer._get_function_alias by 18% in PR #1377 (js-trace)#1378codeflash-ai[bot] wants to merge 6 commits intojs-tracefrom
JavaScriptTracer._get_function_alias by 18% in PR #1377 (js-trace)#1378codeflash-ai[bot] wants to merge 6 commits intojs-tracefrom
Conversation
Add Babel-based function tracing for JavaScript/TypeScript: - tracer.js: Core tracer with SQLite storage and nanosecond timing - babel-tracer-plugin.js: AST transformation to wrap functions - trace-runner.js: Entry point for running traced code - replay.js: Utilities for replay test generation - Update package.json with exports and dependencies
Add Python modules for JavaScript tracing orchestration: - replay_test.py: Generate Jest/Vitest replay tests from traces - tracer_runner.py: Run trace-runner.js and detect test frameworks - tracer.py: Refactored to use Babel-only approach, removed legacy source transformation code
Move Jest/Vitest JUnit XML parsing from separate module into the main parse_test_output.py file to reduce module fragmentation. Remove duplicate tests that were testing the now-deleted module.
Add language detection and routing to JavaScript tracer in the main tracer CLI. Supports --language flag and auto-detection from file extensions in project.
Add comprehensive test coverage for JavaScript tracing: - Unit tests for trace parsing (function_calls and legacy schemas) - Unit tests for replay test generation (Jest and Vitest) - E2E test for full tracing pipeline with npm dependencies - Framework detection tests (Jest, Vitest, package.json)
The optimization achieves a **17% runtime improvement** by eliminating redundant regex compilation overhead.
**Key Changes:**
- Moved the regex pattern compilation (`r"[^a-zA-Z0-9]"`) from inside `_get_function_alias` to a module-level constant `_NON_ALNUM_RE`
- Changed from `re.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's `re.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:
- **Simple paths**: 35-50% faster on individual calls (2-4μs range)
- **Complex paths with many special characters**: 30-40% faster (still significant)
- **Very long inputs**: Smaller but measurable gains (7-19% on 100+ directory levels, 1.4% on extreme 500-directory paths)
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_alias` is 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.
3 tasks
f2dd9cf to
55da4a2
Compare
Collaborator
|
Closing stale bot PR. |
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 #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.