Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Port to 2.1 - Fix performance regression in Guid.NewGuid on OSX (#29457)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
janvorli authored and stephentoub committed May 2, 2018
1 parent 484f904 commit 6906dbf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Native/Unix/Common/pal_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#cmakedefine01 HAVE_TIOCGWINSZ
#cmakedefine01 HAVE_SCHED_GETAFFINITY
#cmakedefine01 HAVE_SCHED_SETAFFINITY
#cmakedefine01 HAVE_ARC4RANDOM
#cmakedefine01 KEVENT_HAS_VOID_UDATA
#cmakedefine01 HAVE_FDS_BITS
#cmakedefine01 HAVE_PRIVATE_FDS_BITS
Expand Down
5 changes: 5 additions & 0 deletions src/Native/Unix/System.Native/pal_random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <time.h>
#include <errno.h>

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

#if HAVE_ARC4RANDOM
arc4random_buf(buffer, (size_t)bufferLength);
#else
static volatile int rand_des = -1;
long num = 0;
static bool sMissingDevURandom;
Expand Down Expand Up @@ -93,4 +97,5 @@ extern "C" void SystemNative_GetNonCryptographicallySecureRandomBytes(uint8_t* b
*(buffer + i) ^= num;
num >>= 8;
}
#endif // HAS_ARC4RANDOM
}
4 changes: 4 additions & 0 deletions src/Native/Unix/configure.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ check_function_exists(
sched_setaffinity
HAVE_SCHED_SETAFFINITY)

check_function_exists(
arc4random
HAVE_ARC4RANDOM)

check_symbol_exists(
TIOCGWINSZ
"sys/ioctl.h"
Expand Down

0 comments on commit 6906dbf

Please sign in to comment.