Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 6906dbf

Browse files
janvorlistephentoub
authored andcommitted
Port to 2.1 - Fix performance regression in Guid.NewGuid on OSX (#29457)
We have recently removed dependency on the libuuid for Guid creation. Unfortunately, while the perf on Linux has improved, we have not noticed that the perf on OSX degraded 10 fold. This change fixes it by modifying the implementation of the underlying SystemNative_GetNonCryptographicallySecureRandomBytes to use arc4random like the uuid_generate_random that we were using before the libuuid dependency removal does.
1 parent 484f904 commit 6906dbf

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

src/Native/Unix/Common/pal_config.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#cmakedefine01 HAVE_TIOCGWINSZ
2929
#cmakedefine01 HAVE_SCHED_GETAFFINITY
3030
#cmakedefine01 HAVE_SCHED_SETAFFINITY
31+
#cmakedefine01 HAVE_ARC4RANDOM
3132
#cmakedefine01 KEVENT_HAS_VOID_UDATA
3233
#cmakedefine01 HAVE_FDS_BITS
3334
#cmakedefine01 HAVE_PRIVATE_FDS_BITS

src/Native/Unix/System.Native/pal_random.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <time.h>
1212
#include <errno.h>
1313

14+
#include "pal_config.h"
1415
/*
1516
1617
Generate random bytes. The generated bytes are not cryptographically strong.
@@ -20,6 +21,9 @@ extern "C" void SystemNative_GetNonCryptographicallySecureRandomBytes(uint8_t* b
2021
{
2122
assert(buffer != NULL);
2223

24+
#if HAVE_ARC4RANDOM
25+
arc4random_buf(buffer, (size_t)bufferLength);
26+
#else
2327
static volatile int rand_des = -1;
2428
long num = 0;
2529
static bool sMissingDevURandom;
@@ -93,4 +97,5 @@ extern "C" void SystemNative_GetNonCryptographicallySecureRandomBytes(uint8_t* b
9397
*(buffer + i) ^= num;
9498
num >>= 8;
9599
}
100+
#endif // HAS_ARC4RANDOM
96101
}

src/Native/Unix/configure.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ check_function_exists(
140140
sched_setaffinity
141141
HAVE_SCHED_SETAFFINITY)
142142

143+
check_function_exists(
144+
arc4random
145+
HAVE_ARC4RANDOM)
146+
143147
check_symbol_exists(
144148
TIOCGWINSZ
145149
"sys/ioctl.h"

0 commit comments

Comments
 (0)