Skip to content

Run shortestPath from many sources with multi-source BFS #76

Closed

Description

@prrao87

Problem

shortestPath runs one breadth-first search per source node (a source with one target searches from both ends; a source with several targets shares one forward search). A query that starts from many source nodes repeats traversals over the same parts of the graph.

Idea

Multi-source BFS runs many breadth-first searches over the CSR at once. Each search is a bit in a per-node bitset, so one pass over a neighbour list advances every search that has reached that node. DuckPGQ uses it for path-finding (CIDR 2023 paper, cited in lance-format/lance-graph#159).

Activity

  1. added
    enhancement
    on Oct 28, 2026
  2. prrao87 commented on Nov 13, 2026

    @prrao87
    MemberAuthor

    Refined scope: batch compatible shortest-path searches

    Keep this as an optional execution optimization for a general-purpose graph engine. The goal is to share adjacency reads across compatible searches when that reduces repeated traversal, while preserving existing query semantics and retaining the current search strategies as fallbacks.

    Here, multi-source BFS means batching independent searches while preserving each source's identity and results. It does not mean merging sources into one search that returns only the distance to the nearest source.

    Starting point

    crates/lance-csr/src/shortest_path.rs already groups targets by source so that several targets share one forward search. Eligible single-target searches use bidirectional BFS. This issue adds sharing across sources; it should not replace those strategies indiscriminately.

    First investigation

    • Start with existing unweighted shortest-path queries whose searches share the same relationship types, directions, filters and graph snapshot. Keep source-dependent filters and other unsupported shapes on the existing path.
    • Use bounded batches of sources, with per-node bit masks recording which searches are active or have visited a node. A machine-word-sized batch is a candidate to measure, not a required public setting.
    • Share a neighbour-list read when multiple active searches can use it. Preserve separate visited state, target completion and results for each source.
    • Account for retained memory, including frontier masks, visited state and predecessor information. Respect cancellation and existing execution limits.
    • Keep current single-source and bidirectional searches as fallbacks. Establish a conservative selection rule from measurements; many sources alone do not guarantee enough overlap to justify batching.
    • Treat path reconstruction as part of the problem. A distance-only prototype can establish traversal costs, but does not complete an optimization of an API that returns paths. Preserve the existing contract for one shortest path, all shortest paths, ties, duplicate input bindings and unreachable targets. Unsupported output modes retain their current implementation.

    Weighted shortest paths, a new graph storage layout, a public tuning API and a general graph-analytics framework are outside this scope.

    Evidence needed before adoption

    1. Compare results against the current implementation. Cover directed and undirected traversal, cycles, self-loops, parallel edges, disconnected components, source equal to target, depth limits, several targets per source, duplicate bindings, ties, and batch boundaries where supported by the existing API. Test fallback cases too.
    2. Measure both favourable and unfavourable workloads: overlapping searches on hub-heavy graphs, chains, trees, disconnected regions, low-overlap searches, and selective source–target pairs. Include one-source and few-source controls. Do not make small-world/social graphs a requirement for using lance-csr.
    3. Report a table of latency, peak/retained memory, adjacency work and result size, with and without batching. Separate traversal savings from path reconstruction and output costs. Check whether source/frontier parallelism alone is a smaller sufficient improvement.
    4. Before proposing a production performance change, follow CONTRIBUTING.md: release wheels from main and the branch, both existing benchmark suites in memory and lazy modes, alternating repeated runs, unchanged queries/settings and every result reported. The targeted shortest-path workloads supplement those suites.
    5. Conclude whether the measured benefit justifies the implementation. If it does, document supported shapes, the selection rule and fallbacks; otherwise record the negative result rather than forcing a new execution path.

    References and limits of the evidence

    No lance-csr prototype or new performance measurement accompanies this scope update. This remains an investigation into a selective optimization, not a commitment to specialize the engine around one graph shape.

  3. prrao87 commented on Nov 13, 2026

    @prrao87
    MemberAuthor

    Experiment results: batching pays off when traversal state is shared

    Two prototypes were measured against the current search at c4ab747 (v0.10.0), first as a search kernel and then end to end through Cypher. Everything is opt-in behind an experimental-msbfs feature; normal builds and query plans are unchanged.

    What was tried

    • batched shares each neighbour read across up to 64 sources, but keeps a depth map and a predecessor list per source for every vertex reached. Its state grows with sources × reached vertices, as in the unbatched search, so it saves only the adjacency scan. It is slower than the current search in most cases and uses more memory. This is the negative result.
    • masked keeps one 64-lane mask per reached vertex and one (predecessor, lane mask) record per discovering edge, shared by all sources. Distances are read from the masks after each level. Per-source predecessor maps are built afterwards, only for vertices on a shortest path to a requested target. Every shortest path, tie and parallel edge is preserved. This is the structure the MS-BFS paper and DuckPGQ use, with hash maps keyed by raw id because the CSR stores raw neighbour ids.

    Search kernel

    Search time in ms for 64 sources on synthetic graphs. Parallel runs the current search on eight workers; the others use one thread.

    Graph Targets/source Current Batched Masked Parallel
    Hub, 32K vertices 3 90.6 162.6 1.23 20.5
    Uniform, 32K 3 164.0 270.9 20.3 49.8
    Reachable tree, 4K 3 2.59 2.03 0.17 1.11
    Layered, 4K 3 1.90 1.80 0.53 1.00
    Chain, 4K 3 0.058 0.069 0.079 0.212
    Hub, 32K 1 0.025 171.6 1.28 0.13
    Uniform, 32K 1 2.15 215.5 19.4 0.95
    Tree, 4K 1 0.39 1.92 0.082 0.41

    Whole-process peak memory on the 32K hub with three targets: 269 MiB current, 393 MiB batched, 29 MiB masked.

    End to end

    The same workloads run as Cypher through CypherEngine with a CSR. A relationship binds each (source, target) request and the query returns both endpoints, length(p) and nodes(p), so parsing, planning, the join, path reconstruction and list output are inside the timing. search_batch hands sources to masked under one of two selections: only sources with several targets (single-target sources stay on bidirectional search), or every source. The selections alternate within one process and must return the same rows.

    Median ms for shortestPath, 64 sources:

    Graph Targets/source Current Masked, multi-target Masked, all
    Hub, 32K 3 118.0 2.16 1.95
    Uniform, 32K 3 213.3 23.5 23.1
    Reachable tree, 4K 3 3.30 0.71 0.71
    Layered, 32K 3 2.56 1.24 1.26
    Chain, 4K 3 0.498 0.523 0.526
    Hub, 32K 1 0.456 0.444 1.80
    Uniform, 32K 1 3.65 2.98 22.7
    Layered, 32K 1 1.53 1.53 0.55

    allShortestPaths and single-partition runs give the same ratios.

    Conclusions

    • Batching is worth pursuing with shared state. With several targets per source, masked is 2× to 55× faster end to end on hub, uniform, tree and layered graphs. Chains, which have nothing to share, are 5% slower (about 0.02 ms).
    • Keep bidirectional search for single-target sources. Sending them to masked is up to 6× slower on hub and uniform graphs. It is up to 2.9× faster on tree and layered graphs, so a better rule may exist, but these measurements do not support a cost model.
    • Source parallelism is a separate lever. Eight workers make the current search 2× to 4.6× faster on multi-target cases, less than masked gains on one thread. It could run 64-source batches concurrently.
    • Correctness. A self-check compares all shortest paths against forward and bidirectional search over 160 combinations of small graphs, direction, appended and deleted edges and depth limits, with 65 requests each. It covers repeated sources, ties, self-loops, parallel edges, unreachable and missing targets, source equal to target and the 64-source batch boundary.

    Not done

    • Inputs large enough to split across batches or partitions. Every query here reached the operator as one batch, the best case for sharing.
    • Lance-backed data, memory accounting for the shared state, and memory measured end to end.
    • allShortestPaths with large outputs, lanes wider than 64, and cancellation or deadline tests for the new path.
    • The CONTRIBUTING.md suites. No main-versus-branch speedup is claimed; those remain a prerequisite for a production change.

    Full tables, raw data and reproduction steps are in RESULTS.md on branch codex/multi-source-bfs-experiment. Measured on an Apple M5 (10 cores) with synthetic graphs, which are controls, not a sample of real workloads.

  4. prrao87 commented on Nov 13, 2026

    @prrao87
    MemberAuthor

    Done in #153 (merged as d450ccf).

    What shipped. The forward shortest-path searches of an input batch now run together, up to 64 sources at a time. Each source owns one bit of a mask; a reached node keeps two masks and one (predecessor, mask) record per edge, and per-source predecessors are collected afterwards only for nodes on a shortest path to a requested target. Every shortest path, tie and parallel edge is kept. A source with one target still searches from both ends when incoming adjacency is available. There is no new setting and no planner change.

    Against the evidence this issue asked for:

    1. Same results. Tests compare the batched search with one search per source on every directed three-node graph (parallel edges, self-loops, directed and undirected, hop limits 0 to 3), check 150 sources split across three batches through the operator, and check cancellation. The experiment's self-check also covered appended and deleted edges, repeated sources, unreachable and missing targets, and sparse keys.
    2. Favourable and unfavourable workloads. Hub, uniform, chain, tree, disconnected and layered graphs, with one-source and few-source controls, are in the experiment results above. Chains, with nothing to share, were about 5% slower end to end (about 0.02 ms).
    3. Latency, memory and output. On a 20,000-node, 160,000-edge graph with release wheels from main and the branch:
    Workload Time on main Time on branch Speedup Memory on main Memory on branch
    1,000 sources x 16 targets 1.57 s 0.19 s 8.4x 1.9 GiB 0.2 GiB
    Same, without a CSR 3.12 s 0.42 s 7.4x 5.7 GiB 0.2 GiB
    20,000 pairs 5.81 s 0.89 s 6.5x 9.7 GiB 0.5 GiB
    allShortestPaths, 300 sources x 8 targets 0.40 s 0.06 s 7.1x 0.7 GiB 0.2 GiB
    64 sources x 3 targets 0.07 s 0.03 s 2.9x 0.2 GiB 0.2 GiB

    These inputs exceed one batch and spread over partitions, and runs over Lance datasets on disk match the in-memory ones. Source parallelism alone gave 2x to 4.6x on eight workers, less than batching on one thread.
    4. Both benchmark suites. Within noise in memory and lazy modes (geometric mean of branch over main between 0.994 and 1.013), every query passing. Per-query tables are in #153.
    5. Conclusion. The benefit justified the change. The first prototype, which shared neighbour reads but kept per-source maps, was slower than main; that negative result is recorded above and on the codex/multi-source-bfs-experiment branch.

    Sources. The approach follows The More the Merrier: Efficient Multi-Source Graph Traversal and DuckPGQ's use of it, adapted to raw neighbour keys and to returning every shortest path.

    Left out, as possible follow-ups: batches wider than 64 sources, memory accounting for the operator (main had none either), and a cost-based choice between batched and bidirectional search for single targets, where batching was faster on tree and layered graphs but far slower on hub and uniform ones.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions