perf(random): use pre-allocated buffers for random

This commit is contained in:
Anna 2023-08-31 14:24:27 -04:00
parent fae0bbfc58
commit fae067bd5d
Signed by: anna
GPG Key ID: D0943384CD9F87D1
1 changed files with 10 additions and 4 deletions

View File

@ -494,6 +494,8 @@ fn main() -> Result<()> {
let mut sha1 = Sha1::default();
let mut buffer = itoa::Buffer::new();
let mut count_buffer = itoa::Buffer::new();
let mut random_bytes = [0; 16];
let mut random_hex = [0; 32];
let mut first = true;
while !found.load(Ordering::SeqCst) {
let append = if gpg.is_some() || seq_key.is_some() {
@ -501,9 +503,13 @@ fn main() -> Result<()> {
} else {
match method {
Method::Random => {
let mut bytes = [0; 16];
rand::thread_rng().fill(&mut bytes);
Some(Cow::from(data_encoding::HEXLOWER.encode(&bytes)))
rand::thread_rng().fill(&mut random_bytes);
data_encoding::HEXLOWER.encode_mut(&random_bytes, &mut random_hex);
let random = unsafe {
std::str::from_utf8_unchecked(&random_hex)
};
Some(Cow::from(random))
}
Method::Counter => {
let count = counter.fetch_add(1, Ordering::SeqCst) + 1;
@ -517,7 +523,7 @@ fn main() -> Result<()> {
let timestamp = match method {
Method::Decrement => timestamp.fetch_sub(1, Ordering::SeqCst) - 1,
Method::Increment => timestamp.fetch_add(1, Ordering::SeqCst) + 1,
_ => timestamp.load(Ordering::SeqCst),
_ => unreachable!(),
};
author_parts[author_len - 2] = buffer.format(timestamp).to_owned();