From b2475a139e69ba7792054233faee946be7f83ca9 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Sun, 9 May 2021 10:26:14 +0200 Subject: [PATCH] MINOR: tools/rnd: compute the result outside of the CAS loop ha_random64() uses a DWCAS loop to produce the random, but it computes the resulting value inside the loop while it doesn't change upon success, so this is a needless overhead inside the critcal path that participates to making threads fail the race and try again. Let's take the value out of the loop. --- src/tools.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/tools.c b/src/tools.c index de5dfa8a3..718bb49b8 100644 --- a/src/tools.c +++ b/src/tools.c @@ -5021,7 +5021,6 @@ static uint64_t ha_random_state[2] ALIGNED(2*sizeof(uint64_t)); */ uint64_t ha_random64() { - uint64_t result; uint64_t old[2] ALIGNED(2*sizeof(uint64_t)); uint64_t new[2] ALIGNED(2*sizeof(uint64_t)); @@ -5037,7 +5036,6 @@ uint64_t ha_random64() #if defined(USE_THREAD) && defined(HA_CAS_IS_8B) && defined(HA_HAVE_CAS_DW) do { #endif - result = rotl64(old[0] * 5, 7) * 9; new[1] = old[0] ^ old[1]; new[0] = rotl64(old[0], 24) ^ new[1] ^ (new[1] << 16); // a, b new[1] = rotl64(new[1], 37); // c @@ -5051,7 +5049,7 @@ uint64_t ha_random64() HA_SPIN_UNLOCK(OTHER_LOCK, &rand_lock); #endif #endif - return result; + return rotl64(old[0] * 5, 7) * 9; } /* seeds the random state using up to bytes from , starting with