Repository navigation
Description
Problem
The CSR lives in memory even when tables are read lazily from Lance. After #159, on GDC SF1 it takes 417 MiB for both directions:
| Storage, both directions | MiB |
|---|---|
| Source index | 92.6 |
| Neighbours | 263.3 |
| Packed edge rows | 61.2 |
The neighbours are now the largest part. Each one is a raw u64 node id, 8 bytes per edge per direction. The ideas tried for them under #79 don't pay off:
- Packing neighbours roughly halved them, but lookup-heavy queries slowed down: custom Q30 by about 22% (feat(csr)!: store the csr inside each lance dataset and pack edge rows #109).
- A
u32offset frommin_keydoesn't fit SF1, whose node keys span up to 2^45.
The source index is small on SF1, but only because #159 picks a layout per direction from the shape of its keys. Graphs with random or timestamp-style ids get the larger per-source hash map.
Both costs come from the same thing: hops pass node ids. A hop has to turn each id into a position through a hash map, and a neighbour has to be stored as an id because the next hop expects one.
Idea
Pass each node's row number between hops instead of its id. A row number is the node's position in its node table: one 32-bit integer per node, not the row's contents. No property is read during a hop, today or under this proposal. A query reads a node's properties only after the traversal, and then only the columns it uses, as it does now with take_rows. A large column such as a blob is never touched unless the query returns it.
With row numbers between hops:
- The source index becomes a plain array of offsets indexed by row number, with no hash map and no per-direction layout choice.
- Neighbours become 32-bit row numbers instead of 64-bit ids.
- A hop no longer looks up the target's id in the node index before fetching its properties, since it already has the row number.
Worked out from SF1 counts, not measured end to end:
| Storage, both directions | Now (MiB) | Row numbers between hops (MiB) |
|---|---|---|
| Source index | 92.6 | 80.0 |
| Neighbours | 263.3 | 131.7 |
| Packed edge rows | 61.2 | 61.2 |
| Total | 417.1 | 272.9 |
Queries still need ids in places: WHERE a.id = …, RETURN a.id, and joins with parts of a plan that don't use the CSR. That needs an array from row number to id per label: 24 MiB on SF1 in lazy mode. The CSR's size would no longer depend on how ids are distributed.
In a debug microbenchmark, a lookup by row number was 0.07 to 0.26 times the time of the current per-source map. A second form was measured too and dropped: keep ids between hops, and find each source's row number through the node index on every hop. It was slower everywhere and would save at most 29 MiB on SF1. See A source index addressed by node row.
What it touches
- What flows between operators: hops would emit and accept row numbers, and ids would be looked up only where a query uses them.
- The stored CSR format and deltas: both hold ids today.
- Shortest paths, which walk the CSR by id.
CsrIndex::neighbors(), which returns&[u64]ids. This is a public signature.- Node table changes: compacting or deleting from a node table can renumber its rows. The CSR would then depend on its node tables' row numbers, so a change to a node table would invalidate it, not only a change to the relationship table.
- Fetching properties in lazy mode: a Lance row address is 64 bits (fragment id and offset), so a 32-bit row number needs a way to reach it, such as an array per label or a take by offset. That is not worked out yet.
- Size limit: 32-bit row numbers cap a label at about 4 billion nodes.
- The CSR build order: the node index is needed before the CSR build, and today the two are built concurrently.
Before starting
- Decide how a node table change invalidates or remaps a CSR, since that sets whether this fits the current freshness model (feat(csr): report how far the base csr trails the data #93, feat(csr): fold the delta into a new base with optimize_csr() #98).
- Decide how a row number reaches a Lance row in lazy mode, and include that mapping in the memory estimate.
- Benchmark against
mainon both suites in bothmemoryandlazymodes, followingCONTRIBUTING.md, with resident memory reported next to latency. Include a graph with random ids, which perf(csr): shrink the csr's per-source index #159's layouts don't help.
Split from #79.
Activity
[-]Carry node rows between CSR hops instead of ids[/-][+]Pass node row numbers between CSR hops instead of ids[/+]prrao87 commentedon Nov 14, 2026
Closing as not planned. A feasibility review against
mainat 61613d1 found that row numbers between hops cost more than they would save.Why row numbers were ruled out
It breaks a documented guarantee. The CSR is built from the relationship table alone, and the CSR guide promises that writes to a node table never touch it. With row numbers, appending to, deleting from or compacting a node table would invalidate the CSR. Edges whose target node doesn't exist yet (covered by a test in
test_engine_csr.py) would have no row number to store.The CSR can't always tell which node table to number against. A relationship's
source_labelandtarget_labelare optional, and a single hop picks the target's node index from the query pattern, not from the mapping.Lazy mode has no dense row numbers. The node index maps an id to a 64-bit Lance row id (fragment and offset). A 32-bit row number would need an array per label to reach it, on top of the row-to-id array that queries returning ids would need. Both reduce the saving.
Undirected hops probe both directions with the same key. That works with ids, which every table shares, but with row numbers only when both ends have the same label.
It touches nearly every CSR consumer:
Shortest paths would also change behaviour: they currently pass through nodes missing from the node table.
The saving would be about 145 of the CSR's 417 MiB on SF1, less the mapping arrays, so roughly 100 MiB net. That is about 4% to 7% of resident memory, worked out from counts and not measured.
A possible future option: dictionary-encoded neighbours
Most directions have far fewer distinct targets than edges. For example, 2.05M comments point to about 9,300 creators. Each direction could store its distinct target ids once, with each neighbour as a
u32index into that list. The CSR would still hold only ids, so node tables stay out of it, andCsrIndex::neighbors()would not change.Estimated from the per-direction counts in
csr-memory-79-branch.csv, using the dictionary only in the 32 of 46 directions where it is smaller, SF1's neighbours would drop from 263.3 to 182.9 MiB, saving about 80 MiB.This is untested. Each neighbour would cost one extra array read, and bit-packed neighbours slowed custom Q30 by about 22% in #109. A lookup microbenchmark should come first, before the suites.