Skip to content

perf(csr): read a closing edge from its shorter end - #228

Merged
prrao87 merged 4 commits into
mainfrom
perf/hub-safe-closing-edge
Nov 28, 2026
Merged

prrao87 merged 4 commits into
mainfrom
perf/hub-safe-closing-edge

Conversation

@prrao87

@prrao87 prrao87 commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

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, CsrExpandExec walked 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 with use_csr=False, so the CSR path was losing to the join path it is meant to replace.

What changed

  • A closing edge reads the shorter of its two ends' lists. Both directions of every relationship's CSR already exist and share one edge-row table, so the edges between two bound nodes appear in the source's outgoing list and in the target's incoming list, with the same edge rows. When the source's list is long (over 32 edges), a row compares the two degrees and walks the shorter list. An undirected hop does the same with the target's own two lists. Rows, edge rows, relationship filters and target lookups are unchanged; only which list is walked differs.
  • No cost where it can't help. Each CSR base now records its largest degree when it is built. A closing edge over an adjacency with no long list anywhere (for example postHasCreator, one creator per post) skips the comparison and scans, as before. Appended edges make that bound unknown, and rows are then compared individually.
  • Closing edges get their own copy of the expand loop. A const generic, like the loop's existing ROWS switch, 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).
  • The operator docs say how a closing edge reads its lists.

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 and typos pass 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.
  • All three suites, per CONTRIBUTING.md: release wheels from main (1d01a8d) and this branch (602d5ae), alternating main and branch twice each, memory and lazy. Every answer check and every shape-suite phrasing and plan check passed.

Shape suite headlines (small graph):

Group Headline Ideal memory: main memory: branch lazy: main lazy: branch
phrasing slowest / fastest of 8 phrasings 1 2.86x 2.86x 7.08x 7.17x
selectivity two filters: slowest / fastest end 1 14.65x 14.31x 11.79x 11.56x
selectivity hop order: slowest / fastest 1 1.52x 1.56x 1.42x 1.44x
count_rows slowest count / fastest rows <= 1 7.23x 7.13x 6.51x 6.52x
hubs cycle: degree 100000 / degree 10 1 16.31x 0.99x 9.91x 0.98x
hubs cycle, hub at both ends: degree 100000 / 10 1 23.57x 23.21x 15.89x 16.25x
hubs pattern predicate: degree 100000 / 10 1 71.67x 69.01x 29.36x 30.29x
hubs cycle at degree 100000: CSR / join <= 1 5.73x 0.35x 3.61x 0.36x
hubs predicate at degree 100000: CSR / join <= 1 3.71x 3.62x 3.00x 2.94x
optional filtered / unfiltered OPTIONAL MATCH <= 1 3.47x 3.47x 26.09x 25.02x
optional filtered OPTIONAL MATCH / filtered MATCH about 1 3.13x 3.17x 16.21x 14.79x
wide_columns cross_filter: notes / ids about 1 4.13x 4.15x 16.64x 16.81x
wide_columns top_k: notes / ids about 1 2.87x 2.92x 13.21x 13.66x
wide_columns far_filter: notes / ids about 1 1.36x 1.39x 6.38x 6.42x
wide_columns all: notes / ids > 1 4.32x 4.30x 16.52x 16.85x
early_exit LIMIT 10 / all rows, ids << 1 0.36x 0.36x 0.41x 0.41x
early_exit LIMIT 10 / all rows, notes << 1 0.63x 0.62x 0.85x 0.82x

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 main in the other: optional/match (0.76 and 1.13 ms against 0.75 ms) and cycle_join_10000, which runs with use_csr=False (3.66 and 5.22 ms against 3.69 and 3.89 ms). Neither moved in two earlier rounds. The main run-to-run geomean was 1.004 (memory) and 0.995 (lazy).

SF1 suites:

Suite Mode Geomean branch / main main run 2 / run 1
graph-benchmark memory 0.993 0.998
graph-benchmark-ldbc memory 0.994 1.004
graph-benchmark-ldbc lazy 1.005 0.999
graph-benchmark-ldbc-complex memory 0.995 0.995
graph-benchmark-ldbc-complex lazy 1.003 1.002

