From 1f8b14b7be51efcfe3c57fe9aecdc88c7acf3f5c Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Thu, 29 Feb 2024 11:57:28 +0100 Subject: [PATCH] OPTIM: ring: don't even try to update offset when failed to read 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 | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/ring.c b/src/ring.c index 826662aea3..31b75a13fe 100644 --- a/src/ring.c +++ b/src/ring.c @@ -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; -- 2.47.3