perf: cache jedi project, batch test cache writes, fix Windows relative_to bug#1852
perf: cache jedi project, batch test cache writes, fix Windows relative_to bug#1852
Conversation
|
Claude finished @KRRT7's task in 3s —— View job PR Review Summary
Prek Checks
Mypy: One new error introduced by this PR was fixed and pushed (
All remaining mypy errors in the changed files are pre-existing (present on Code Review
Duplicate DetectionNo duplicates detected. Test CoverageAll relevant tests pass:
Coverage for changed files (against these test suites):
Low coverage is expected — these files depend on live Jedi analysis, SQLite, and API keys for most paths. The tests that can run without external dependencies all pass. Fix committed: |
…ve_to bug Port valuable improvements from #1846 that remain applicable after #1660: - Cache jedi.Project instances via @cache to avoid recreating across 5 call sites - Fix unguarded relative_to() in get_code_optimization_context (Windows 8.3 paths) - Pre-group references by parent function in get_function_sources_from_jedi for O(1) lookup - Batch TestsCache writes with flush() + executemany instead of per-row commit - Gracefully disable cache writes on sqlite3.OperationalError - Build functions_to_optimize_by_name dict for O(1) fallback lookup in process_test_files - Derive all_defs from all_names via is_definition() to save a redundant Jedi call
27c8226 to
5060c08
Compare
The optimization added early-exit logic to `add_needed_imports_from_module` that checks whether the source module contains any module-level imports before invoking the heavyweight `GatherImportsVisitor` and downstream import-merging machinery. In the common case where a pruned module has no imports (or only nested ones inside functions), line profiling showed the gatherer and two `AddImportsVisitor`/`RemoveImportsVisitor` transforms consumed 36% of original runtime; the early exit skips all three, falling back to the destination code immediately. A second early exit after gathering verifies the visitor actually collected imports, avoiding redundant CST transformations when the source is import-free. Combined, these checks eliminate ~99% of the work when imports are absent, yielding an 18222% speedup with no semantic change because the fallback path always returned the correct destination code.
⚡️ Codeflash found optimizations for this PR📄 18,223% (182.23x) speedup for
|
…2026-03-17T06.54.17 ⚡️ Speed up function `re_extract_from_cache` by 18,223% in PR #1852 (`cf-1846-port-perf-improvements`)
|
This PR is now faster! 🚀 @claude[bot] accepted my optimizations from: |
|
@claude fix failing CI |
|
Claude finished @KRRT7's task in 3m 34s —— View job Fixed Failing CI
Root CauseThe mypy CI failure was a
FixRemoved the first (weaker) definition at lines 1586–1598, along with its now-unused
|
…no-redef error Co-authored-by: Kevin Turcios <KRRT7@users.noreply.github.com>
…imports_from_module The generator expression yielded `isinstance(stmt, ...)` where `stmt` was already filtered to be `cst.SimpleStatementLine`, so it always returned False. This caused add_needed_imports_from_module to always skip adding imports, breaking 11 tests in test_code_context_extractor.py. Co-authored-by: Kevin Turcios <undefined@users.noreply.github.com>
…n flat output
- Replace with
in
_maybe_strip_docstring — the | syntax for runtime isinstance is Python 3.10+
only and caused TypeError on 3.9
- Strip leading newlines from cst.Module.code in add_needed_imports_from_module
fallback path so early-exit returns are consistent with the normal
transformed_module.code.lstrip("\n") path, fixing extra blank lines after
file headers in read_writable_code.flat output
Co-Authored-By: Oz <oz-agent@warp.dev>
|
@claude work on the identified issues in #1852 (comment) |
|
Claude finished @KRRT7's task in 1m 36s —— View job
Changes Made
Committed as |
…mports_from_module
- Remove silent .lstrip("\n") on fallback for cst.Module input (restores original behavior)
- Replace `parsed_dst_module is None` with `isinstance(dst_module_code, str)` for correct mypy narrowing
- Change get_jedi_project return type from object to Any for accuracy
Co-authored-by: Kevin Turcios <KRRT7@users.noreply.github.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…wing The early `parsed_dst_module = None` assignment broke mypy's type narrowing. Add explicit `else: parsed_dst_module = dst_module_code` so mypy sees the variable is always a `cst.Module` before the `.visit()` call. Co-authored-by: Kevin Turcios <undefined@users.noreply.github.com>
Summary
Ports the valuable improvements from #1846 that remain applicable after #1660 was merged:
jedi.Projectinstances via@cacheto avoid recreating across 5 call sitesrelative_to()inget_code_optimization_context(Windows 8.3 path bug)get_function_sources_from_jedifor O(1) lookupTestsCachewrites withflush()+executemanyinstead of per-row commitsqlite3.OperationalErrorfunctions_to_optimize_by_namedict for O(1) fallback lookup inprocess_test_filesall_defsfromall_namesviais_definition()to save a redundant Jedi callThanks to @aseembits93 for the original work in #1846 — this PR carries forward the pieces that are still applicable after the #1660 architecture changes.
Test plan
pytest tests/test_code_context_extractor.py— 104 passedpytest tests/test_unit_test_discovery.py— 57 passed