Skip to content

Shrink the CSR's per-node map and neighbours #79

Closed

Description

@prrao87

Problem

The CSR lives in memory even when tables are read lazily from Lance. On GDC SF1, its 17.3M edges take 732 MiB for both directions.

#109 packed the edge rows from 8 bytes to about 2 per edge per direction, which brought SF1 down from 926 MiB, at almost no query cost. What's left, per direction (from docs/csr.md):

  • Neighbours: 8 bytes per edge, plain u64 raw target keys.
  • Per source node: about 37 bytes, for its offset and its entry in Adjacency's FastMap<i64, u64> from key to CSR position.

Worked out from those figures, not measured: at SF1 the edges account for about 330 MiB and the per-node part for about 400 MiB. If so, the map from key to position, not the neighbours, is now the largest piece.

Already tried

Any decoding on the neighbour path costs every hop, so delta-encoding neighbours is unlikely to pay off either.

Ideas

  • The map from key to position. Replace FastMap<i64, u64> with something denser, such as a sorted key array searched by binary search (a stored CSR already holds keys in position order). That trades memory for slower lookups at the start of a hop, so it needs measuring on hop-heavy queries.
  • Narrower neighbours where the key range allows it. At build time, choose a u32 offset from min_key when max_key - min_key fits in 32 bits, and keep u64 otherwise. This doesn't help SF1, and CsrIndex::neighbors() returns &[u64], so it changes a public signature.

Before starting

  • Measure the real split between the key-to-position map, offsets, neighbours and edge rows at SF1. The numbers above are worked out from type sizes and the documented totals.
  • Benchmark against main on both suites in both memory and lazy modes, following CONTRIBUTING.md, with resident memory reported next to latency.

Activity

  1. added
    enhancement
    on Oct 28, 2026
  2. changed the title [-]Store CSR neighbours in fewer bytes[/-] [+]Shrink the CSR's per-node map and neighbours[/+] on Nov 3, 2026
  3. prrao87 commented on Nov 14, 2026

    @prrao87
    MemberAuthor

    Status after #159, which shrank the per-node map. The neighbours are unchanged.

    Done in #159

    Measured on GDC SF1 for all 23 relationship types, both directions. The figures are allocated bytes from a diagnostic build, not process memory.

    Storage Before (MiB) After (MiB)
    Source index (map, plus offsets before) 444.7 92.6
    Neighbours 263.3 263.3
    Packed edge rows 68.9 61.2
    Total 776.9 417.1

    Resident memory after engine start fell by 12% to 18% on both SF1 suites in both modes, and no suite query got slower beyond run-to-run noise. Both "Before starting" items were done: the split above is measured, and both suites ran against main in memory and lazy modes.

    The sorted key array with binary search was tried and rejected, because lookups were 3 to 8 times slower. Each direction now picks its own layout instead: by default, a hash map from key to the source's start and end; for large many-to-one directions with clustered ids, a 64-bit bitmap per run of 64 keys. The rule and the measurements behind it are in docs/internals/indexes.md.

    Not finished

    • Neighbours. They are still 8 bytes per edge as raw u64 keys, and now the largest part of the CSR (263 of 417 MiB). The u32 offset from min_key still doesn't fit SF1, whose keys span up to 2^45, and it would change the signature of CsrIndex::neighbors().
    • Carrying node rows between hops. This is the one idea measured so far that would shrink both parts. If a hop received node rows instead of ids, the source index would become a plain array and neighbours could be 32-bit rows: about 273 MiB instead of 417 on SF1, for any id distribution, and the per-direction layouts would no longer be needed. That figure is worked out from SF1 counts. Only lookups were measured, in a debug microbenchmark, at 0.07 to 0.26 times the current lookup time. It would change what flows between operators, the stored format, deltas, shortest paths and CsrIndex::neighbors(), and it ties a CSR to its node tables' rows, so changing a node table would invalidate it. See A source index addressed by node row.
    • Gaps in what perf(csr): shrink the csr's per-source index #159 measured.
      • Graphs with skewed degrees, string properties or timestamp-style ids were not run end to end.
      • The bitmap layout between 1M and 4M sources was measured end to end only on SF1.
      • The wide layout, for directions with more than u32::MAX edges, is covered by unit tests only.
      • IC14 moves by up to 39% between builds for a reason not pinned down.
  4. prrao87 commented on Nov 14, 2026

    @prrao87
    MemberAuthor

    Closing: #159 shrank the per-node map, as summarised above. The neighbours are what's left, and the ideas listed here for them don't fit SF1. The approach that would shrink them, carrying node rows between hops, is a larger redesign and is tracked in #160.

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