Repository navigation
perf(csr): read a closing edge from its shorter end - #228
Merged
Merged
Conversation
A closing edge (a hop whose target is already bound, as in the last hop of a triangle) walked every neighbour of its source and compared each to the bound id, so a hub source cost its whole degree per row. The shape suite measured the cycle growing 19x from hub degree 10 to 100,000, and 6x slower than the join path at the top. Both directions of each relationship's CSR exist and share edge rows, so each row now reads the shorter of its two ends' lists: the source's outgoing list or the target's incoming one (for an undirected hop, the target's own two lists). Rows, edge rows and filters are unchanged. A hub at both ends still costs its degree; that needs another approach.
A control build showed the shorter-end read itself costs nothing, but editing the shared loop slowed suite queries without closing edges by up to 8% through code generation alone. A const generic gives closing edges their own copy, and the common copy folds back to the previous loop.
Comparing the two ends costs two lookups per row, more than scanning a source with a few edges. Graph-benchmark-ldbc q30, the one suite query with a closing edge, has low-degree ends and came out 6% slower in both modes.
… list Each CSR base records its largest degree when built. A closing edge whose adjacency has no source above the scan cutoff (postHasCreator: one creator per post) now scans every row without the per-row degree lookup, which still cost graph-benchmark-ldbc q30 5%. Appended edges make the bound unknown, and rows are then compared as before.
5 of 16 tasks
Deploying lance-csr with
|
| Latest commit: |
602d5ae
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://cd4d39ee.lance-csr.pages.dev |
| Branch Preview URL: | https://perf-hub-safe-closing-edge.lance-csr.pages.dev |
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.
Part of #223.
Why
A query that looks for a cycle, such as a triangle, ends with a closing edge: a hop whose target an earlier part of the pattern has already bound. To check that edge,
CsrExpandExecwalked the source node's whole neighbour list looking for the target. That is cheap for an ordinary node, but when the source is a hub with 100,000 neighbours, every row walks all 100,000.Cycles through high-degree nodes, such as an exchange account or a domain controller, come up often in investigative queries. On
main, the shape suite's triangle query got 16 times slower in memory mode (10 times in lazy mode) as the hub's degree rose from 10 to 100,000. At that degree it was 5.7 times slower than the same query withuse_csr=False, so the CSR path was losing to the join path it is meant to replace.What changed
postHasCreator, one creator per post) skips the comparison and scans, as before. Appended edges make that bound unknown, and rows are then compared individually.ROWSswitch, compiles one copy for closing edges and one for every other hop. In the other copy the new code folds away, leaving the previous loop. Without this, editing the shared loop slowed queries that have no closing edge (see the control build below).A hub at both ends (the bound target also has a huge in-degree) still costs that degree, so the #223 item "A closing edge costs the same whatever the degree of its endpoints" stays open: the shape suite's both-ends headline didn't move (23×). That needs a per-query hash set of a hub's neighbours, a separate change.
Validation
cargo test --workspace,cargo clippy --workspace --all-targets -- -D warnings,make test(1,814 passed), the strict docs build andtypospass locally. A new unit test checks that reading from either end returns the same rows and edge rows, for a directed hop with parallel edges, a missing target and a reversed pair, and for an undirected hop, and that the largest-degree bound is exact and becomes unknown once edges are appended. CI has not run yet.CONTRIBUTING.md: release wheels frommain(1d01a8d) and this branch (602d5ae), alternatingmainand branch twice each,memoryandlazy. Every answer check and every shape-suite phrasing and plan check passed.Shape suite headlines (
smallgraph):The hub-cycle queries moved as intended: degree 10,000 by −67% (memory) and −54% (lazy), degree 100,000 by −94% and −90%. Two lazy queries that don't run this code moved in one branch run each and matched
mainin the other:optional/match(0.76 and 1.13 ms against 0.75 ms) andcycle_join_10000, which runs withuse_csr=False(3.66 and 5.22 ms against 3.69 and 3.89 ms). Neither moved in two earlier rounds. Themainrun-to-run geomean was 1.004 (memory) and 0.995 (lazy).SF1 suites:
mainmainrun 2 / run 1No SF1 query moved by 10% or more except graph-benchmark-ldbc q20 in memory (−15%, in a suite and mode whose two
mainruns differed by up to 31% on one query). q30, the only suite query with a closing edge, is +3% (memory) and +1% (lazy).How we got here. The first version changed the shared loop and compared degrees on every closing-edge row. It fixed the hub cycle, but SF1 came out 1 to 7% slower, mostly on queries with no closing edge. A control wheel (that version with the shorter-end read disabled), alternated with
mainand the branch, showed the read itself costs nothing on those queries (branch against control 0.97 to 1.02), while control againstmainwas up to 1.08: the edit changed how the shared loop compiled. Giving closing edges their own loop copy removed that. q30 then remained 5 to 6% slower in both modes, which the control also pointed to (branch against control 1.07 there): its closing edge starts at a post, whose one creator makes the per-row degree lookup pure overhead. The largest-degree bound removed that.Machine: Apple M5 MacBook Pro, 24 GB, macOS 26.6.2. Load average 2.7 to 8.2 (the high end at the start, right after the release build).
Every SF1 query
graph-benchmark, memory
Geomean branch/main: 0.993; main run 2 / run 1: 0.998 (largest single-query main gap 2%)
graph-benchmark-ldbc, memory
Geomean branch/main: 0.994; main run 2 / run 1: 1.004 (largest single-query main gap 31%)
graph-benchmark-ldbc, lazy
Geomean branch/main: 1.005; main run 2 / run 1: 0.999 (largest single-query main gap 2%)
graph-benchmark-ldbc-complex, memory
Geomean branch/main: 0.995; main run 2 / run 1: 0.995 (largest single-query main gap 3%)
graph-benchmark-ldbc-complex, lazy
Geomean branch/main: 1.003; main run 2 / run 1: 1.002 (largest single-query main gap 5%)
Every shape-suite query
memory: small graph (20,000 accounts, 229,517 T edges, hub degrees [10, 1000, 10000, 100000])
Runs: main x2, branch x2.
main run-to-run geomean: 1.004; largest single-query gap 6%.
lazy: small graph (20,000 accounts, 229,517 T edges, hub degrees [10, 1000, 10000, 100000])
Runs: main x2, branch x2.
main run-to-run geomean: 0.995; largest single-query gap 12%.
🤖 Generated with Claude Code