Repository: bitcoin/bitcoin
File: src/cuckoocache.h (C/C++)

Code before:
        // Set to 45% as described above
        epoch_size = std::max(uint32_t{1}, (45 * size) / 100);
        // Initially set to wait for a whole epoch
        epoch_heuristic_counter = epoch_size;
        return size;
    }

    /** setup_bytes is a convenience function which accounts for internal memory
     * usage when deciding how many elements to store. It isn't perfect because
     * it doesn't account for any overhead (struct size, MallocUsage, collection
     * and epoch flags). This was done to simplify selecting a power of two
     * size. In the expected use case, an extra two bits per entry should be
     * negligible compared to the size of the elements.
     *
     * @param bytes the approximate number of bytes to use for this data
     * structure
     * @returns A pair of the maximum number of elements storable (see setup()
     * documentation for more detail) and the approximate total size of these
     * elements in bytes.
     */
    std::pair<uint32_t, size_t> setup_bytes(size_t bytes)
    {
        size_t requested_num_elems = bytes / sizeof(Element);
        if (std::numeric_limits<uint32_t>::max() < requested_num_elems) {
            requested_num_elems = std::numeric_limits<uint32_t>::max();
        }

        auto num_elems = setup(requested_num_elems);

        size_t approx_size_bytes = num_elems * sizeof(Element);
        return std::make_pair(num_elems, approx_size_bytes);
    }

    /** insert loops at most depth_limit times trying to insert a hash
     * at various locations in the table via a variant of the Cuckoo Algorithm
     * with eight hash locations.
     *
     * It drops the last tried element if it runs out of depth before
     * encountering an open slot.
     *
     * Thus:
     *
     * ```
     * insert(x);
     * return contains(x, false);
     * ```
     *
     * is not guaranteed to return true.
     *
     * @param e the element to insert
     * @post one of the following: All previously inserted elements and e are

Review comment (performance):
nit: declaring `requested_num_elems` as a `uint32_t` would seem more intuitive:

<details>
<summary>git diff on 8960bc986b</summary>

```diff
diff --git a/src/cuckoocache.h b/src/cuckoocache.h
index c9bd45befc..85556a4da5 100644
--- a/src/cuckoocache.h
+++ b/src/cuckoocache.h
@@ -364,10 +364,10 @@ public:
      */
     std::pair<uint32_t, size_t> setup_bytes(size_t bytes)
     {
-        size_t requested_num_elems = bytes / sizeof(Element);
-        if (std::numeric_limits<uint32_t>::max() < requested_num_elems) {
-            requested_num_elems = std::numeric_limits<uint32_t>::max();
-        }
+        uint32_t requested_num_elems{(uint32_t)std::min(
+            bytes / sizeof(Element),
+            (size_t)std::numeric_limits<uint32_t>::max()
+        )};
 
         auto num_elems = setup(requested_num_elems);
 

```
</details>


Code after:
        epoch_size = std::max(uint32_t{1}, (45 * size) / 100);
        // Initially set to wait for a whole epoch
        epoch_heuristic_counter = epoch_size;
        return size;
    }

    /** setup_bytes is a convenience function which accounts for internal memory
     * usage when deciding how many elements to store. It isn't perfect because
     * it doesn't account for any overhead (struct size, MallocUsage, collection
     * and epoch flags). This was done to simplify selecting a power of two
     * size. In the expected use case, an extra two bits per entry should be
     * negligible compared to the size of the elements.
     *
     * @param bytes the approximate number of bytes to use for this data
     * structure
     * @returns A pair of the maximum number of elements storable (see setup()
     * documentation for more detail) and the approximate total size of these
     * elements in bytes.
     */
    std::pair<uint32_t, size_t> setup_bytes(size_t bytes)
    {
        uint32_t requested_num_elems(std::min<size_t>(
            bytes / sizeof(Element),
            std::numeric_limits<uint32_t>::max()));

        auto num_elems = setup(requested_num_elems);

        size_t approx_size_bytes = num_elems * sizeof(Element);
        return std::make_pair(num_elems, approx_size_bytes);
    }

    /** insert loops at most depth_limit times trying to insert a hash
     * at various locations in the table via a variant of the Cuckoo Algorithm
     * with eight hash locations.
     *
     * It drops the last tried element if it runs out of depth before
     * encountering an open slot.
     *
     * Thus:
     *
     * ```
     * insert(x);
     * return contains(x, false);
     * ```
     *
     * is not guaranteed to return true.
     *
     * @param e the element to insert
     * @post one of the following: All previously inserted elements and e are
     * now in the table, one previously inserted element is evicted from the
     * table, the entry attempted to be inserted is evicted.