Skip to content

fix detection wrt config#1308

Merged
KRRT7 merged 14 commits intomainfrom
js-suggester
Feb 3, 2026
Merged

fix detection wrt config#1308
KRRT7 merged 14 commits intomainfrom
js-suggester

Conversation

@KRRT7
Copy link
Collaborator

@KRRT7 KRRT7 commented Feb 3, 2026

No description provided.

KRRT7 and others added 7 commits February 2, 2026 19:32
JS projects often have package.json main/module/exports pointing to
build output directories (build/, dist/, out/) which contain compiled
code. Now the detector prioritizes common source directories (src/,
lib/, source/) and skips build output paths when determining module root.
The js-suggester branch captures config_file from has_existing_config()
to display the path in --show-config output.
The detect_module_root() in config_js.py was not updated when
detector.py's _detect_js_module_root() was fixed to skip build
output directories. This caused --show-config to display 'src'
but --all to use 'build/src'.

Changes:
- Add is_build_output_dir() helper function
- Prioritize src/, lib/, source/ directories first
- Skip build output dirs (build/, dist/, out/, .next/, .nuxt/)
  when extracting paths from package.json fields
Import from codeflash.setup.detector instead of defining locally.
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.
@codeflash-ai
Copy link
Contributor

codeflash-ai bot commented Feb 3, 2026

⚡️ Codeflash found optimizations for this PR

📄 144% (1.44x) speedup for is_build_output_dir in codeflash/setup/detector.py

⏱️ Runtime : 503 microseconds 206 microseconds (best of 153 runs)

A dependent PR with the suggested changes has been created. Please review:

If you approve, it will be merged into this PR (branch js-suggester).

Static Badge

KRRT7 and others added 7 commits February 3, 2026 07:16
Tests now correctly expect '.' when package.json exports points to
build output directories (dist/, build/, etc.) without a src/ directory.
…2026-02-03T12.05.29

⚡️ Speed up function `is_build_output_dir` by 144% in PR #1308 (`js-suggester`)
Use starting_line/ending_line/starting_col/ending_col instead of
start_line/end_line/start_col/end_col in test_with_tricky_helpers test.
@KRRT7 KRRT7 merged commit 4a7432e into main Feb 3, 2026
26 of 27 checks passed
@KRRT7 KRRT7 deleted the js-suggester branch February 3, 2026 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant