Skip to content

perf(csr): bound the pattern-predicate walk at hubs and repeated nodes - #231

Draft
prrao87 wants to merge 1 commit into
mainfrom
perf/pattern-predicate-walk
Draft

prrao87 wants to merge 1 commit into
mainfrom
perf/pattern-predicate-walk

Conversation

@prrao87

@prrao87 prrao87 commented Nov 28, 2026

Copy link
Copy Markdown
Member

Part of #223.

Why

A pattern predicate such as WHERE (b)-[:R]->()-[:P]->(a) asks, for each row, whether a path exists. With the CSR, the engine answers it by walking the graph from b, one hop at a time, until it finds a path to a. That walk had two weak spots. On the last hop it scanned the full neighbour list of whatever node it had reached, so when that node was a hub with 100,000 neighbours, every row scanned all 100,000. And it kept no record of where it had already been, so a longer predicate could walk the same node again for every path that reached it, multiplying the degrees along the way.

On main, the shape suite's predicate through a hub got 72 times slower in memory mode (30 times in lazy mode) as the hub's degree rose from 10 to 100,000. At that degree it was 3.7 times slower than the same query with use_csr=False.

What changed

  • The last hop to a bound end reads the shorter list. When the walk reaches its last hop and the end is already bound (a above), it checks the edge from whichever end has fewer neighbours, as a closing edge does since perf(csr): read a closing edge from its shorter end #228. The other end's list is the same edges keyed the other way, with the same edge rows. A hop whose sources all have at most 32 edges skips the comparison and scans, so low-degree predicates pay nothing for it.
  • Each row remembers the nodes it has explored. Any path found ends the search for that row, so a node reached a second time at the same depth can only fail again; the walk now skips it. One-hop predicates never revisit a node and keep no memo.
  • The operator docs describe both.

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 walks a four-hop predicate in which two paths reach the same node before a hub hop, and checks that every end gives the same answer with and without the shorter-end read, that a missing end node matches nothing, and that the repeated node is explored once. CI has not run yet.
  • All three suites, per CONTRIBUTING.md: release wheels from main (5676cda) and this branch (f0e6fb4), 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.82x 2.89x 7.04x 7.06x
selectivity two filters: slowest / fastest end 1 14.19x 14.44x 11.59x 11.66x
selectivity hop order: slowest / fastest 1 1.55x 1.53x 1.42x 1.42x
count_rows slowest count / fastest rows <= 1 7.07x 7.16x 6.52x 6.54x
hubs cycle: degree 100000 / degree 10 1 1.01x 1.01x 1.01x 1.01x
hubs cycle, hub at both ends: degree 100000 / 10 1 23.22x 23.98x 16.06x 16.45x
hubs pattern predicate: degree 100000 / 10 1 71.85x 1.01x 29.82x 1.00x
hubs cycle at degree 100000: CSR / join <= 1 0.35x 0.36x 0.36x 0.36x
hubs predicate at degree 100000: CSR / join <= 1 3.73x 0.05x 2.94x 0.10x
optional filtered / unfiltered OPTIONAL MATCH <= 1 3.41x 3.46x 23.76x 23.66x
optional filtered OPTIONAL MATCH / filtered MATCH about 1 3.09x 3.11x 14.84x 14.68x
wide_columns cross_filter: notes / ids about 1 4.01x 4.07x 16.56x 17.74x
wide_columns top_k: notes / ids about 1 2.87x 2.97x 13.95x 13.57x
wide_columns far_filter: notes / ids about 1 1.40x 1.38x 6.33x 6.42x
wide_columns all: notes / ids > 1 4.35x 4.39x 16.99x 17.14x
early_exit LIMIT 10 / all rows, ids << 1 0.36x 0.36x 0.42x 0.41x
early_exit LIMIT 10 / all rows, notes << 1 0.63x 0.62x 0.88x 0.90x

Only the hub predicate queries moved by 15% or more: degree 1,000 by −35% (memory) and −23% (lazy), 10,000 by −89% and −77%, and 100,000 by −99% and −97%.

SF1 suites:

Suite Mode Geomean branch / main main run 2 / run 1
graph-benchmark memory 1.005 0.997
graph-benchmark-ldbc memory 0.994 1.007
graph-benchmark-ldbc lazy 0.996 1.001
graph-benchmark-ldbc-complex memory 0.997 0.987
graph-benchmark-ldbc-complex lazy 0.987 1.006

IC7 and IC10 are the only suite queries with a pattern predicate. IC10 moved by −10% (memory) and −6% (lazy), which fits the change (its last hop now checks the bound end once instead of looking up every neighbour in the node index) but is within the noise range, so I don't claim it. IC7 didn't move, and no other SF1 query moved by 10% or more.

Machine: Apple M5 MacBook Pro, 24 GB, macOS 26.6.2. Load average 2.5 to 7.2, with the high end right after the release build.

Every SF1 query

graph-benchmark, memory

Query main (ms) branch (ms) Change
query1 7.29 7.49 +3%
query2 12.07 12.17 +1%
query3 2.72 2.71 -0%
query4 1.62 1.60 -1%
query5 1.64 1.63 -0%
query6 1.89 1.90 +1%
query7 3.68 3.66 -1%
query8 12.82 13.05 +2%
query9 12.14 12.27 +1%

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

graph-benchmark-ldbc, memory

Query main (ms) branch (ms) Change
query1 0.69 0.66 -5%
query2 0.57 0.57 +0%
query3 0.69 0.68 -1%
query4 0.54 0.54 -0%
query5 0.83 0.82 -2%
query6 0.46 0.47 +2%
query7 1.09 1.09 -0%
query8 0.67 0.67 -0%
query9 0.88 0.87 -0%
query10 0.77 0.77 -1%
query11 1.58 1.57 -1%
query12 0.78 0.78 +0%
query13 0.84 0.84 -0%
query14 0.56 0.56 -0%
query15 0.59 0.59 -0%
query16 0.71 0.71 -0%
query17 0.95 0.95 -0%
query18 0.73 0.73 +0%
query19 0.98 0.98 -0%
query20 2.08 2.09 +0%
query21 0.81 0.80 -2%
query22 0.76 0.75 -2%
query23 0.78 0.78 -1%
query24 0.73 0.74 +0%
query25 0.85 0.84 -2%
query26 0.79 0.80 +1%
query27 0.92 0.90 -2%
query28 1.43 1.42 -0%
query29 0.73 0.73 -1%
query30 19.40 18.81 -3%

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

graph-benchmark-ldbc, lazy

Query main (ms) branch (ms) Change
query1 1.18 1.16 -2%
query2 1.43 1.43 -0%
query3 0.83 0.83 -1%
query4 0.81 0.81 +0%
query5 2.19 2.19 -0%
query6 0.76 0.76 -1%
query7 1.14 1.14 -0%
query8 1.75 1.75 +0%
query9 1.39 1.38 -1%
query10 0.98 0.97 -1%
query11 2.03 2.02 -0%
query12 6.57 6.61 +1%
query13 1.10 1.08 -2%
query14 0.72 0.72 -1%
query15 0.80 0.80 -0%
query16 1.14 1.14 -0%
query17 1.43 1.43 +0%
query18 1.17 1.18 +0%
query19 1.38 1.37 -1%
query20 16.71 16.68 -0%
query21 0.86 0.86 -0%
query22 0.97 0.97 -0%
query23 1.28 1.28 -0%
query24 1.10 1.10 +0%
query25 1.24 1.23 -0%
query26 1.10 1.10 +0%
query27 1.36 1.34 -1%
query28 2.07 2.07 +0%
query29 1.06 1.07 +0%
query30 19.68 19.47 -1%

Geomean branch/main: 0.996; main run 2 / run 1: 1.001 (largest single-query main gap 4%)

graph-benchmark-ldbc-complex, memory

Query main (ms) branch (ms) Change
query1 24.39 24.92 +2%
query2 2.51 2.52 +1%
query3 24.97 25.46 +2%
query4 5.87 5.89 +0%
query5 71.01 72.12 +2%
query6 21.12 20.70 -2%
query7 9.98 9.94 -0%
query8 2.37 2.39 +1%
query9 35.60 36.13 +2%
query10 20.34 18.40 -10%
query11 3.05 3.04 -0%
query12 16.66 16.30 -2%
query13 1.14 1.16 +2%
query14 247.89 246.53 -1%

Geomean branch/main: 0.997; main run 2 / run 1: 0.987 (largest single-query main gap 6%)

graph-benchmark-ldbc-complex, lazy

Query main (ms) branch (ms) Change
query1 33.95 34.77 +2%
query2 25.42 24.60 -3%
query3 168.25 164.40 -2%
query4 13.00 13.05 +0%
query5 94.44 93.86 -1%
query6 22.67 22.08 -3%
query7 13.67 13.83 +1%
query8 3.46 3.38 -2%
query9 223.68 220.11 -2%
query10 28.83 27.09 -6%
query11 5.45 5.46 +0%
query12 19.48 19.03 -2%
query13 1.44 1.43 -1%
query14 257.59 257.34 -0%

Geomean branch/main: 0.987; main run 2 / run 1: 1.006 (largest single-query main gap 4%)

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.82x 2.89x
selectivity two filters: slowest / fastest end 1 14.19x 14.44x
selectivity hop order: slowest / fastest 1 1.55x 1.53x
count_rows slowest count / fastest rows <= 1 7.07x 7.16x
hubs cycle: degree 100000 / degree 10 1 1.01x 1.01x
hubs cycle, hub at both ends: degree 100000 / 10 1 23.22x 23.98x
hubs pattern predicate: degree 100000 / 10 1 71.85x 1.01x
hubs cycle at degree 100000: CSR / join <= 1 0.35x 0.36x
hubs predicate at degree 100000: CSR / join <= 1 3.73x 0.05x
optional filtered / unfiltered OPTIONAL MATCH <= 1 3.41x 3.46x
optional filtered OPTIONAL MATCH / filtered MATCH about 1 3.09x 3.11x
wide_columns cross_filter: notes / ids about 1 4.01x 4.07x
wide_columns top_k: notes / ids about 1 2.87x 2.97x
wide_columns far_filter: notes / ids about 1 1.40x 1.38x
wide_columns all: notes / ids > 1 4.35x 4.39x
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.52 0.50 -2% 1.7 1.6
phrasing/unnamed_start_where 0.52 0.52 +0% 1.7 1.7
phrasing/unnamed_end_inline 0.51 0.50 -2% 1.7 1.6
phrasing/unnamed_end_where 0.52 0.52 -0% 1.7 1.7
phrasing/named_start_inline 1.42 1.41 -1% 10.6 10.5
phrasing/named_start_where 1.44 1.44 -0% 10.2 10.3
phrasing/named_end_inline 1.42 1.41 -1% 10.6 10.2
phrasing/named_end_where 1.45 1.45 +1% 11.4 10.6
selectivity/rows_fwd 10.80 10.88 +1% 2.7 2.7
selectivity/rows_rev 0.76 0.75 -1% 2.1 2.0
selectivity/hop_cheap_first 0.96 0.97 +0% 2.3 2.1
selectivity/hop_hub_first 1.49 1.48 -1% 2.2 2.1
count_rows/count_fwd 5.38 5.39 +0% 3.6 3.2
count_rows/count_rev 5.34 5.30 -1% 3.4 3.3
hubs/cycle_10 1.13 1.12 -0% 2.3 2.2
hubs/cycle_join_10 1.49 1.50 +1% 2.7 2.5
hubs/both_hubs_10 1.08 1.07 -1% 2.1 1.9
hubs/predicate_10 0.58 0.58 -0% 1.8 1.7
hubs/predicate_join_10 2.93 2.89 -1% 4.3 4.2
hubs/cycle_1000 1.13 1.12 -1% 2.2 2.1
hubs/cycle_join_1000 1.50 1.52 +1% 2.5 2.6
hubs/both_hubs_1000 1.25 1.23 -1% 2.1 2.0
hubs/predicate_1000 0.91 0.59 -35% 1.8 1.7
hubs/predicate_join_1000 3.00 2.96 -1% 4.9 5.3
hubs/cycle_10000 1.13 1.12 -0% 2.2 2.1
hubs/cycle_join_10000 1.81 1.81 -0% 3.3 3.5
hubs/both_hubs_10000 3.40 3.43 +1% 2.0 2.0
hubs/predicate_10000 5.11 0.58 -89% 1.8 1.7
hubs/predicate_join_10000 4.03 3.99 -1% 9.0 9.4
hubs/cycle_100000 1.13 1.14 +1% 2.2 2.1
hubs/cycle_join_100000 3.21 3.20 -0% 12.8 10.9
hubs/both_hubs_100000 25.00 25.62 +3% 2.0 1.9
hubs/predicate_100000 41.63 0.58 -99% 1.8 1.7
hubs/predicate_join_100000 11.17 11.27 +1% 54.9 50.5
optional/match 0.60 0.60 -1% 2.5 2.4
optional/optional 0.66 0.65 -1% 2.7 2.4
optional/optional_where 2.25 2.25 +0% 7.0 6.9
optional/match_where 0.73 0.72 -0% 2.5 2.3
wide_columns/far_filter_ids 2.13 2.15 +1% 3.2 2.9
wide_columns/far_filter_notes 2.98 2.98 -0% 29.5 27.1
wide_columns/cross_filter_ids 1.17 1.16 -1% 3.2 2.8
wide_columns/cross_filter_notes 4.69 4.71 +0% 123.2 126.7
wide_columns/top_k_ids 1.36 1.35 -1% 3.1 3.3
wide_columns/top_k_notes 3.90 4.01 +3% 67.4 65.8
wide_columns/all_ids 1.13 1.13 +0% 3.5 3.9
wide_columns/all_notes 4.90 4.95 +1% 187.5 187.8
early_exit/hop_limit_ids 0.39 0.39 -2% 2.0 2.1
early_exit/hop_all_ids 1.10 1.09 -1% 3.4 3.4
early_exit/hop_limit_notes 0.55 0.54 -2% 3.2 2.4
early_exit/hop_all_notes 0.87 0.87 +1% 20.2 20.2
Index main MiB branch MiB main build ms branch build ms
account 19.0 20.0 21 21
ring 29.3 24.3 26 33

