]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: memprof: attempt different retry slots for different hashes on collision
authorWilly Tarreau <w@1wt.eu>
Wed, 11 Mar 2026 15:33:33 +0000 (16:33 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 12 Mar 2026 17:06:37 +0000 (18:06 +0100)
When two pointer hash to the same memprofile bin, we currently try again
with the same bin until we find a spare one or we reach the limit of 16.
Olivier suggested to try with a different step for different pointers so
as to limit the number of bins to visit in such a case, so let's split
the pointer hash calculation so that we keep the raw hash before reduction
and use its lowest bits as the retry step. We force lowest bit to 1 to
avoid integral multiples that would oscillate between only a few positions.

Quick tests with h1+h2 requests show that for ~744 distinct entries, we
used to have 1.17 retries per lookup before and 0.6 now so we're halving
the cost of hash collisions. A heavier workload that used to produce 920
entries with 2.01 retries per lookup now reaches 966 entries (94.3% usage
vs 89.8% before) with only 1.44 retries per lookup.

This should be safe to backport, but depends on this previous commit:

    MINOR: tools: extend the pointer hashing code to ease manipulations

src/activity.c

index c55e3136f1e399ac6d244b66f64e2a6a21737712..d270a6500b8a5246ce29bf40b6e6a06d3784068c 100644 (file)
@@ -302,13 +302,15 @@ struct memprof_stats *memprof_get_bin(const void *ra, enum memprof_method meth)
        int retries = 16; // up to 16 consecutive entries may be tested.
        const void *old;
        unsigned int bin;
+       ullong hash;
 
        if (unlikely(!ra)) {
                bin = MEMPROF_HASH_BUCKETS;
                goto leave;
        }
-       bin = ptr_hash(ra, MEMPROF_HASH_BITS);
-       for (; memprof_stats[bin].caller != ra; bin = (bin + 1) & (MEMPROF_HASH_BUCKETS - 1)) {
+       hash = _ptr_hash(ra);
+       bin = _ptr_hash_reduce(hash, MEMPROF_HASH_BITS);
+       for (; memprof_stats[bin].caller != ra; bin = (bin + (hash | 1)) & (MEMPROF_HASH_BUCKETS - 1)) {
                if (!--retries) {
                        bin = MEMPROF_HASH_BUCKETS;
                        break;