]> git.hungrycats.org Git - bees/commitdiff
hash: resize in one pass when no buckets merge
authorZygo Blaxell <bees@furryterror.org>
Sun, 23 Aug 2026 17:14:47 +0000 (13:14 -0400)
committerZygo Blaxell <bees@furryterror.org>
Tue, 6 Oct 2026 05:51:40 +0000 (01:51 -0400)
Resizing walked the old table c_cells_per_bucket (256) times, once per
cell position, so that cells from old buckets merging into one new
bucket interleave by LRU rank instead of one bucket evicting another.
That cost is only necessary when merging actually happens.

A cell is placed at hash % new_buckets and lived at hash % old_buckets.
If old_buckets divides new_buckets, hash % new_buckets == n implies
hash % old_buckets == n % old_buckets, so every cell in new bucket n
comes from exactly one old bucket and no merging occurs.  That covers
every integer-factor grow, including the doubling an operator gets from
asking for twice as much hash table.

With nothing to interleave, one sequential pass over the old table
produces bit-identical output.  Detect the divisible case and take it.

The difference is not academic.  A real 8G to 16G resize logged:

  Resizing hash table: 19423/16777216 (0%), 210.11 sec elapsed,
      181278 sec remaining

256 passes over 8G is 2 TiB of reads.  Once the old table no longer fits
in the page cache every pass after the first comes from disk, which measured
~12 MB/s and put the finish 50 hours out, with the estimate still climbing as
the 16G destination evicted what remained of the source.  One pass reads 8 GiB.

Verified end to end by resizing one populated 128M table with the
pre-change and post-change binaries and comparing beeshash.dat:
identical for a 2x grow and a 3x grow (fast path taken), and identical
for a 1.5x grow, where 49152 % 32768 != 0 keeps the merging path and
confirms it is unchanged.

Shrinks and non-divisible grows still make 256 passes.  Shrink is the
worse case, since the table being read 256 times is the larger one; it
needs a rank-aware single-pass merge, which needs somewhere to keep each
cell's LRU rank, and is left for later.

Assisted-by: Claude-Code:claude-opus-5
Signed-off-by: Zygo Blaxell <bees@furryterror.org>
src/bees-hash.cc

index c422a8d6e9b9c55f1b9d6cb223e21a40e08b60f3..4784bcbcb133d1c0a66b23f6697a030de0e99ff3 100644 (file)
@@ -975,6 +975,24 @@ BeesHashTable::resize_file(off_t new_size_signed)
        BEESLOGDEBUG("Allocated " << pretty(new_size) << " (" << new_extents << " extents)"
                " for the resized hash table in " << alloc_timer << " sec");
 
+       // Can two old buckets land in the same new bucket?  A cell is placed
+       // at new_bucket = hash % new_buckets, and lived at
+       // old_bucket = hash % old_buckets.  If old_buckets divides
+       // new_buckets, then hash % new_buckets == n implies
+       // hash % old_buckets == n % old_buckets, so every cell in new bucket n
+       // came from exactly one old bucket and no merging occurs.  That covers
+       // every power-of-two grow, which is what an operator asking for "twice
+       // as much hash table" gets.
+       //
+       // With no merging there is no LRU interleaving to do, so one sequential
+       // pass over the old table produces bit-identical output to the
+       // c_cells_per_bucket-pass loop below, for 1/c_cells_per_bucket of the
+       // reads.  The difference is not academic: 256 passes over an 8G table
+       // is 2 TiB of reads, which is ~50 hours once the table no longer fits
+       // in the page cache, versus 8 GiB read once.
+       const uint64_t old_bucket_count = old_size / BLOCK_SIZE_HASHTAB_BUCKET;
+       const bool no_merge = old_bucket_count > 0 && (new_buckets % old_bucket_count) == 0;
+
        // Cell-position is the OUTER loop, not the inner.  When two old
        // buckets collide on one new bucket (the common case on shrink:
        // cutting buckets in half forces every cell to share a new bucket
@@ -988,34 +1006,58 @@ BeesHashTable::resize_file(off_t new_size_signed)
        // new bucket retains the newest cells across all colliding old
        // buckets — which is the LRU semantics we want.
        //
-       // Cost: c_cells_per_bucket × old_extents pread calls instead of
-       // old_extents.  When the old file fits in the page cache this is
-       // fast; when it does not, every pass after the first reads from
-       // disk again.  Resizing a hash table is a rare, slow operation —
-       // correctness over speed.
+       // Cost when merging: c_cells_per_bucket × old_extents pread calls
+       // instead of old_extents.  When the old file fits in the page cache
+       // this is fast; when it does not, every pass after the first reads
+       // from disk again.
        //
