]> git.hungrycats.org Git - bees/commitdiff
hash: load the table eagerly at startup, drop demand-load
authorZygo Blaxell <bees@furryterror.org>
Tue, 16 Jun 2026 23:28:33 +0000 (19:28 -0400)
committerZygo Blaxell <bees@furryterror.org>
Tue, 6 Oct 2026 05:51:40 +0000 (01:51 -0400)
The hash table mapping is MAP_PRIVATE | MAP_ANONYMOUS, so "demand loading"
was a per-extent pread into anonymous pages on the first worker-thread
touch, gated by ExtentMetaData::m_missing.  Under dedupe conditions,
lookups are effectively random, which turns the table read into maximally
scattered I/O taken on worker threads under the per-extent lock, when it
could be an optimal sequential read at startup, with no further locking
overhead afterward.

Move the read into the main thread in prepare(), after the mapping is sized
and before mlock, looping over extents in index order.  This is sequential
I/O and completes before any worker can look up a hash, so the lookup and
insert paths may now assume the table is fully resident.  Read per-extent
because pread_or_die treats a short read as fatal (it loops only on EINTR);
a multi-GB single read could legitimately come up short.  A read failure
here is fatal on purpose: a table that cannot be fully loaded is not safe
to dedupe against.  Memory-only mode skips the load entirely — the pages
are already zero from MAP_ANONYMOUS.

With every extent resident after prepare(), the demand-load machinery
is dead: delete m_missing, fetch_missing_extent_by_index/_by_hash,
the six worker call sites, the demand-load call in prefetch_loop,
and the m_prefetch_running readahead hint.  The toxic-bit clear and
verify_cell_range that rode along on the per-extent fetch are gone with
it; both guarded against long-fixed kernel bugs that scan-next no longer
bothers to detect.

prefetch_loop keeps its occupancy survey for now (still correct against the
now fully-resident table); collapsing that into incremental counters is a
follow-up.  Residency now rests entirely on mlock, with memcg swappiness=0
or a table small relative to RAM as the fallback when mlock is unavailable.

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

index 46123ceb525acd4775d3e74679328f34e4902d23..cf23246f3e1e8d48d5f6de30707e26fb11da57bd 100644 (file)
@@ -287,12 +287,8 @@ BeesHashTable::prefetch_loop()
                size_t toxic_count = 0;
                size_t unaligned_eof_count = 0;
 
-               m_prefetch_running = true;
                for (uint64_t ext = 0; ext < m_extents && !m_stop_requested; ++ext) {
-                       BEESNOTE("prefetching hash table extent #" << ext << " of " << m_extents);
                        catch_all([&]() {
-                               fetch_missing_extent_by_index(ext);
-
                                BEESNOTE("analyzing hash table extent #" << ext << " of " << m_extents);
                                bool duplicate_bugs_found = false;
                                auto lock = lock_extent_by_index(ext);
@@ -329,7 +325,6 @@ BeesHashTable::prefetch_loop()
                                }
                        });
                }
-               m_prefetch_running = false;
 
                BEESNOTE("calculating hash table statistics");
 
@@ -437,97 +432,13 @@ BeesHashTable::lock_extent_by_index(uint64_t extent_index)
 unique_lock<mutex>
 BeesHashTable::lock_extent_by_hash(HashType hash)
 {
-       BEESTOOLONG("fetch_missing_extent for hash " << to_hex(hash));
+       BEESTOOLONG("lock_extent_by_hash " << to_hex(hash));
        return lock_extent_by_index(hash_to_extent_index(hash));
 }
 
