[PATCH v3] Add single-threaded fast path to rand()

Wilco Dijkstra Wilco.Dijkstra@arm.com
Mon Feb 24 14:01:52 GMT 2025


Hi Adhemerval,

>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.

It's a very common pitfall when one creates benchmarks and wants some randomized
inputs. Then they find out 90+% of the time is spent in rand() and not in the code they
try to benchmark... We've been asked plenty of times to improve rand() performance
by various customers, so this is just a quick fix for that common issue.

However using a simpler and faster LFSR without lots of global state would be even
better since that could just use an atomic for the state update and avoid locks. 
Rand() is not required to be perfectly random, let alone cryptographically safe...

Cheers,
Wilco


More information about the Libc-alpha mailing list