perf: remember keys dropped from the kernel's `whnf` window in a Bloom filter Only keys dropped from the window can have been computed before, so the cache now records just those, in a fixed 1 MB Bloom filter allocated on the first drop, instead of the hashes of all inserted keys in a growing table. The long `decide +kernel` reductions of the `kernel_reduce_cache` benchmark take 1.08 s instead of 1.17 s (1.50 s with unbounded caches), with a third as many page faults. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
diff --git a/src/kernel/window_cache.h b/src/kernel/window_cache.h index 1f1fd1e..53469ca 100644 --- a/src/kernel/window_cache.h +++ b/src/kernel/window_cache.h
@@ -15,61 +15,61 @@ /* A cache in the hash table `Table` that keeps entries seen once only in a bounded window, and entries that were looked up again, or inserted again after leaving the window, without bound. The window holds two generations of at most `limit` entries each, like `two_gen_cache`. Keys - inserted before are remembered by their hash only, so a result computed once and never needed - again does not stay alive, while each result is computed at most twice. A hash collision merely - moves an entry into the unbounded table early. */ + dropped from the window are remembered in a fixed-size Bloom filter, so a result computed once + and never needed again does not stay alive, while each result is computed at most twice. A false + positive of the filter merely moves an entry into the unbounded table early. */ template<typename Table> class window_cache { public: typedef typename Table::key_type key_type; typedef typename Table::value_type value_type; private: + static constexpr unsigned filter_log_bits = 23; + static constexpr unsigned filter_num_probes = 3; Table m_hot; // window generation `m_new` and the older one `1 - m_new` Table m_window[2]; unsigned m_new = 0; size_t m_limit; - // open addressing set of the nonzero hashes of all keys inserted so far - uint64_t * m_seen = nullptr; - size_t m_seen_mask = 0; - size_t m_seen_size = 0; + // Bloom filter of the keys dropped from the window, allocated on the first drop + uint64_t * m_filter = nullptr; - static uint64_t mix(uint64_t h) { + uint64_t hash_of(key_type const & k) const { + uint64_t h = static_cast<uint64_t>(m_hot.hash_function()(k)); h *= 0xBF58476D1CE4E5B9ull; - h ^= h >> 31; - return h ? h : 1; + return h ^ (h >> 31); } - void grow_seen() { - size_t old_cap = m_seen ? m_seen_mask + 1 : 0; - size_t cap = old_cap ? 2 * old_cap : 1024; - uint64_t * seen = static_cast<uint64_t *>(std::calloc(cap, sizeof(uint64_t))); - if (!seen) - throw std::bad_alloc(); - for (size_t i = 0; i < old_cap; i++) { - if (uint64_t h = m_seen[i]) { - size_t j = h & (cap - 1); - while (seen[j]) - j = (j + 1) & (cap - 1); - seen[j] = h; - } - } - std::free(m_seen); - m_seen = seen; - m_seen_mask = cap - 1; + /* The bit index of probe `i` for `h`, by double hashing. */ + static size_t probe(uint64_t h, unsigned i) { + uint64_t h2 = (h >> 32) | 1; + return static_cast<size_t>((h + i * h2) & ((static_cast<uint64_t>(1) << filter_log_bits) - 1)); } - /* Records `h`, returning whether it was recorded before. */ - bool seen_before(uint64_t h) { - if (!m_seen || 2 * (m_seen_size + 1) > m_seen_mask + 1) - grow_seen(); - size_t i = h & m_seen_mask; - while (uint64_t g = m_seen[i]) { - if (g == h) - return true; - i = (i + 1) & m_seen_mask; + bool filter_contains(uint64_t h) const { + for (unsigned i = 0; i < filter_num_probes; i++) { + size_t b = probe(h, i); + if (!(m_filter[b / 64] & (static_cast<uint64_t>(1) << (b % 64)))) + return false; } - m_seen[i] = h; - m_seen_size++; - return false; + return true; + } + void filter_add(uint64_t h) { + for (unsigned i = 0; i < filter_num_probes; i++) { + size_t b = probe(h, i); + m_filter[b / 64] |= static_cast<uint64_t>(1) << (b % 64); + } + } + void drop_oldest_generation() { + Table & oldest = m_window[1 - m_new]; + if (!m_filter) { + m_filter = static_cast<uint64_t *>(std::calloc((static_cast<size_t>(1) << filter_log_bits) / 64, sizeof(uint64_t))); + if (!m_filter) + throw std::bad_alloc(); + } + for (value_type const & e : oldest) + filter_add(hash_of(e.first)); + m_new = 1 - m_new; + // keeps the bucket array of the dropped generation + oldest.clear(); } value_type const * promote(Table & gen, typename Table::iterator it) { value_type v = *it; @@ -79,7 +79,7 @@ public: explicit window_cache(size_t limit):m_limit(limit) {} window_cache(window_cache const &) = delete; - ~window_cache() { std::free(m_seen); } + ~window_cache() { std::free(m_filter); } /* The entry for `k`, or `nullptr`; valid until the next `find` or `insert`. */ value_type const * find(key_type const & k) { auto it = m_hot.find(k); @@ -97,15 +97,13 @@ } template<typename V> void insert(V && v) { - if (seen_before(mix(m_hot.hash_function()(v.first)))) { + // only keys dropped from the window can have been computed before + if (m_filter && filter_contains(hash_of(v.first))) { m_hot.insert(std::forward<V>(v)); return; } - if (m_window[m_new].size() >= m_limit) { - m_new = 1 - m_new; - // keeps the bucket array of the dropped generation - m_window[m_new].clear(); - } + if (m_window[m_new].size() >= m_limit) + drop_oldest_generation(); m_window[m_new].insert(std::forward<V>(v)); } };