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
// 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);
+ }
}
}
}