From: Wilco Dijkstra Date: Mon, 18 Mar 2024 15:18:20 +0000 (+0000) Subject: stdlib: Add single-threaded fast path to rand() X-Git-Tag: glibc-2.42~450 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=be0cfd848d9ad7378800d6302bc11467cf2b514f;p=thirdparty%2Fglibc.git stdlib: Add single-threaded fast path to rand() 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  --- diff --git a/stdlib/random.c b/stdlib/random.c index 17cc61ba8f..5d482a8570 100644 --- a/stdlib/random.c +++ b/stdlib/random.c @@ -51,6 +51,7 @@ SUCH DAMAGE.*/ #include +#include #include #include #include @@ -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);