-       // One "read" here is one old extent on one cell-pass, so the total
-       // unit count is the product.  Every unit costs the same one pread
-       // plus a fixed amount of bucket walking, which makes the linear ETA
-       // in BeesProgressLogger meaningful.
-       const uint64_t total_reads = c_cells_per_bucket * old_extents;
+       // One "read" is one old extent on one cell-pass, so the total unit
+       // count is the product.  Every unit costs the same one pread plus a
+       // fixed amount of bucket walking, which makes the linear ETA in
+       // BeesProgressLogger meaningful — as long as the page cache holds the
+       // old table.  Once it does not, the estimate runs optimistic because
+       // later passes fault in from disk.
+       const uint64_t total_reads = no_merge ? old_extents : c_cells_per_bucket * old_extents;
        BeesProgressLogger resize_progress("Resizing hash table");
        ByteVector extent_buf(BLOCK_SIZE_HASHTAB_EXTENT);
-       for (uint64_t ci = c_cells_per_bucket; ci > 0; --ci) {
-               const uint64_t cell_idx = ci - 1;
-               const uint64_t reads_before = (c_cells_per_bucket - ci) * old_extents;
+       if (no_merge) {
+               BEESLOGINFO("Resizing without bucket merges: " << old_bucket_count
+                       << " old buckets divide " << new_buckets << " new buckets, one pass");
                for (uint64_t pass = 0; pass < old_extents; ++pass) {
                        const uint64_t ei = old_extents - 1 - pass;
-                       resize_progress.update(reads_before + pass + 1, total_reads);
+                       resize_progress.update(pass + 1, total_reads);
                        const off_t offset = ranged_cast<off_t>(ei) * BLOCK_SIZE_HASHTAB_EXTENT;
                        pread_or_die(m_fd, extent_buf, offset);
 
-                       const Bucket *const old_buckets = reinterpret_cast<const Bucket *>(extent_buf.data());
+                       const Bucket *const old_bucket_ptr = reinterpret_cast<const Bucket *>(extent_buf.data());
                        for (uint64_t b = 0; b < buckets_per_extent; ++b) {
-                               const Cell &c = old_buckets[b].p_cells[cell_idx];
-                               if (!c.e_addr) continue;
-                               Bucket &dst = new_bucket_ptr[c.e_hash % new_buckets];
-                               push_front_in_range(dst.p_cells, dst.p_cells + c_cells_per_bucket, c);
+                               // LRU-oldest slot first so the newest ends up at the
+                               // front, matching what the cell-pass loop below builds.
+                               for (uint64_t ci = c_cells_per_bucket; ci > 0; --ci) {
+                                       const Cell &c = old_bucket_ptr[b].p_cells[ci - 1];
+                                       if (!c.e_addr) continue;
+                                       Bucket &dst = new_bucket_ptr[c.e_hash % new_buckets];
+                                       push_front_in_range(dst.p_cells, dst.p_cells + c_cells_per_bucket, c);
+                               }
+                       }
+               }
+       } else {
+               for (uint64_t ci = c_cells_per_bucket; ci > 0; --ci) {
+                       const uint64_t cell_idx = ci - 1;
+                       const uint64_t reads_before = (c_cells_per_bucket - ci) * old_extents;
+                       for (uint64_t pass = 0; pass < old_extents; ++pass) {
+                               const uint64_t ei = old_extents - 1 - pass;
+                               resize_progress.update(reads_before + pass + 1, total_reads);
+                               const off_t offset = ranged_cast<off_t>(ei) * BLOCK_SIZE_HASHTAB_EXTENT;
+                               pread_or_die(m_fd, extent_buf, offset);
+
+                               const Bucket *const old_bucket_ptr = reinterpret_cast<const Bucket *>(extent_buf.data());
+                               for (uint64_t b = 0; b < buckets_per_extent; ++b) {
+                                       const Cell &c = old_bucket_ptr[b].p_cells[cell_idx];
+                                       if (!c.e_addr) continue;
+                                       Bucket &dst = new_bucket_ptr[c.e_hash % new_buckets];
+                                       push_front_in_range(dst.p_cells, dst.p_cells + c_cells_per_bucket, c);
+                               }
                        }
                }
        }