From 655bebcdec6d89f0ffa33d794069ee5eee0df3e5 Mon Sep 17 00:00:00 2001 From: Joel Natividad <1980690+jqnatividad@users.noreply.github.com> Date: Sat, 7 Oct 2023 06:37:59 -0400 Subject: [PATCH] `frequency`: optimize allocations before hot loop --- src/cmd/frequency.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/cmd/frequency.rs b/src/cmd/frequency.rs index bd74c2da8..c751601ae 100644 --- a/src/cmd/frequency.rs +++ b/src/cmd/frequency.rs @@ -190,11 +190,14 @@ impl Args { { let null = &b""[..].to_vec(); let nsel = sel.normal(); - let mut tabs: Vec<_> = (0..nsel.len()).map(|_| Frequencies::new()).collect(); + let nsel_len = nsel.len(); + let mut tabs: Vec<_> = (0..nsel_len).map(|_| Frequencies::new()).collect(); + #[allow(unused_assignments)] - // amortize allocation - let mut field_work: Vec = Vec::with_capacity(100); - let mut row_work: csv::ByteRecord = csv::ByteRecord::default(); + // amortize allocations + let mut field_work: Vec = Vec::with_capacity(nsel_len); + let mut row_work: csv::ByteRecord = csv::ByteRecord::with_capacity(200, nsel_len); + let flag_no_nulls = self.flag_no_nulls; for row in it { row_work.clone_from(&row?);