public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 2/2] Add single-threaded fast path to rand()
@ 2024-03-18 15:20 Wilco Dijkstra
  2024-03-18 19:24 ` Cristian Rodríguez
  0 siblings, 1 reply; 27+ messages in thread
From: Wilco Dijkstra @ 2024-03-18 15:20 UTC (permalink / raw)
  To: 'GNU C Library'

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);




^ permalink raw reply	[flat|nested] 27+ messages in thread
* [PATCH 2/2] Add single-threaded fast path to rand()
@ 2023-11-28 17:37 Wilco Dijkstra
  0 siblings, 0 replies; 27+ messages in thread
From: Wilco Dijkstra @ 2023-11-28 17:37 UTC (permalink / raw)
  To: 'GNU C Library'


Improve performance of rand() and __random() by adding a single-threaded fast
path.  Bench-random-lock shows about 5x speedup on Neoverse V1.

---

diff --git a/stdlib/random.c b/stdlib/random.c
index 62f22fac8d58c7977f09c134bf80a797750da645..a22de60a0f96031c74dd5a949b6717c2b0fc321a 100644
--- a/stdlib/random.c
+++ b/stdlib/random.c
@@ -51,6 +51,7 @@
    SUCH DAMAGE.*/
 
 #include <libc-lock.h>
+#include <single-thread.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);


^ permalink raw reply	[flat|nested] 27+ messages in thread

end of thread, other threads:[~2024-03-25 17:52 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-18 15:20 [PATCH 2/2] Add single-threaded fast path to rand() Wilco Dijkstra
2024-03-18 19:24 ` Cristian Rodríguez
2024-03-19 15:44   ` Wilco Dijkstra
2024-03-20 12:31     ` Adhemerval Zanella Netto
2024-03-20 14:18       ` Cristian Rodríguez
2024-03-20 14:27         ` Adhemerval Zanella Netto
2024-03-21  7:39           ` Florian Weimer
2024-03-21 13:33             ` Cristian Rodríguez
2024-03-21 14:35               ` Mathieu Desnoyers
2024-03-21 15:07                 ` Szabolcs Nagy
2024-03-21 15:18                   ` Wilco Dijkstra
2024-03-21 15:53               ` Adhemerval Zanella Netto
2024-03-22 14:27                 ` Zack Weinberg
2024-03-22 14:46                   ` Adhemerval Zanella Netto
2024-03-22 15:30                     ` Zack Weinberg
2024-03-22 18:05                       ` Adhemerval Zanella Netto
2024-03-22 19:47                         ` Mathieu Desnoyers
2024-03-22 22:54                           ` Cristian Rodríguez
2024-03-23 14:01                           ` Zack Weinberg
2024-03-23 15:23                             ` Mathieu Desnoyers
2024-03-25 14:09                               ` Mathieu Desnoyers
2024-03-25 17:52                                 ` Adhemerval Zanella Netto
2024-03-24  0:59                             ` Cristian Rodríguez
2024-03-25  6:44                             ` Florian Weimer
2024-03-20 14:28         ` Wilco Dijkstra
2024-03-20 14:40           ` Xi Ruoyao
  -- strict thread matches above, loose matches on Subject: below --
2023-11-28 17:37 Wilco Dijkstra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).