[PATCH 2/2] Add single-threaded fast path to rand()
Wilco Dijkstra
Wilco.Dijkstra@arm.com
Mon Mar 18 15:20:33 GMT 2024
Improve performance of rand() and __random() by adding a single-threaded fast
path. Bench-random-lock shows about 5x speedup on Neoverse V1.
OK for commit?
---
diff --git a/stdlib/random.c b/stdlib/random.c
index 62f22fac8d58c7977f09c134bf80a797750da645..174603a8915fd8aa4b3ae64d023003c9e2c038f2 100644
--- a/stdlib/random.c
+++ b/stdlib/random.c
@@ -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);
More information about the Libc-alpha
mailing list