-void
-BeesHashTable::fetch_missing_extent_by_index(uint64_t extent_index)
-{
-       BEESNOTE("checking hash extent #" << extent_index << " of " << m_extents << " extents");
-       auto lock = lock_extent_by_index(extent_index);
-       if (!m_extent_metadata.at(extent_index).m_missing) {
-               return;
-       }
-
-       if (!m_persistent) {
-               // Memory-only mode: extent is zero-initialised from MAP_ANONYMOUS.
-               m_extent_metadata.at(extent_index).m_missing = false;
-               return;
-       }
-
-       // OK we have to read this extent
-       BEESNOTE("fetching hash extent #" << extent_index << " of " << m_extents << " extents");
-       BEESTRACE("Fetching hash extent #" << extent_index << " of " << m_extents << " extents");
-       BEESTOOLONG("Fetching hash extent #" << extent_index << " of " << m_extents << " extents");
-
-       uint8_t *const dirty_extent      = m_extent_ptr[extent_index].p_byte;
-       uint8_t *const dirty_extent_end  = m_extent_ptr[extent_index + 1].p_byte;
-       const size_t dirty_extent_size   = dirty_extent_end - dirty_extent;
-       const size_t dirty_extent_offset = dirty_extent - m_byte_ptr;
-
-       // If the read fails don't retry, just go with whatever data we have
-       m_extent_metadata.at(extent_index).m_missing = false;
-
-       catch_all([&]() {
-               BEESTOOLONG("pread(fd " << m_fd << " '" << name_fd(m_fd)<< "', length " << to_hex(dirty_extent_end - dirty_extent) << ", offset " << to_hex(dirty_extent - m_byte_ptr) << ")");
-               pread_or_die(m_fd, dirty_extent, dirty_extent_size, dirty_extent_offset);
-
-               // Only count extents successfully read
-               BEESCOUNT(hash_extent_in);
-
-               // Won't need that again
-               bees_unreadahead(m_fd, dirty_extent_offset, dirty_extent_size);
-
-               // If we are in prefetch, give the kernel a hint about the next extent
-               if (m_prefetch_running) {
-                       // Use the kernel readahead here, because it might work for this use case
-                       readahead(m_fd, dirty_extent_offset + dirty_extent_size, dirty_extent_size);
-               }
-       });
-
-       Cell *cell     = m_extent_ptr[extent_index    ].p_buckets[0].p_cells;
-       Cell *cell_end = m_extent_ptr[extent_index + 1].p_buckets[0].p_cells;
-       size_t toxic_cleared_count = 0;
-       set<BeesHashTable::Cell> seen_it(cell, cell_end);
-       while (cell < cell_end) {
-               if (cell->e_addr & BeesAddress::c_toxic_mask) {
-                       ++toxic_cleared_count;
-                       cell->e_addr &= ~BeesAddress::c_toxic_mask;
-                       // Clearing the toxic bit might mean we now have a duplicate.
-                       // This could be due to a race between two
-                       // inserts, one finds the extent toxic while the
-                       // other does not.  That's arguably a bug elsewhere,
-                       // but we should rewrite the whole extent lookup/insert
-                       // loop, not spend time fixing code that will be
-                       // thrown out later anyway.
-                       // If there is a cell that is identical to this one
-                       // except for the toxic bit, then we don't need this one.
-                       if (seen_it.count(*cell)) {
-                               cell->e_addr = 0;
-                               cell->e_hash = 0;
-                       }
-               }
-               ++cell;
-       }
-       if (toxic_cleared_count) {
-               BEESLOGDEBUG("Cleared " << toxic_cleared_count << " hashes while fetching hash table extent " << extent_index);
-       }
-}
-
-void
-BeesHashTable::fetch_missing_extent_by_hash(HashType hash)
-{
-       uint64_t extent_index = hash_to_extent_index(hash);
-       BEESNOTE("waiting to fetch hash extent #" << extent_index << " of " << m_extents << " extents");
-
-       fetch_missing_extent_by_index(extent_index);
-}
-
 vector<BeesHashTable::Cell>
 BeesHashTable::find_cell(HashType hash)
 {
-       fetch_missing_extent_by_hash(hash);
        BEESTOOLONG("find_cell hash " << BeesHash(hash));
        vector<Cell> rv;
        auto lock = lock_extent_by_hash(hash);
@@ -544,7 +455,6 @@ BeesHashTable::find_cell(HashType hash)
 void
 BeesHashTable::erase_hash_addr(HashType hash, AddrType addr)
 {
-       fetch_missing_extent_by_hash(hash);
        BEESTOOLONG("erase hash " << to_hex(hash) << " addr " << addr);
        auto lock = lock_extent_by_hash(hash);
        auto er = get_cell_range(hash);
@@ -611,7 +521,6 @@ BeesHashTable::push_front_in_range(Cell *begin, Cell *end, const Cell &mv)
 bool
 BeesHashTable::push_front_hash_addr(HashType hash, AddrType addr)
 {
-       fetch_missing_extent_by_hash(hash);
        BEESTOOLONG("push_front_hash_addr hash " << BeesHash(hash) <<" addr " << BeesAddress(addr));
        auto lock = lock_extent_by_hash(hash);
        const auto er = get_cell_range(hash);
@@ -643,7 +552,6 @@ thread_local uniform_int_distribution<size_t> BeesHashTable::tl_distribution(0,
 bool
 BeesHashTable::push_random_hash_addr(HashType hash, AddrType addr)
 {
-       fetch_missing_extent_by_hash(hash);
        BEESTOOLONG("push_random_hash_addr hash " << BeesHash(hash) << " addr " << BeesAddress(addr));
        auto lock = lock_extent_by_hash(hash);
        auto er = get_cell_range(hash);
@@ -885,6 +793,23 @@ BeesHashTable::prepare()
        // mmap + size metadata at the final size.
        mmap_and_size_metadata();
 
+       // Load the whole table into the (anonymous) mapping up front, in the
+       // main thread, before any worker can look up a hash.  This replaces
+       // the old per-extent demand-load: random worker faults turned the
+       // read into scattered I/O, so a single sequential pass here is both
+       // faster to steady state and lets the lookup/insert paths assume the
+       // table is always resident.  Memory-only mode skips this —
+       // the pages are already zero from MAP_ANONYMOUS.
+       if (m_persistent) {
+               for (uint64_t ext = 0; ext < m_extents; ++ext) {
+                       BEESNOTE("loading hash extent #" << ext << " of " << m_extents);
+                       uint8_t *const extent_begin = m_extent_ptr[ext    ].p_byte;
+                       uint8_t *const extent_end   = m_extent_ptr[ext + 1].p_byte;
+                       pread_or_die(m_fd, extent_begin, extent_end - extent_begin, extent_begin - m_byte_ptr);
+                       BEESCOUNT(hash_extent_in);
+               }
+       }
+
        // Pre-fault and lock now, while we know which size the host has to
        // back.  prefetch_loop() used to do this on its first analyze pass,
        // but by then the mmap had already been allocated unconditionally
index 04cac36953545d7a6549875c04bbb51ce932d9f3..69f63562ffd4b95594a1e865823b1e91ac12d822 100644 (file)
@@ -835,9 +835,6 @@ private:
        RateLimiter             m_flush_rate_limit;
        BeesStringFile          m_stats_file;
 
-       // Prefetch readahead hint
-       bool                    m_prefetch_running = false;
-
        // Mutex/condvar for the writeback thread
        mutex                   m_dirty_mutex;
        condition_variable      m_dirty_condvar;
@@ -869,7 +866,6 @@ private:
        struct ExtentMetaData {
                shared_ptr<mutex> m_mutex_ptr;  ///< Serializes concurrent access to this extent.
                bool    m_dirty = false;         ///< True when the extent has unsaved modifications.
-               bool    m_missing = true;        ///< True when the extent has not yet been loaded from disk.
                ExtentMetaData();
        };
        vector<ExtentMetaData>  m_extent_metadata;
@@ -917,8 +913,6 @@ private:
        void try_mmap_flags(int flags);
        pair<Cell *, Cell *> get_cell_range(HashType hash);
        pair<uint8_t *, uint8_t *> get_extent_range(HashType hash);
-       void fetch_missing_extent_by_hash(HashType hash);
-       void fetch_missing_extent_by_index(uint64_t extent_index);
        void set_extent_dirty_locked(uint64_t extent_index);
        size_t flush_dirty_extents(bool slowly);
        void resize_file(off_t new_size);