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

Code before:
        table.resize(size);
        collection_flags.setup(size);
        epoch_flags.resize(size);
        // 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)
    {
        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);

        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.
     *

Review comment (suggestion):
nit: You can combine the cast by using `()` over `{}`. Also you can specify the template to avoid the `size_t` cast, and ensure the right template is always picked:

```cpp
uint32_t requested_num_elems(std::min<size_t>(

Code after:
        collection_flags.setup(size);
        epoch_flags.resize(size);
        // 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)
    {
        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