From 2f6fb815816127f9c9b8724dccab04d543af7d24 Mon Sep 17 00:00:00 2001 From: Jan Vorlicek Date: Wed, 2 May 2018 19:34:29 +0200 Subject: [PATCH] Port to 2.1 - Fix performance regression in Guid.NewGuid on OSX 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. --- src/Native/Unix/Common/pal_config.h.in | 1 + src/Native/Unix/System.Native/pal_random.cpp | 5 +++++ src/Native/Unix/configure.cmake | 4 ++++ 3 files changed, 10 insertions(+) diff --git a/src/Native/Unix/Common/pal_config.h.in b/src/Native/Unix/Common/pal_config.h.in index 6099d2f32ca6..157cb4bf782b 100644 --- a/src/Native/Unix/Common/pal_config.h.in +++ b/src/Native/Unix/Common/pal_config.h.in @@ -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 diff --git a/src/Native/Unix/System.Native/pal_random.cpp b/src/Native/Unix/System.Native/pal_random.cpp index e6bda4490ab0..d0b3330f7338 100644 --- a/src/Native/Unix/System.Native/pal_random.cpp +++ b/src/Native/Unix/System.Native/pal_random.cpp @@ -11,6 +11,7 @@ #include #include +#include "pal_config.h" /* Generate random bytes. The generated bytes are not cryptographically strong. @@ -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; @@ -93,4 +97,5 @@ extern "C" void SystemNative_GetNonCryptographicallySecureRandomBytes(uint8_t* b *(buffer + i) ^= num; num >>= 8; } +#endif // HAS_ARC4RANDOM } diff --git a/src/Native/Unix/configure.cmake b/src/Native/Unix/configure.cmake index 7b885239bdfc..f4a30ad6cb29 100644 --- a/src/Native/Unix/configure.cmake +++ b/src/Native/Unix/configure.cmake @@ -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"