No SF1 query moved by 10% or more except graph-benchmark-ldbc q20 in memory (−15%, in a suite and mode whose two main runs 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 main and the branch, showed the read itself costs nothing on those queries (branch against control 0.97 to 1.02), while control against main was 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

Query main (ms) branch (ms) Change
query1 7.62 7.40 -3%
query2 12.33 12.13 -2%
query3 2.74 2.73 -0%
query4 1.60 1.64 +2%
query5 1.63 1.63 -0%
query6 1.89 1.90 +1%
query7 3.67 3.70 +1%
query8 13.26 12.84 -3%
query9 12.58 12.37 -2%

Geomean branch/main: 0.993; main run 2 / run 1: 0.998 (largest single-query main gap 2%)

graph-benchmark-ldbc, memory

Query main (ms) branch (ms) Change
query1 0.69 0.66 -4%
query2 0.57 0.57 -0%
query3 0.68 0.68 -0%
query4 0.54 0.54 -0%
query5 0.83 0.83 -1%
query6 0.46 0.46 +1%
query7 1.09 1.12 +3%
query8 0.67 0.67 -0%
query9 0.89 0.87 -2%
query10 0.76 0.76 -1%
query11 1.57 1.58 +1%
query12 0.79 0.79 -0%
query13 0.84 0.85 +1%
query14 0.56 0.57 +1%
query15 0.59 0.60 +1%
query16 0.71 0.71 +0%
query17 0.95 0.96 +1%
query18 0.74 0.74 +1%
query19 0.97 0.98 +1%
query20 2.44 2.07 -15%
query21 0.87 0.81 -7%
query22 0.77 0.76 -1%
query23 0.78 0.78 +0%
query24 0.73 0.74 +1%
query25 0.84 0.85 +1%
query26 0.79 0.79 +0%
query27 0.91 0.91 +0%
query28 1.43 1.43 -0%
query29 0.73 0.73 -1%
query30 19.12 19.79 +3%

Geomean branch/main: 0.994; main run 2 / run 1: 1.004 (largest single-query main gap 31%)

graph-benchmark-ldbc, lazy

Query main (ms) branch (ms) Change
query1 1.17 1.17 +0%
query2 1.43 1.43 -0%
query3 0.83 0.83 -1%
query4 0.82 0.81 -1%
query5 2.20 2.20 -0%
query6 0.76 0.76 -0%
query7 1.14 1.14 +0%
query8 1.75 1.75 -0%
query9 1.37 1.37 +0%
query10 0.96 0.97 +1%
query11 2.02 2.02 -0%
query12 6.68 6.76 +1%
query13 1.08 1.09 +1%
query14 0.72 0.72 +1%
query15 0.79 0.80 +1%
query16 1.13 1.14 +1%
query17 1.42 1.44 +1%
query18 1.17 1.18 +1%
query19 1.37 1.39 +2%
query20 16.69 16.74 +0%
query21 0.86 0.86 -0%
query22 0.96 0.97 +0%
query23 1.26 1.27 +1%
query24 1.09 1.10 +1%
query25 1.22 1.23 +1%
query26 1.09 1.10 +1%
query27 1.33 1.35 +1%
query28 2.08 2.07 -0%
query29 1.06 1.06 +0%
query30 19.52 19.77 +1%

Geomean branch/main: 1.005; main run 2 / run 1: 0.999 (largest single-query main gap 2%)

graph-benchmark-ldbc-complex, memory

Query main (ms) branch (ms) Change
query1 24.77 24.84 +0%
query2 2.54 2.53 -0%
query3 25.77 25.37 -2%
query4 5.90 5.94 +1%
query5 72.10 70.15 -3%
query6 21.19 21.23 +0%
query7 10.05 9.92 -1%
query8 2.41 2.39 -1%
query9 37.02 36.99 -0%
query10 20.88 20.47 -2%
query11 3.08 3.05 -1%
query12 16.42 16.74 +2%
query13 1.17 1.14 -2%
query14 245.82 247.88 +1%

Geomean branch/main: 0.995; main run 2 / run 1: 0.995 (largest single-query main gap 3%)

graph-benchmark-ldbc-complex, lazy

Query main (ms) branch (ms) Change
query1 35.06 34.68 -1%
query2 25.35 25.55 +1%
query3 164.34 167.31 +2%
query4 12.96 13.09 +1%
query5 96.56 94.62 -2%
query6 23.01 22.92 -0%
query7 13.86 13.87 +0%
query8 3.40 3.41 +0%
query9 226.26 229.20 +1%
query10 29.06 29.01 -0%
query11 5.43 5.47 +1%
query12 19.19 19.44 +1%
query13 1.43 1.43 +0%
query14 259.08 258.29 -0%

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.

Group Headline Ideal main branch
phrasing slowest / fastest of 8 phrasings 1 2.86x 2.86x
selectivity two filters: slowest / fastest end 1 14.65x 14.31x
selectivity hop order: slowest / fastest 1 1.52x 1.56x
count_rows slowest count / fastest rows <= 1 7.23x 7.13x
hubs cycle: degree 100000 / degree 10 1 16.31x 0.99x
hubs cycle, hub at both ends: degree 100000 / 10 1 23.57x 23.21x
hubs pattern predicate: degree 100000 / 10 1 71.67x 69.01x
hubs cycle at degree 100000: CSR / join <= 1 5.73x 0.35x
hubs predicate at degree 100000: CSR / join <= 1 3.71x 3.62x
optional filtered / unfiltered OPTIONAL MATCH <= 1 3.47x 3.47x
optional filtered OPTIONAL MATCH / filtered MATCH about 1 3.13x 3.17x
wide_columns cross_filter: notes / ids about 1 4.13x 4.15x
wide_columns top_k: notes / ids about 1 2.87x 2.92x
wide_columns far_filter: notes / ids about 1 1.36x 1.39x
wide_columns all: notes / ids > 1 4.32x 4.30x
early_exit LIMIT 10 / all rows, ids << 1 0.36x 0.36x
early_exit LIMIT 10 / all rows, notes << 1 0.63x 0.62x
Query main ms branch ms Change main peak MiB branch peak MiB
phrasing/unnamed_start_inline 0.51 0.51 +0% 1.7 1.8
phrasing/unnamed_start_where 0.52 0.52 -0% 1.6 1.8
phrasing/unnamed_end_inline 0.51 0.51 -1% 1.6 1.8
phrasing/unnamed_end_where 0.52 0.52 -1% 1.6 1.7
phrasing/named_start_inline 1.46 1.41 -4% 11.1 11.3
phrasing/named_start_where 1.44 1.45 +1% 10.4 10.5
phrasing/named_end_inline 1.42 1.42 -1% 11.0 10.5
phrasing/named_end_where 1.45 1.43 -2% 10.2 10.5
selectivity/rows_fwd 11.05 10.82 -2% 2.7 2.8
selectivity/rows_rev 0.75 0.76 +0% 2.0 2.1
selectivity/hop_cheap_first 0.98 0.97 -1% 2.1 2.3
selectivity/hop_hub_first 1.49 1.51 +2% 2.3 2.3
count_rows/count_fwd 5.45 5.39 -1% 3.3 3.2
count_rows/count_rev 5.44 5.38 -1% 3.4 3.4
hubs/cycle_10 1.12 1.13 +1% 2.0 2.2
hubs/cycle_join_10 1.50 1.53 +2% 2.5 2.7
hubs/both_hubs_10 1.07 1.06 -1% 2.0 2.1
hubs/predicate_10 0.58 0.59 +1% 1.7 1.8
hubs/predicate_join_10 2.91 2.90 -0% 4.2 4.2
hubs/cycle_1000 1.24 1.12 -10% 2.0 2.3
hubs/cycle_join_1000 1.51 1.50 -1% 2.5 2.6
hubs/both_hubs_1000 1.27 1.27 -0% 2.0 2.1
hubs/predicate_1000 0.90 0.90 +0% 1.7 1.8
hubs/predicate_join_1000 3.00 2.98 -1% 4.7 4.6
hubs/cycle_10000 3.39 1.13 -67% 2.0 2.2
hubs/cycle_join_10000 1.81 1.80 -1% 3.4 3.2
hubs/both_hubs_10000 3.42 3.40 -0% 2.0 2.1
hubs/predicate_10000 5.14 5.18 +1% 1.7 1.8
hubs/predicate_join_10000 4.01 4.04 +1% 9.8 9.4
hubs/cycle_100000 18.32 1.12 -94% 2.0 2.2
hubs/cycle_join_100000 3.20 3.20 +0% 11.1 11.6
hubs/both_hubs_100000 25.26 24.65 -2% 1.9 2.1
hubs/predicate_100000 41.77 40.62 -3% 1.8 1.8
hubs/predicate_join_100000 11.26 11.22 -0% 52.3 50.8
optional/match 0.59 0.60 +2% 2.3 2.5
optional/optional 0.65 0.66 +1% 2.3 2.5
optional/optional_where 2.26 2.29 +1% 6.9 7.2
optional/match_where 0.72 0.72 -0% 2.3 2.5
wide_columns/far_filter_ids 2.18 2.16 -1% 2.9 3.1
wide_columns/far_filter_notes 2.97 3.00 +1% 27.3 25.7
wide_columns/cross_filter_ids 1.16 1.16 +0% 4.0 3.6
wide_columns/cross_filter_notes 4.79 4.82 +1% 122.5 127.7
wide_columns/top_k_ids 1.36 1.35 -0% 3.2 3.5
wide_columns/top_k_notes 3.90 3.96 +1% 63.3 68.0
wide_columns/all_ids 1.15 1.14 -1% 2.8 4.1
wide_columns/all_notes 4.95 4.89 -1% 190.7 187.6
early_exit/hop_limit_ids 0.39 0.39 +0% 1.8 1.9
early_exit/hop_all_ids 1.08 1.10 +2% 2.4 2.0
early_exit/hop_limit_notes 0.55 0.55 +0% 3.5 3.7
early_exit/hop_all_notes 0.87 0.89 +2% 20.4 20.4
Index main MiB branch MiB main build ms branch build ms
account 19.0 17.2 20 20
ring 29.3 30.8 26 26

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.

Group Headline Ideal main branch
phrasing slowest / fastest of 8 phrasings 1 7.08x 7.17x
selectivity two filters: slowest / fastest end 1 11.79x 11.56x
selectivity hop order: slowest / fastest 1 1.42x 1.44x
count_rows slowest count / fastest rows <= 1 6.51x 6.52x
hubs cycle: degree 100000 / degree 10 1 9.91x 0.98x
hubs cycle, hub at both ends: degree 100000 / 10 1 15.89x 16.25x
hubs pattern predicate: degree 100000 / 10 1 29.36x 30.29x
hubs cycle at degree 100000: CSR / join <= 1 3.61x 0.36x
hubs predicate at degree 100000: CSR / join <= 1 3.00x 2.94x
optional filtered / unfiltered OPTIONAL MATCH <= 1 26.09x 25.02x
optional filtered OPTIONAL MATCH / filtered MATCH about 1 16.21x 14.79x
wide_columns cross_filter: notes / ids about 1 16.64x 16.81x
wide_columns top_k: notes / ids about 1 13.21x 13.66x
wide_columns far_filter: notes / ids about 1 6.38x 6.42x
wide_columns all: notes / ids > 1 16.52x 16.85x
early_exit LIMIT 10 / all rows, ids << 1 0.41x 0.41x
early_exit LIMIT 10 / all rows, notes << 1 0.85x 0.82x
Query main ms branch ms Change main peak MiB branch peak MiB
phrasing/unnamed_start_inline 0.66 0.65 -1% 2.2 2.1
phrasing/unnamed_start_where 0.66 0.65 -1% 2.1 2.2
phrasing/unnamed_end_inline 0.65 0.64 -2% 2.1 2.2
phrasing/unnamed_end_where 0.66 0.66 +1% 2.2 2.2
phrasing/named_start_inline 4.60 4.52 -2% 92.4 92.6
phrasing/named_start_where 4.59 4.58 -0% 92.1 92.5
phrasing/named_end_inline 4.52 4.54 +0% 91.8 92.5
phrasing/named_end_where 4.60 4.57 -1% 92.3 92.6
selectivity/rows_fwd 13.08 12.83 -2% 6.2 5.8
selectivity/rows_rev 1.11 1.11 +0% 3.5 3.4
selectivity/hop_cheap_first 1.30 1.29 -1% 2.6 2.8
selectivity/hop_hub_first 1.85 1.85 -0% 2.9 2.8
count_rows/count_fwd 7.22 7.23 +0% 7.5 7.9
count_rows/count_rev 7.23 7.16 -1% 6.9 7.8
hubs/cycle_10 2.03 2.03 +0% 3.9 3.8
hubs/cycle_join_10 3.37 3.37 -0% 5.5 6.0
hubs/both_hubs_10 1.58 1.57 -1% 2.8 2.5
hubs/predicate_10 1.42 1.39 -2% 3.5 3.9
hubs/predicate_join_10 5.71 5.70 -0% 12.1 16.7
hubs/cycle_1000 2.28 2.02 -11% 3.6 3.7
hubs/cycle_join_1000 3.38 3.41 +1% 5.5 5.6
hubs/both_hubs_1000 1.84 1.82 -1% 2.3 2.9
hubs/predicate_1000 1.85 1.81 -2% 2.5 3.5
hubs/predicate_join_1000 5.79 5.78 -0% 12.2 14.2
hubs/cycle_10000 4.41 2.03 -54% 3.7 3.9
hubs/cycle_join_10000 3.79 4.44 +17% 7.1 9.6
hubs/both_hubs_10000 3.93 3.96 +1% 2.5 3.2
hubs/predicate_10000 6.03 6.02 -0% 4.8 3.5
hubs/predicate_join_10000 6.57 6.48 -1% 22.7 19.3
hubs/cycle_100000 20.12 2.00 -90% 3.4 3.7
hubs/cycle_join_100000 5.58 5.61 +1% 24.0 24.0
hubs/both_hubs_100000 25.15 25.53 +2% 2.4 2.5
hubs/predicate_100000 41.84 42.16 +1% 3.0 3.9
hubs/predicate_join_100000 13.97 14.36 +3% 57.2 67.6
optional/match 0.75 0.95 +26% 2.8 4.2
optional/optional 0.81 0.81 -0% 2.9 3.0
optional/optional_where 21.13 20.19 -4% 24.9 27.5
optional/match_where 1.30 1.37 +5% 4.3 5.1
wide_columns/far_filter_ids 3.08 3.05 -1% 5.6 5.4
wide_columns/far_filter_notes 19.68 19.60 -0% 107.5 107.7
wide_columns/cross_filter_ids 1.44 1.45 +1% 3.9 4.0
wide_columns/cross_filter_notes 23.93 24.29 +2% 257.8 243.0
wide_columns/top_k_ids 1.67 1.69 +1% 3.8 4.0
wide_columns/top_k_notes 22.13 23.03 +4% 228.9 205.6
wide_columns/all_ids 1.42 1.43 +1% 5.5 5.6
wide_columns/all_notes 23.40 24.12 +3% 286.0 287.5
early_exit/hop_limit_ids 0.49 0.49 -0% 2.4 2.2
early_exit/hop_all_ids 1.18 1.20 +1% 5.9 5.9
early_exit/hop_limit_notes 5.33 5.44 +2% 107.0 108.6
early_exit/hop_all_notes 6.28 6.62 +5% 117.2 119.1
Index main MiB branch MiB main build ms branch build ms
account 18.9 19.0 20 19
ring 29.0 27.7 28 26

main run-to-run geomean: 0.995; largest single-query gap 12%.

🤖 Generated with Claude Code

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.
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying lance-csr with  Cloudflare Pages  Cloudflare Pages

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

View logs

@prrao87
prrao87 marked this pull request as ready for review November 28, 2026 13:48
@prrao87
prrao87 merged commit 5676cda into main Nov 28, 2026
10 checks passed
@prrao87
prrao87 deleted the perf/hub-safe-closing-edge branch November 28, 2026 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant