Skip to content

perf(analyze): vectorize sampled betweenness with numpy, same bits - #5670

Open
Max17190 wants to merge 1 commit into
Graphify-Labs:v8from
Max17190:perf/vectorized-betweenness
Open

Max17190 wants to merge 1 commit into
Graphify-Labs:v8from
Max17190:perf/vectorized-betweenness

Conversation

@Max17190

@Max17190 Max17190 commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Why

suggest_questions computes sampled betweenness on every rebuild. The scalar loop takes 45s on Kubernetes and 68s on VS Code. Fixes #5667.

Summary

Each source's breadth first search now runs one level at a time with numpy over a CSR copy of the adjacency. Every floating point sum keeps networkx's order, so the scores match nx.betweenness_centrality to the last bit. Graphs with narrow levels fall back to the existing scalar loop.

_betweenness_centrality with k=100 v8 This PR
Django, 55k nodes 5.0s 0.63s
Kubernetes, 472k nodes 44.6s 6.0s
VS Code, 315k nodes 67.6s 6.6s

Each number is the median of 3 runs on a 16 vCPU Linux machine with CPython 3.12 and numpy 1.26. Cold builds match v8 byte for byte.

Limitations: exact scores rely on np.bincount adding in input order. numpy does this but does not document it, so the tests pin it. The speedup is smaller before numpy 1.25, and a 50 node graph takes 1.8ms instead of 1.1ms.

Test plan

python -m pytest tests -q -n 8 --dist loadfile   # 12671 passed
python -m pytest tests/test_analyze.py -k "betweenness or descending" -q   # 46 passed on numpy 1.26.4 and 2.5.5
uv run ruff check .                              # passes
uv run pyright --pythonpath .venv/bin/python     # same diagnostics as v8
uv run python -m tools.skillgen --check          # 134 artifacts match
graphify update .   # cold on Django and Kubernetes, and incremental on Django; identical to v8
  • Read CONTRIBUTING.md
  • AI help disclosed in the commit trailer

suggest_questions computes betweenness with 100 sampled sources on
every full rebuild and every topology-changing update. The scalar
Brandes loop spends most of that time in Python bookkeeping per
neighbor entry: about 45s on a 470k node graph and 67s on a 315k node
graph.

Run each source's search one breadth-first level at a time over a CSR
copy of the adjacency instead. Every float sum is still taken in the
order networkx takes it, so the scores stay bit-identical: the next
level comes out in queue order by keeping each node's first appearance
in the level's expansion, path counts are summed with np.bincount in
expansion order, and dependencies are summed per predecessor in reverse
queue order of the successors after a radix sort of the arcs. Scores
are added per source in source order and rescaled exactly as before.

numpy only pays off when levels are wide. Each level costs a few dozen
numpy calls, so chains, deep trees and tiny graphs would get slower;
once the levels seen average fewer than 128 neighbor entries, the
remaining sources go through the unchanged scalar loop, which adds into
the same scores.

Fixes Graphify-Labs#5667

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Max17190
Max17190 requested a review from safishamsi as a code owner December 1, 2026 04:45
@github-actions

github-actions Bot commented Dec 1, 2026

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 Dec 1, 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 review View in Graphify

Looks safe to merge · Merge confidence 5/5

No issues reported · High impact

The betweenness calculation now uses a NumPy breadth-first-level path for wider searches and keeps the scalar path for searches that become narrow.

Code graph · 1217 functions depend on this change
  • 68 of 665 test files are affected by this change.
  • Gate, test list and every flagged function: in the Graphify check.

Reviewed f7e883e

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.

Sampled betweenness in suggest_questions takes 45 to 70s on large graphs

1 participant