Skip to content

perf(resolve): index reference edges by source (#4909) - #4913

Closed
Deepjyoti-Sarmah wants to merge 2 commits into
Graphify-Labs:v8from
Deepjyoti-Sarmah:fix/4909-reference-index
Closed

Deepjyoti-Sarmah wants to merge 2 commits into
Graphify-Labs:v8from
Deepjyoti-Sarmah:fix/4909-reference-index

Conversation

@Deepjyoti-Sarmah

Copy link
Copy Markdown
Contributor

What does this PR do?

Refs #4909.

The current full rebuild is O(imports times edges) in _apply_symbol_resolution_facts. On a 21k-node TypeScript corpus it takes 14 minutes. The pass added for #4854 loops over every edge for every import so it can rewrite a reference edge from a sourceless stub to the imported declaration.

The index is keyed by the same _js_source_path(...) value the old comparison computed. The per-edge checks stay the same:

  • relation must be references
  • target must be a sourceless stub
  • stub label must match the imported or local name

No graph schema, cache schema, or manifest field changes. The written graph is identical on the 200-file fixture.

This does not close the rest of #4909. _collect_js_symbol_resolution_facts still re-parses every JS/TS tree, and the original 0.9.48 profile has other costs. This removes the current regression that makes a full rebuild worse than the reported version.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Tests or CI
  • Refactor
  • Security fix

Evidence

Before: current v8 at a0946f96.

200 files / 3,200 nodes:     25.3 s
1,312 files / 20,992 nodes: 847.0 s  (14 m 7 s)

After: branch fix/4909-reference-index.

200 files / 3,200 nodes:      4.24 s
1,312 files / 20,992 nodes:  24.32 s

Speedup on the 21k corpus: about 34.8x.

Scaling:

files:   200 -> 1,312      6.56x
before:  25.3 -> 847.0     33.5x
after:    4.24 -> 24.32     5.7x

The before path is quadratic. The after path is close to linear.

Failing test before the fix:

464 path lookups for 8 imports and 40 edges; linear budget is 416

After:

1 passed

The test counts _js_source_path calls and requires a linear budget. It does not measure wall time, so it will not flake on a busy CI machine.

Graph equality on the 200-file corpus:

nodes: 3,200 both
links: 5,400 both
node ids equal: yes
node field diffs: 0
edge set equal: yes
missing edges: 0
extra edges: 0

Verification & Invariants

  • Read the CONTRIBUTING.md guide.
  • Reproduced the issue and identified the invariant.
  • Made the smallest fix necessary.
  • Added a regression test (if bug fix) or isolated boundary test.
  • Kept the PR description synchronized with the final implementation.
  • Documented any limitations / unsupported cases explicitly.

The invariant: the cost of reference binding scales with nodes, edges, and imports, not with imports times edges. A per-import full scan fails the new test.

Limitation: the index holds reference edges grouped by source file for the duration of this pass. That is one list per file, not a second graph. Memory is proportional to the reference edge count.

How was this tested?

.venv/bin/python -m ruff check graphify/extractors/resolution.py \
  tests/test_4909_reference_binding_scale.py

.venv/bin/python -m pytest tests/test_4909_reference_binding_scale.py \
  tests/test_file_id_collision_4854.py tests/test_watch.py \
  tests/test_extract.py tests/test_build.py -q

.venv/bin/python -m pytest -q

Results:

ruff: All checks passed

targeted: 745 passed, 1 skipped, 1 xfailed in 23.62s

full: 9191 passed, 18 skipped, 1 xfailed in 608.40s

Merge Danger

Door: Two-way.

The change is local to one resolution pass and can be reverted without a graph migration. No schema bump and no cache invalidation.

Blast Radius: JS/TS reference binding.

The main risk is a subtle change in which edges are rewritten. The index uses the same source path and the same checks as the old loop. The 200-file graph comparison found identical nodes, fields, and edges, and all #4854 tests pass. A revert restores the old behavior and the old cost.

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments. No source fragments changed.
  • I confirmed that AST/structural extraction remains deterministic (no ambient state dependencies like ENV variables).
  • I reviewed changes for security implications (no unsafe interpolation into shell/Python).
  • I confirmed no API keys or local-only graph data are included.
  • (If applicable) I disclosed AI authorship in my commit messages.

The Graphify-Labs#4854 stub-binding pass scanned every edge once per import fact. That made a full rebuild O(imports x edges): 14m7s on a 21k-node TS corpus, versus 26.8s on 0.9.48. Group reference edges by their emitting source file once, then scan only that file's edges per import. The 21k corpus now rebuilds in 25.5s, and the written graph is identical.
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @Deepjyoti-Sarmah. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.10 and 3.13, and that the change keeps extraction deterministic.

@github-actions github-actions Bot added the contributor Pull request from a returning community contributor label Oct 31, 2026

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Graphify reviewed this change.

No issues found by static checks — no coupling regressions or blocking issues in the code graph. That is not the same as safe to merge: see what was not checked.

Not checked: tests were not run; formal verification proved 0 of 1 changed function(s) (1 not verified).

Formal verification. PR-changed functions: 0/1 verified (0 proven, 0 may-equivalent, 0 distinguished) · 1 not verified (1 unsupported).

Not verified on this run: \_apply\_symbol\_resolution\_facts (unsupported).


Graphify review — findings

Indexes references edges by their resolved source file once, so binding a reference to its imported declaration no longer rescans the whole edge list for every import fact. This takes the pass from O(imports × edges) to linear in graph size on full rebuilds, with the same rewrite behaviour. A new test pins this by counting _js_source_path calls against a linear budget rather than timing anything.

No blocking issues surfaced.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 3353 functions depend on the 172 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: _apply_symbol_resolution_facts() — 6 callers, 12 callees

Verification — 3353 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 1297 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

35 of 480 test file(s) selected (7%) via static blast radius.

  • tests/test_4909_reference_binding_scale.py — impact, changed-test
  • tests/test_astro_extraction.py — impact
  • tests/test_build.py — impact
  • tests/test_cjs_module_extension.py — impact
  • tests/test_ejs_extractor.py — impact
  • tests/test_ext_tagged_id_clash.py — impact
  • tests/test_extract.py — impact
  • tests/test_forwarding_review_findings.py — impact
  • tests/test_import_extension_resolution.py — impact
  • tests/test_indirect_call_block_scoped_shadow.py — impact
  • tests/test_js_exported_scalar_bindings.py — impact
  • tests/test_js_member_collisions.py — impact
  • tests/test_js_walk_order.py — impact
  • tests/test_language_override_imports.py — impact
  • tests/test_languages.py — impact
  • tests/test_loose_sibling_import_resolution.py — impact
  • tests/test_multilang.py — impact
  • tests/test_package_json_subpath_imports.py — impact
  • tests/test_pascal.py — impact
  • tests/test_pascal_resolution.py — impact
  • tests/test_phantom_external_import.py — impact
  • tests/test_pr10_resolution_overrides.py — impact
  • tests/test_python_import_resolution.py — impact
  • tests/test_python_member_call_language_gate.py — impact
  • tests/test_python_parse_memoization.py — impact
  • tests/test_rationale.py — impact
  • tests/test_resolve_memoization.py — impact
  • tests/test_source_key_memoization.py — impact
  • tests/test_src_layout_import_resolution.py — impact
  • tests/test_svelte_extraction.py — impact
  • tests/test_ts_new_expression_calls.py — impact
  • tests/test_typescript_module_extensions.py — impact
  • tests/test_unmapped_at_alias_resolution.py — impact
  • tests/test_vue_extraction.py — impact
  • tests/test_walk_python_tree_iterative.py — impact

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

Formal verification

Could not verify: Could not verify \_apply\_symbol\_resolution\_facts.

The verifier did not have enough to check \_apply\_symbol\_resolution\_facts, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: parameter `facts` is annotated `_SymbolResolutionFacts` — outside the synthesizable primitive/collection set

· 1 more finding(s) on lines outside this diff (see the check run).

@safishamsi

Copy link
Copy Markdown
Member

Landed in v0.9.100, now on PyPI (pip install graphifyy==0.9.100). Reference edges are indexed by source, so JS/TS import resolution no longer re-walks every edge. Thanks for the fix, @Deepjyoti-Sarmah.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contributor Pull request from a returning community contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants