-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reduce number of syscalls in rand
#53725
Conversation
In case that it is statically known that the OS doesn't support `getrandom` (non-Linux) or becomes clear at runtime that `getrandom` isn't available (`ENOSYS`), the opened fd ("/dev/urandom") isn't closed after the function, so that future calls can reuse it. This saves repeated `open`/`close` system calls at the cost of one permanently open fd. Additionally, this skips the initial zero-length `getrandom` call and directly hands the user buffer to the operating system, saving one `getrandom` syscall.
(rust_highfive has picked a reviewer for you, use r? to override) |
Program:
Without this PR:
With this PR:
The initial |
Thanks for the PR! The changes to |
Done. |
@bors: r |
📌 Commit 6d47737 has been approved by |
…richton Reduce number of syscalls in `rand` This skips the initial zero-length `getrandom` call and directly hands the user buffer to the operating system, saving one `getrandom` syscall.
@bors r-
|
6d47737
to
d6d280b
Compare
let mut read = 0; | ||
while read < v.len() { | ||
let result = getrandom(&mut v[read..]); | ||
if result == -1 { | ||
let err = errno() as libc::c_int; | ||
if err == libc::EINTR { | ||
continue; | ||
} else if err == libc::ENOSYS { | ||
GETRANDOM_UNAVAILABLE.store(true, Ordering::Relaxed); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this not loop infinitely in the ENOSYS case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh my...
@bors: r |
📌 Commit b95c491 has been approved by |
Reduce number of syscalls in `rand` This skips the initial zero-length `getrandom` call and directly hands the user buffer to the operating system, saving one `getrandom` syscall.
☀️ Test successful - status-appveyor, status-travis |
This skips the initial zero-length
getrandom
call anddirectly hands the user buffer to the operating system, saving one
getrandom
syscall.