Skip to content

perf(analyze): speed up suggest_questions betweenness, same scores - #5303

Open
Max17190 wants to merge 2 commits into
Graphify-Labs:v8from
Max17190:perf/faster-bridge-betweenness
Open

Max17190 wants to merge 2 commits into
Graphify-Labs:v8from
Max17190:perf/faster-bridge-betweenness

Conversation

@Max17190

@Max17190 Max17190 commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Why

suggest_questions runs nx.betweenness_centrality to pick the top 3 bridge nodes for the report. Once a graph has more than 1000 nodes it samples 100 sources, but each search still reaches 96 to 100% of the graph, and networkx keeps that search state in dicts keyed by node id. Every full build runs this step, and so does every graphify update that finds a changed file. On Guava it takes a third of a one-file update.

one-file graphify update v8 this PR suggest_questions on v8 this PR
Django, 55k nodes 30.6s 25.1s 7.75s 2.29s
Guava, 84k nodes 51.9s 38.6s 18.2s 4.59s

Median of 3 runs on an Apple M5 with Python 3.13 and networkx 3.7. In one full build each, the step went from 6.4s to 2.7s on Django and from 14.1s to 4.7s on Guava.

Summary

  • _betweenness_centrality numbers the nodes once and keeps distances, path counts and predecessor lists in lists. After each source it resets only the entries that source reached.
  • It follows networkx's search order, summation order, sampling and rescale, so every score matches nx.betweenness_centrality(G, k=k, seed=42) bit for bit on networkx 3.5 and later. The lockfile has 3.6.1 and 3.7.
  • pyproject.toml still allows networkx 3.4, which rescaled sampled scores differently. There it returns networkx's own result, so the report matches v8 on every supported networkx. Without that, axum's report on 3.4.2 printed one bridge score as 0.108 instead of 0.107.
  • No new dependency. The output files do not change, so there is no cache schema bump.

Test plan

uv run pytest tests/test_analyze.py -q                  # 77 passed, 12 of them new, on Python 3.11 to 3.14 and on networkx 3.4.2
uv run pytest tests/ -q                                 # 11591 passed, 134 skipped, 1 xfailed on Python 3.11 (networkx 3.6.1) and 3.13 (networkx 3.7)
uv run ruff check .                                     # passes
uv run pyright graphify/analyze.py tests/test_analyze.py  # the same 2 errors as v8
uv run python -m tools.skillgen --check                 # 134 artifacts match

The new tests compare every score and the key order with networkx. They cover empty, one-node, path, star, cycle and complete graphs, random directed and undirected graphs with self-loops and isolated nodes, and sampled runs up to k=100 on 1500 nodes. On networkx 3.4.2 the three sampled cases fail without the 3.4 fallback.

End to end on gin, ripgrep, axum, clap, excalidraw, Django and Guava, I ran a full build, then a one-file edit and graphify update, on v8 and on this branch at the same path. graph.json, graph.html and GRAPH_REPORT.md came out byte-identical every time. manifest.json and the build state differ only in timestamps. On networkx 3.4.2, the suggested questions match v8 on all seven graphs.

  • Read CONTRIBUTING.md
  • No generated skill files changed
  • Extraction unchanged and deterministic
  • No shell or Python interpolation, secrets, or local graph data
  • AI help disclosed in the commit trailer

suggest_questions runs nx.betweenness_centrality to pick the top 3 bridge
nodes. On a graph over 1000 nodes it samples 100 sources, but each search
still reaches nearly the whole graph, with the search state in dicts keyed
by node id. On Guava's 84k-node graph that was 18.2s of a 51.9s one-file
update.

_betweenness_centrality numbers the nodes once and keeps the same state in
lists, resetting only the entries a source reached. It follows networkx's
search order, summation order and rescale, so the scores match networkx 3.5
and later bit for bit. The step drops to 4.6s on Guava, and from 7.8s to
2.3s on Django.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Max17190
Max17190 requested a review from safishamsi as a code owner November 18, 2026 06:10
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @Max17190. 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.11 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 Nov 18, 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 code review View in Graphify

