refactor: prevent some allocations for perf

This commit is contained in:
Anna 2023-08-30 23:28:28 -04:00
parent fae04bd975
commit fae04ba3b8
Signed by: anna
GPG Key ID: D0943384CD9F87D1
1 changed files with 13 additions and 13 deletions

View File

@ -456,12 +456,9 @@ fn main() -> Result<()> {
let mut sha1 = Sha1::default();
let mut buffer = itoa::Buffer::new();
let mut count_buffer = itoa::Buffer::new();
let mut first = true;
loop {
if found.load(Ordering::SeqCst) {
break;
}
while !found.load(Ordering::SeqCst) {
let append = if gpg.is_some() || seq_key.is_some() {
None
} else {
@ -469,11 +466,11 @@ fn main() -> Result<()> {
Method::Random => {
let mut bytes = [0; 16];
rand::thread_rng().fill(&mut bytes);
Some(data_encoding::HEXLOWER.encode(&bytes))
Some(Cow::from(data_encoding::HEXLOWER.encode(&bytes)))
}
Method::Counter => {
let count = counter.fetch_add(1, Ordering::SeqCst) + 1;
Some(buffer.format(count).to_owned())
Some(Cow::from(count_buffer.format(count)))
}
_ => None,
}
@ -495,18 +492,21 @@ fn main() -> Result<()> {
if method == Method::Header {
let count = counter.fetch_add(1, Ordering::SeqCst) + 1;
let line = format!("xvain {}", buffer.format(count));
if first {
header_lines.insert(committer_idx + 1, line);
} else {
header_lines[committer_idx + 1] = line;
header_lines.insert(committer_idx + 1, String::with_capacity(16));
first = false;
}
first = false;
let line = &mut header_lines[committer_idx + 1];
line.clear();
line.push_str("xvain ");
line.push_str(buffer.format(count));
}
let mut header = header_lines.join("\n");
// NOTE: don't need to handle append here, since we'll never be
// both appending *and* signing
if let Some(ctx) = &mut gpg {
let to_sign = format!("{header}\n{message}");
let mut output = Vec::new();
@ -570,7 +570,7 @@ fn main() -> Result<()> {
Some(a) => {
sha1.update(&message);
sha1.update("\n");
sha1.update(a);
sha1.update(a.as_bytes());
sha1.update("\n");
}
None => sha1.update(&message),