]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
stdlib: Add single-threaded fast path to rand()
authorWilco Dijkstra <wilco.dijkstra@arm.com>
Mon, 18 Mar 2024 15:18:20 +0000 (15:18 +0000)
committerWilco Dijkstra <wilco.dijkstra@arm.com>
Mon, 24 Feb 2025 14:13:43 +0000 (14:13 +0000)
Improve performance of rand() and __random() by adding a single-threaded
fast path.  Bench-random-lock shows about 5x speedup on Neoverse V1.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
stdlib/random.c

index 17cc61ba8f55a2e17b3da847b5b7e51920bd7e21..5d482a857065161a80b195768bc86ad6cd81a97c 100644 (file)
@@ -51,6 +51,7 @@
    SUCH DAMAGE.*/
 
 #include <libc-lock.h>
+#include <sys/single_threaded.h>
 #include <limits.h>
 #include <stddef.h>
 #include <stdlib.h>
@@ -288,6 +289,12 @@ __random (void)
 {
   int32_t retval;
 
+  if (SINGLE_THREAD_P)
+    {
+      (void) __random_r (&unsafe_state, &retval);
+      return retval;
+    }
+
   __libc_lock_lock (lock);
 
   (void) __random_r (&unsafe_state, &retval);