Repository navigation
Improve performance of SystemNative_GetNonCryptographicallySecureRandomBytes - #27601
Conversation
…omBytes Based on the same change in coreclr. Improves the performance 6 times. Main improvement comes from using just one read per the whole buffer instead of one per byte. Smaller part of the win comes from using only the /dev/urandom.
fd1b73b to
01f5a7e
Compare
|
|
||
| int rand_des; | ||
| char buf; | ||
| uint8_t* buf = (uint8_t*)alloca(bufferLength); |
There was a problem hiding this comment.
Why do we need this? This seems problematic: if you pass in a large buffer, you could stack overflow. Why can't we just read into the original buffer and then update it in place with the xor'ing?
There was a problem hiding this comment.
This was the semantics of the original code which I haven't changed and in coreclr, it was like that even before it was open sourced. But I agree there is not much benefit in doing it and removing it would have the nice consequence of not having to allocate the buffer.
I was thinking about the potential of stack overflow before too, but since this function is ever used only for tiny buffers in our code base and it is internal, it seemed ok.
I'll change it based on your suggestion.
| } | ||
|
|
||
| *(buffer + i) ^= num; | ||
| *(buffer + i) ^= buf[i] ^ num; |
There was a problem hiding this comment.
Is this xor'ing really necessary?
|
Actually, I've just got another idea - keeping the FD for the /dev/urandom open all the time. This further improves the performance twice. |
| // in case /dev/urandom is not really random | ||
|
|
||
| for(i = 0; i < bufferLength; i++) | ||
| for (int i = 0; i < bufferLength; i++) |
There was a problem hiding this comment.
I'm still not clear why this xor'ing is necessary, why the data we get back from urandom is insufficient.
There was a problem hiding this comment.
This was done back in the Rotor/Silveright days. At that time, we have seen cases where the random device was just returning non-random data on some systems under certain circumstances due to OS bugs. This was mitigating them.
There was a problem hiding this comment.
@stephentoub, @jkotas what do you think about removing it then?
There was a problem hiding this comment.
I would need to add fail fast if the read failed for some obscure reason in that case.
There was a problem hiding this comment.
If the function is changed to return error and all places are changed to do something about the error, I would be ok with removing it.
There was a problem hiding this comment.
I'll keep it in. The perf difference between having and not having it is just 2%
By keeping the file descriptor for /dev/urandom open permanently, another 2x perf improvement was achieved. The total perf improvement measured on my physical machine is now 13.75x.
9388708 to
78203ca
Compare
| if (rand_des == -1) | ||
| { | ||
| if (errno == ENOENT) | ||
| int fd = open("/dev/urandom", O_RDONLY, O_CLOEXEC); |
There was a problem hiding this comment.
Is it thread safe to read concurrently from this now shared fd?
There was a problem hiding this comment.
See the "Bugs" section in http://man7.org/linux/man-pages/man2/read.2.html. It says that from Linux >= 3.14, it is thread safe.
On Linux < 3.14, the doc says that multiple threads can possibly obtain blocks that overlap.
But looking at the kernel sources, the random data extraction has a spinlock around it, so it should be safe even on the older Linux kernels.
There was a problem hiding this comment.
As for the read functions, according to their man pages, they should both conform to POSIX 1 standard, like on Linux. That means that the operation should thread safe there too. However, POSIX talks only about regular files, not pseudo files like /dev/urandom.
Looking at FreeBSD sources, there is a lock around reading from the random generator in the /dev/urandom handler.
And the same is true for macOS source code (checked in the 10.12 version).
| while (offset != bufferLength); | ||
|
|
||
| close(rand_des); | ||
| assert(offset == bufferLength); |
There was a problem hiding this comment.
Nit, this is asserting the line above, does not seem worth it.
There was a problem hiding this comment.
The loop has a break in it, so in case of an error in the read which should not happen, the assert would fire.
There was a problem hiding this comment.
Should we just move the assert (with a different condition) to where the break is instead then?
There was a problem hiding this comment.
Done, it makes more sense.
| for( ; i < bufferLength; i++) | ||
| { | ||
| if (read(rand_des, &buf, 1) < 1) | ||
| if (!__sync_bool_compare_and_swap(&rand_des, -1, fd)) |
There was a problem hiding this comment.
How many handles does have a small .NET process opened (e.g. Console.ReadLine)? Just want to understand the relative cost of one more permanently open handle.
There was a problem hiding this comment.
There are 21 handles open without my change, so that would be 23 with it (one in corefx, one in coreclr)
|
@dotnet-bot test OSX x64 Debug Build please |
1 similar comment
|
@dotnet-bot test OSX x64 Debug Build please |
|
@dotnet-bot test OSX x64 Debug Build please |
|
Thanks, Jan. |
…omBytes (dotnet/corefx#27601) * Improve performance of SystemNative_GetNonCryptographicallySecureRandomBytes Based on the same change in coreclr. Improves the performance 6 times. Main improvement comes from using just one read per the whole buffer instead of one per byte. Smaller part of the win comes from using only the /dev/urandom. * Remove alloca, generate right to the buffer * Further 2x perf improvement By keeping the file descriptor for /dev/urandom open permanently, another 2x perf improvement was achieved. The total perf improvement measured on my physical machine is now 13.75x. * Handle EINTR for the open right away * Move assert to the read error location Commit migrated from dotnet/corefx@52b6280
Based on the same change in coreclr. Improves the performance 6 times.
Main improvement comes from using just one read per the whole buffer
instead of one per byte. Smaller part of the win comes from using only
the /dev/urandom.