[PATCH v3] Add single-threaded fast path to rand()
Adhemerval Zanella Netto
adhemerval.zanella@linaro.org
Fri Feb 21 18:42:42 GMT 2025
On 29/07/24 11:23, Wilco Dijkstra wrote:
> Improve performance of rand() and __random() by adding a single-threaded fast
> path. Bench-random-lock shows about 5x speedup on Neoverse V1.
LGTM, although I am not sure which kind of workload does really
benefit from this optimization.
>
> ---
>
> 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