Repository navigation
Speed up file reads, parsing, staging, and indexing pipeline - #87
Merged
Merged
Conversation
- Pipeline indexing in `indexing.py` so a context-propagated background producer scans, reads, and parses into a bounded queue (`maxsize=4`) while the main thread embeds and stages chunks/references concurrently; bump candidate group thresholds to 512 files / 512 KiB. - Add a lean `spawn`-based multi-process parser pool (`parse_pool.py`, configurable via `CODE_INDEXING_PARSE_WORKERS` / `IndexSettings.parse_workers`) using compact wire tuples and avoiding heavy runtime imports (`lancedb`, `pyarrow`) in worker processes. - Optimize single-core extraction and staging (`extractor.py`, `models.py`, `language_rules.py`, `staging.py`, `progress.py`): `_ParentMap` parent index, per-language `_IdentifierDispatch` tables, guarded `class_heritage` and `_call_shape` checks, slotted dataclasses (`CallShape`, `ExtractedReference`, `_LanguageRules`, `ChunkRow`, `ReferenceRow`), columnar Arrow reference staging via `StagingJob.stage_reference_columns`, and shallow-copied deferred updates in `ProgressPublisher`. - Speed up file I/O and directory traversal (`source_io.py`, `scanner.py`): single-shot `os.read` fast path, `read_source_direct`, and `.git` repo-boundary detection inside `os.scandir` walks with single `rel_posix` computation.
…progress - Switch ParseWorkerPool to spawn ProcessPoolExecutor so worker crashes raise BrokenProcessPool instead of deadlocking drain_one(), and enforce memory budgets in workers and batch draining. - Preserve force/retry read-error semantics in run_pool via ParseTask.stat_matched_previous so unreadable files retain indexed chunks and record retryable failures instead of being deleted. - Pass scan (st_dev, st_ino) identity through ScannedFile/ParseTask to read_source_direct() so PinnedDirectoryCache recovers from renamed-and-replaced directories without an extra lstat. - Flush pending lazy_fields on ProgressPublisher.state reads and apply monotonic phase anchoring to lazy phase updates. - Add producer liveness guard in _index_scan, deduplicate MAX_PARSE_WORKERS, WireChunk, and Indexer._reference_rows, remove dead SourceScanner._is_repository_worktree, fix CallShape non-ASCII JSON parity, and add regression tests.
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.
Summary
Pure-Python optimizations (no compiled native wheels) across file scanning, source I/O, tree-sitter extraction, Arrow staging, progress reporting, and indexing concurrency so parsing and I/O stay ahead of fast local embedding backends (e.g., Apple Silicon MLX):
Producer–consumer indexing pipeline (
indexing.py,application.py)contextvars.copy_context()-propagated background producer thread feeding a boundedqueue.Queue(maxsize=4)while the main thread embeds and stages chunks/references concurrently.CANDIDATE_GROUP_COUNTto512files andCANDIDATE_GROUP_CHARSto512 KiB.Multi-process parse worker pool (
parse_pool.py,settings.py,indexing.py)spawn-based worker pool (ParseWorkerPool,resolve_parse_workers, configurable viaCODE_INDEXING_PARSE_WORKERS/IndexSettings.parse_workers, where0/autopicks worker count automatically up toMAX_PARSE_WORKERS = 16).parse_pool.pyfree oflancedbandpyarrowimports (~99.1 mscold worker import).Single-core extraction, columnar Arrow staging & deferred progress (
extractor.py,language_rules.py,models.py,staging.py,progress.py).parentwalks with_ParentMap, dispatch identifier extraction through precomputed per-language_IdentifierDispatchtables, gate JS/TSclass_heritageclimbs and_call_shapechecks, and throttle chunk memory checks.CallShape,ExtractedReference,_LanguageRules,ChunkRow,ReferenceRow) to__slots__dataclasses with compact JSON dumping onCallShape.StagingJob.stage_reference_columns(...)instead of per-row dict/object construction.copy.deepcopyinProgressPublisherwith shallow container cloning, deferredmodel_copy, and throttledlazy_fields.Fast file reads and directory traversal (
source_io.py,scanner.py)_open_source_fd, a single-shotos.readfast path in_read_from_fd, andread_source_directfor pre-stat'd candidate files..gitrepository boundaries directly insideos.scandirwalks (_iter_walk_candidate_batches) and avoid redundant per-file path normalization in_scan_candidate_entries.Benchmarks (Apple M4 Pro, Python 3.12.11, 12 CPUs, 48 GiB RAM)
Indexer.index(316 repo files,DeterministicEmbedder)3,816 ms(parse_workers=1)2,359 ms(parse_workers=0, auto=6)embed ≈ 38.1 s)39,547 ms(in-process) /40,108 ms(pool); parse hidden behind embed34 ms/820 ms)TreeSitterExtractor.extract(314-file isolated A/B)2.240 stotal (1.583 sref extract)1.744 stotal (1.100 sref extract)146,109rows)467.3 ms(row objects)206.1 ms(columnar Arrow)ProgressPublisher.update×10,000 (throttled0.25 s)101.7 ms4.72–4.80 ms8.7 MB)364.2 ms255.0 ms(read_source_direct:84.0 ms)99.1 ms(nolancedb/pyarrow)Verification
Run on branch
perf/file-reads-parsing-pipeline(based onorigin/main64036f3):uv run --no-sync ruff format --check .→171 files already formatteduv run --no-sync ruff check .→All checks passed!uv run --no-sync mypy src→Success: no issues found in 79 source filesuv run --no-sync pytest tests/test_extractor_equivalence.py -v→5 passed(tests/fixtures/extractor_snapshot.jsonuntouched)uv run --no-sync pytest -n auto --dist worksteal:2789 passed, 9 skippedacross all non-socket/non-home-config tests in sandbox (includestests/test_mlx_backend.pycollection-level skip withoutmlxextra)234 passed, 1 skippedacross the 9 Unix-socket / home-config test files (tests/test_daemon.py,tests/test_daemon_lifecycle.py,tests/test_index_worker.py,tests/test_installer_cli.py,tests/test_installer_orchestrator.py,tests/test_installer_tui.py,tests/test_mcp_timeouts.py,tests/test_tui_service.py,tests/test_worker_launcher.py) run unsandboxed (Linux runtime-dir test skipped on macOS)src/code_indexing_mcp/mlx_backend.pywas not modified.