Review completed · 5eac2f3e8817

Tip

Merge confidence: 5/5 · Looks safe to merge · 🟠 High impact
No issues reported
1 not re-checked

The added tests compare the helper’s centrality scores with NetworkX across small, sampled, directed, and disconnected graphs.

Full summary

The added tests compare the helper’s centrality scores with NetworkX across small, sampled, directed, and disconnected graphs. They also include a graph larger than 1,000 nodes to cover the sampling size used by suggest_questions.

Code graph and static checks · 1101 functions depend on this change

Impact & health

Impact — 1101 functions depend on the 138 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: suggest_questions() — 22 callers, 6 callees

Formal checks — 1101 functions this change can affect weren't formally checked in this review. Formal checks are informational only.

Gate & verification

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

Advisory (not blocking):

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

Test selection

61 of 593 test file(s) selected (10%) via static blast radius.

  • tests/test_analyze.py — impact, changed-test
  • tests/test_atomic_canvas_export.py — impact
  • tests/test_atomic_writes.py — impact
  • tests/test_build.py — impact
  • tests/test_build_merge_ghost_fold_4955.py — impact
  • tests/test_build_merge_hyperedges_and_prune.py — impact
  • tests/test_carried_hyperedge_remap.py — impact
  • tests/test_chunking.py — impact
  • tests/test_cli_export.py — impact
  • tests/test_community_labels_skill.py — impact
  • tests/test_compact_graph_json.py — impact
  • tests/test_confidence.py — impact
  • tests/test_confidence_score_output.py — impact
  • tests/test_cpp_forward_declarations.py — impact
  • tests/test_cpp_member_declaration_identity.py — impact
  • tests/test_cross_extension_reexport_self_cycle.py — impact
  • tests/test_csharp_method_owner_labels.py — impact
  • tests/test_dedup_shrink_refuses_force_write.py — impact
  • tests/test_export.py — impact
  • tests/test_export_control_characters.py — impact
  • tests/test_export_direction.py — impact
  • tests/test_export_idempotent_writes.py — impact
  • tests/test_export_path_length.py — impact
  • tests/test_extract.py — impact
  • tests/test_extract_memory_release.py — impact
  • tests/test_falkordb_integration.py — impact
  • tests/test_go_qualified_resolution.py — impact
  • tests/test_god_nodes_exclude_hubs.py — impact
  • tests/test_graphdb_push_indexes.py — impact
  • tests/test_html_script_breakout.py — impact
  • tests/test_html_theme.py — impact
  • tests/test_hyperedge_roundtrip.py — impact
  • tests/test_hypergraph.py — impact
  • tests/test_import_cycles_shortest_first.py — impact
  • tests/test_js_import_resolution.py — impact
  • tests/test_js_member_collisions.py — impact
  • tests/test_labeling.py — impact
  • tests/test_lost_saved_semantic_nodes.py — impact
  • tests/test_obsidian_dangling_member.py — impact
  • tests/test_obsidian_filename_cap.py — impact
  • tests/test_obsidian_unicode_tags.py — impact
  • tests/test_obsidian_vault_migration.py — impact
  • tests/test_pipeline.py — impact
  • tests/test_python_import_resolution.py — impact
  • tests/test_reflect.py — impact
  • tests/test_report.py — impact
  • tests/test_report_gap_thresholds.py — impact
  • tests/test_semantic_origin_stamp.py — impact
  • tests/test_semantic_similarity.py — impact
  • tests/test_serve.py — impact
  • … and 11 more

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.

Every flagged function

  • suggest_questions() in graphify/analyze.py:558 · 22 callers, 6 callees: fans out to 6 callees (efferent coupling); 22 callers depend on it (afferent coupling)

networkx 3.4, which pyproject.toml still allows, rescales sampled
betweenness scores differently from 3.5 and later. The fast path follows
3.5, so on 3.4 a report could print a different bridge score: 0.108
instead of 0.107 on axum. On networkx before 3.5, _betweenness_centrality
now returns networkx's own result, so the report matches v8 on every
supported version, and the tests no longer skip the sampled cases there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

1 participant