]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
OPTIM: ring: don't even try to update offset when failed to read
authorWilly Tarreau <w@1wt.eu>
Thu, 29 Feb 2024 10:57:28 +0000 (11:57 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 25 Mar 2024 17:34:19 +0000 (17:34 +0000)
If there's nothing to read, it's pointless for a reader to try to update
the offset pointer, that's two atomic ops to replace a value by itself
twice. Let's just stop this.

src/ring.c

index 826662aea3780b72f3a1779050b769cbf466ab07..31b75a13fed3d08dd3d2ff8dd6848443a847746b 100644 (file)
@@ -538,17 +538,19 @@ int ring_dispatch_messages(struct ring *ring, void *ctx, size_t *ofs_ptr, size_t
 
        vp_data_to_ring(v1, v2, (char *)ring_area, ring_size, &head_ofs, &tail_ofs);
 
-       /* inc readers count on new place */
-       do {
-               readers = _HA_ATOMIC_LOAD(ring_area + head_ofs);
-       } while ((readers > RING_MAX_READERS ||
-                 !_HA_ATOMIC_CAS(ring_area + head_ofs, &readers, readers + 1)) && __ha_cpu_relax());
+       if (head_ofs != prev_ofs) {
+               /* inc readers count on new place */
+               do {
+                       readers = _HA_ATOMIC_LOAD(ring_area + head_ofs);
+               } while ((readers > RING_MAX_READERS ||
+                         !_HA_ATOMIC_CAS(ring_area + head_ofs, &readers, readers + 1)) && __ha_cpu_relax());
 
-       /* dec readers count on old place */
-       do {
-               readers = _HA_ATOMIC_LOAD(ring_area + prev_ofs);
-       } while ((readers > RING_MAX_READERS ||
-                 !_HA_ATOMIC_CAS(ring_area + prev_ofs, &readers, readers - 1)) && __ha_cpu_relax());
+               /* dec readers count on old place */
+               do {
+                       readers = _HA_ATOMIC_LOAD(ring_area + prev_ofs);
+               } while ((readers > RING_MAX_READERS ||
+                         !_HA_ATOMIC_CAS(ring_area + prev_ofs, &readers, readers - 1)) && __ha_cpu_relax());
+       }
 
        if (last_ofs_ptr)
                *last_ofs_ptr = tail_ofs;