main run-to-run geomean: 0.998; largest single-query gap 7%.

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.04x 7.06x
selectivity two filters: slowest / fastest end 1 11.59x 11.66x
selectivity hop order: slowest / fastest 1 1.42x 1.42x
count_rows slowest count / fastest rows <= 1 6.52x 6.54x
hubs cycle: degree 100000 / degree 10 1 1.01x 1.01x
hubs cycle, hub at both ends: degree 100000 / 10 1 16.06x 16.45x
hubs pattern predicate: degree 100000 / 10 1 29.82x 1.00x
hubs cycle at degree 100000: CSR / join <= 1 0.36x 0.36x
hubs predicate at degree 100000: CSR / join <= 1 2.94x 0.10x
optional filtered / unfiltered OPTIONAL MATCH <= 1 23.76x 23.66x
optional filtered OPTIONAL MATCH / filtered MATCH about 1 14.84x 14.68x
wide_columns cross_filter: notes / ids about 1 16.56x 17.74x
wide_columns top_k: notes / ids about 1 13.95x 13.57x
wide_columns far_filter: notes / ids about 1 6.33x 6.42x
wide_columns all: notes / ids > 1 16.99x 17.14x
early_exit LIMIT 10 / all rows, ids << 1 0.42x 0.41x
early_exit LIMIT 10 / all rows, notes << 1 0.88x 0.90x
Query main ms branch ms Change main peak MiB branch peak MiB
phrasing/unnamed_start_inline 0.66 0.65 -1% 2.2 2.3
phrasing/unnamed_start_where 0.67 0.66 -1% 2.1 2.1
phrasing/unnamed_end_inline 0.66 0.66 -0% 2.2 2.1
phrasing/unnamed_end_where 0.65 0.67 +3% 2.3 2.1
phrasing/named_start_inline 4.56 4.55 -0% 92.8 92.7
phrasing/named_start_where 4.61 4.58 -1% 92.5 92.5
phrasing/named_end_inline 4.53 4.54 +0% 92.6 92.4
phrasing/named_end_where 4.57 4.61 +1% 92.5 92.2
selectivity/rows_fwd 12.93 12.84 -1% 6.3 6.3
selectivity/rows_rev 1.12 1.10 -1% 3.7 3.4
selectivity/hop_cheap_first 1.30 1.29 -1% 2.8 2.6
selectivity/hop_hub_first 1.85 1.82 -2% 2.9 3.0
count_rows/count_fwd 7.27 7.20 -1% 8.4 7.5
count_rows/count_rev 7.19 7.17 -0% 7.5 6.9
hubs/cycle_10 2.02 2.00 -1% 3.6 3.3
hubs/cycle_join_10 3.37 3.36 -0% 5.8 5.5
hubs/both_hubs_10 1.59 1.60 +0% 2.5 2.8
hubs/predicate_10 1.40 1.41 +1% 3.3 3.0
hubs/predicate_join_10 5.73 5.69 -1% 14.0 12.5
hubs/cycle_1000 2.01 1.99 -1% 3.4 3.3
hubs/cycle_join_1000 3.41 3.39 -1% 6.1 5.4
hubs/both_hubs_1000 1.81 1.82 +1% 2.5 2.3
hubs/predicate_1000 1.83 1.41 -23% 3.4 3.0
hubs/predicate_join_1000 5.79 5.80 +0% 13.6 16.5
hubs/cycle_10000 2.04 2.01 -1% 3.7 2.7
hubs/cycle_join_10000 3.69 3.72 +1% 6.9 6.2
hubs/both_hubs_10000 3.95 3.97 +0% 2.5 2.3
hubs/predicate_10000 6.06 1.40 -77% 3.9 3.2
hubs/predicate_join_10000 6.50 6.45 -1% 22.7 19.8
hubs/cycle_100000 2.04 2.03 -0% 3.7 3.4
hubs/cycle_join_100000 5.63 5.63 -0% 23.7 23.8
hubs/both_hubs_100000 25.50 26.24 +3% 2.5 2.4
hubs/predicate_100000 41.81 1.41 -97% 3.9 3.2
hubs/predicate_join_100000 14.21 14.21 -0% 55.0 58.3
optional/match 0.76 0.75 -2% 3.0 3.3
optional/optional 0.82 0.82 -1% 3.0 2.8
optional/optional_where 19.51 19.29 -1% 28.5 25.6
optional/match_where 1.31 1.31 -0% 3.9 3.8
wide_columns/far_filter_ids 3.09 3.07 -1% 6.0 5.2
wide_columns/far_filter_notes 19.53 19.68 +1% 108.2 107.8
wide_columns/cross_filter_ids 1.46 1.45 -1% 4.2 3.8
wide_columns/cross_filter_notes 24.20 25.71 +6% 257.4 246.5
wide_columns/top_k_ids 1.69 1.70 +0% 3.9 4.3
wide_columns/top_k_notes 23.61 23.03 -2% 233.5 230.2
wide_columns/all_ids 1.45 1.42 -2% 5.7 5.5
wide_columns/all_notes 24.58 24.36 -1% 287.4 290.5
early_exit/hop_limit_ids 0.49 0.49 -0% 2.2 2.2
early_exit/hop_all_ids 1.19 1.19 +0% 5.8 5.7
early_exit/hop_limit_notes 5.83 5.73 -2% 109.4 108.8
early_exit/hop_all_notes 6.61 6.37 -4% 118.9 119.6
Index main MiB branch MiB main build ms branch build ms
account 18.9 19.3 18 18
ring 29.8 30.9 26 27

main run-to-run geomean: 0.998; largest single-query gap 7%.

🤖 Generated with Claude Code

PathExistsExec answers a pattern predicate per row by walking the CSR.
Its last hop to a bound end walked the whole list of the node before
it, so a hub there cost its degree per row: the shape suite's
predicate grew 69x (memory) from hub degree 10 to 100,000 and ran 3.6x
slower than the join path. With no memory of explored nodes, a longer
predicate also paid the product of the degrees along every path.

- The last hop to a bound end reads the shorter of its two ends' lists,
  as a closing edge does, when some source on that hop has a long list.
- Each row remembers the nodes it has explored at each depth: any path
  from one ends the search, so a second visit can only fail again.
  One-hop predicates keep no memo.
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying lance-csr with  Cloudflare Pages  Cloudflare Pages

Latest commit: f0e6fb4
Status: ✅  Deploy successful!
Preview URL: https://16bd0f94.lance-csr.pages.dev
Branch Preview URL: https://perf-pattern-predicate-walk.lance-csr.pages.dev

View logs

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant