⚡️ Speed up function is_build_output_dir by 144% in PR #1308 (js-suggester)#1310
Merged
KRRT7 merged 1 commit intojs-suggesterfrom Feb 3, 2026
Merged
Conversation
The optimized code achieves a **143% speedup** (from 503μs to 206μs) through three key improvements:
## Primary Optimizations
1. **Eliminated expensive string operations**: The original code called `path.as_posix().split("/")` on every invocation, which:
- Converts the Path object to a POSIX string representation
- Allocates a new string
- Splits it into a list
The optimized version uses `path.parts`, which is a cached tuple property already available on Path objects, eliminating ~61% of the original runtime (line profiler shows `as_posix().split()` took 60.9% of execution time).
2. **Avoided set reconstruction**: The original code recreated the `build_dirs` set on every function call. The optimized version pre-computes `_BUILD_DIRS` as a module-level `frozenset`, eliminating the 5.5% overhead from set creation.
3. **Leveraged efficient set operation**: Instead of using a generator expression with `any()` that performs multiple `in` checks (33.6% of original time), the optimized code uses `isdisjoint()` - a built-in set method implemented in C that performs a single, optimized intersection check.
## Why This Matters
The test results show consistent speedups across all scenarios:
- **Simple paths**: 2-3x faster (e.g., `Path("build")`: 5.19μs → 2.34μs)
- **Deep nesting**: 2-3x faster even with complex paths
- **Large-scale tests**: 3-4x faster for paths with 100+ components (e.g., 100 regular dirs: 14.8μs → 4.26μs, **248% speedup**)
- **Worst-case scenarios**: The optimization shines brightest when checking long paths with no matches (21.7μs → 4.78μs, **355% speedup**)
The optimization scales particularly well because `isdisjoint()` can short-circuit as soon as it finds a match, while `path.parts` access is essentially free after the first call. This makes the function highly efficient for both hot paths that check many paths repeatedly and deep directory structures common in real projects.
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 #1308
If you approve this dependent PR, these changes will be merged into the original PR branch
js-suggester.📄 144% (1.44x) speedup for
is_build_output_dirincodeflash/setup/detector.py⏱️ Runtime :
503 microseconds→206 microseconds(best of153runs)📝 Explanation and details
The optimized code achieves a 143% speedup (from 503μs to 206μs) through three key improvements:
Primary Optimizations
Eliminated expensive string operations: The original code called
path.as_posix().split("/")on every invocation, which:The optimized version uses
path.parts, which is a cached tuple property already available on Path objects, eliminating ~61% of the original runtime (line profiler showsas_posix().split()took 60.9% of execution time).Avoided set reconstruction: The original code recreated the
build_dirsset on every function call. The optimized version pre-computes_BUILD_DIRSas a module-levelfrozenset, eliminating the 5.5% overhead from set creation.Leveraged efficient set operation: Instead of using a generator expression with
any()that performs multipleinchecks (33.6% of original time), the optimized code usesisdisjoint()- a built-in set method implemented in C that performs a single, optimized intersection check.Why This Matters
The test results show consistent speedups across all scenarios:
Path("build"): 5.19μs → 2.34μs)The optimization scales particularly well because
isdisjoint()can short-circuit as soon as it finds a match, whilepath.partsaccess is essentially free after the first call. This makes the function highly efficient for both hot paths that check many paths repeatedly and deep directory structures common in real projects.✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
🔎 Click to see Concolic Coverage Tests
To edit these changes
git checkout codeflash/optimize-pr1308-2026-02-03T12.05.29and push.