Skip to content
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

ref: use SmallRng #119

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ description = "A library for managing temporary files and directories."

[dependencies]
cfg-if = "1"
rand = "0.8"
rand = { version = "0.8", features = ["small_rng", "getrandom"], default_features = false }
remove_dir_all = "0.5"

[target.'cfg(unix)'.dependencies]
Expand Down
20 changes: 9 additions & 11 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
use rand::distributions::Alphanumeric;
use rand::{self, Rng};
use rand::{Rng, SeedableRng};
use rand::rngs::SmallRng;
use std::ffi::{OsStr, OsString};
use std::path::{Path, PathBuf};
use std::{io, str};
use std::io;

use crate::error::IoResultExt;

fn tmpname(prefix: &OsStr, suffix: &OsStr, rand_len: usize) -> OsString {
let mut buf = OsString::with_capacity(prefix.len() + suffix.len() + rand_len);
buf.push(prefix);

// Push each character in one-by-one. Unfortunately, this is the only
// safe(ish) simple way to do this without allocating a temporary
// String/Vec.
unsafe {
rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(rand_len)
.for_each(|b| buf.push(str::from_utf8_unchecked(&[b as u8])))
}
let small_rng = SmallRng::from_entropy();
buf.push(small_rng
.sample_iter(&Alphanumeric)
.take(rand_len)
.collect::<String>()
Comment on lines +16 to +18
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
.sample_iter(&Alphanumeric)
.take(rand_len)
.collect::<String>()
.sample_iter(Alphanumeric)
.take(rand_len)
.map(char::from)
.collect::<String>()

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's still going to allocate, unfortunately.

Copy link
Contributor

@taiki-e taiki-e Dec 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look for a way to avoid the allocation.

EDIT: See #119 (comment)

);
Comment on lines +14 to +19
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can avoid allocation by using char::encode_utf8.
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=421dfa2eebe631075a03b06e08c6e7cf

Suggested change
let small_rng = SmallRng::from_entropy();
buf.push(small_rng
.sample_iter(&Alphanumeric)
.take(rand_len)
.collect::<String>()
);
let mut tmp = [0; 1];
let small_rng = SmallRng::from_entropy();
small_rng
.sample_iter(Alphanumeric)
.take(rand_len)
.for_each(|c| buf.push((c as char).encode_utf8(&mut tmp)));

Can we probably resolve performance issues in combination with putting a smallrng in a thread-local variable?

buf.push(suffix);
buf
}
Expand Down