Skip to content

Commit 1a3a422

Browse files
committed
refactor: remove unnecessary amortized itoa & ryu buffer allocations
per itoa & ryu docs - `Buffer::new()` "is a cheap operation; you don’t need to worry about reusing buffers for efficiency."
1 parent 13e347d commit 1a3a422

File tree

9 files changed

+28
-32
lines changed

9 files changed

+28
-32
lines changed

src/cmd/enumerate.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,6 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
243243
// amortize allocations
244244
let mut record = csv::ByteRecord::new();
245245
let mut counter: u64 = args.flag_start;
246-
let mut itoa_buffer = itoa::Buffer::new();
247246
#[allow(unused_assignments)]
248247
let mut colcopy: Vec<u8> = Vec::with_capacity(20);
249248
let increment = args.flag_increment.unwrap_or(1);
@@ -255,7 +254,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
255254
while rdr.read_byte_record(&mut record)? {
256255
match enum_operation {
257256
EnumOperation::Increment => {
258-
record.push_field(itoa_buffer.format(counter).as_bytes());
257+
record.push_field(itoa::Buffer::new().format(counter).as_bytes());
259258
counter += increment;
260259
},
261260
EnumOperation::Uuid4 => {

src/cmd/frequency.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
184184

185185
#[allow(unused_assignments)]
186186
let mut header_vec: Vec<u8> = Vec::with_capacity(tables.len());
187-
let mut buffer = itoa::Buffer::new();
187+
let mut itoa_buffer = itoa::Buffer::new();
188188
let mut pct_decimal: Decimal;
189189
let mut final_pct_decimal: Decimal;
190190
let mut pct_string: String;
@@ -259,7 +259,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
259259
row = vec![
260260
&*header_vec,
261261
&*value,
262-
buffer.format(count).as_bytes(),
262+
itoa_buffer.format(count).as_bytes(),
263263
pct_string.as_bytes(),
264264
];
265265
wtr.write_record(row)?;

src/cmd/luau.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,18 +1299,18 @@ fn map_computedvalue(
12991299
record.clear();
13001300
}
13011301
let mut columns_inserted = 0_u8;
1302-
let mut ibuffer = itoa::Buffer::new();
1303-
let mut nbuffer = ryu::Buffer::new();
13041302
table.for_each::<String, Value>(|_k, v| {
13051303
if new_column_count > 0 && columns_inserted >= new_column_count {
13061304
// we ignore table values more than the number of
13071305
// new columns defined, so we return early
13081306
return Ok(());
13091307
}
13101308
match v {
1311-
Value::Integer(intval) => record.push_field(ibuffer.format(intval)),
1309+
Value::Integer(intval) => record.push_field(itoa::Buffer::new().format(intval)),
13121310
Value::String(strval) => record.push_field(&strval.to_string_lossy()),
1313-
Value::Number(number) => record.push_field(nbuffer.format_finite(number)),
1311+
Value::Number(number) => {
1312+
record.push_field(ryu::Buffer::new().format_finite(number));
1313+
},
13141314
Value::Boolean(boolean) => {
13151315
record.push_field(if boolean { "true" } else { "false" });
13161316
},
@@ -1735,8 +1735,7 @@ fn setup_helpers(
17351735
record = result.unwrap_or_default();
17361736

17371737
let key = if key_idx == usize::MAX {
1738-
let mut buffer = itoa::Buffer::new();
1739-
buffer.format(row_idx).to_owned()
1738+
itoa::Buffer::new().format(row_idx).to_owned()
17401739
} else {
17411740
record.get(key_idx).unwrap_or_default().trim().to_string()
17421741
};

src/cmd/python.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,8 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
211211
if rconfig.no_headers {
212212
headers = csv::StringRecord::new();
213213

214-
let mut buffer = itoa::Buffer::new();
215214
for i in 0..headers_len {
216-
headers.push_field(buffer.format(i));
215+
headers.push_field(itoa::Buffer::new().format(i));
217216
}
218217
} else {
219218
if !args.cmd_filter {

src/cmd/search.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
197197
let mut match_ctr: u64 = 0;
198198
let mut row_ctr: u64 = 0;
199199
let mut m;
200-
let mut buffer = itoa::Buffer::new();
201200
let invert_match = args.flag_invert_match;
202201

203202
#[allow(unused_assignments)]
@@ -277,7 +276,9 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
277276
if flag_flag {
278277
flag_rowi += 1;
279278
match_row = if m {
280-
buffer.format(flag_rowi).clone_into(&mut matched_rows);
279+
itoa::Buffer::new()
280+
.format(flag_rowi)
281+
.clone_into(&mut matched_rows);
281282
matched_rows.as_bytes()
282283
} else {
283284
b"0"
@@ -360,7 +361,9 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
360361
if flag_flag {
361362
flag_rowi += 1;
362363
match_row = if m {
363-
buffer.format(flag_rowi).clone_into(&mut matched_rows);
364+
itoa::Buffer::new()
365+
.format(flag_rowi)
366+
.clone_into(&mut matched_rows);
364367
matched_rows.as_bytes()
365368
} else {
366369
b"0"

src/cmd/searchset.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
237237
let mut m;
238238
let mut matched = false;
239239
let mut matches: Vec<usize> = Vec::with_capacity(20);
240-
let mut buffer = itoa::Buffer::new();
241240

242241
while rdr.read_byte_record(&mut record)? {
243242
row_ctr += 1;
@@ -270,7 +269,9 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
270269
if do_match_list {
271270
flag_rowi += 1;
272271
flag_column = if m {
273-
buffer.format(flag_rowi).clone_into(&mut matched_rows);
272+
itoa::Buffer::new()
273+
.format(flag_rowi)
274+
.clone_into(&mut matched_rows);
274275
if args.flag_invert_match {
275276
matched_rows.as_bytes().to_vec()
276277
} else {

src/cmd/stats.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1358,10 +1358,10 @@ impl Stats {
13581358
v.add(&n);
13591359
}
13601360
if t == TFloat {
1361-
let mut buffer = ryu::Buffer::new();
1361+
let mut ryu_buffer = ryu::Buffer::new();
13621362
// safety: we know that n is a valid f64
13631363
// so there will always be a fraction part, even if it's 0
1364-
let fractpart = buffer.format_finite(n).split('.').next_back().unwrap();
1364+
let fractpart = ryu_buffer.format_finite(n).split('.').next_back().unwrap();
13651365
self.max_precision = std::cmp::max(
13661366
self.max_precision,
13671367
(if *fractpart == *"0" {
@@ -2008,13 +2008,12 @@ impl TypedSum {
20082008
)),
20092009
}
20102010
},
2011-
TFloat => {
2012-
let mut fbuffer = ryu::Buffer::new();
2013-
Some((
2014-
self.stotlen,
2015-
fbuffer.format(self.float.unwrap_or(0.0)).to_owned(),
2016-
))
2017-
},
2011+
TFloat => Some((
2012+
self.stotlen,
2013+
ryu::Buffer::new()
2014+
.format(self.float.unwrap_or(0.0))
2015+
.to_owned(),
2016+
)),
20182017
TString => Some((self.stotlen, String::new())),
20192018
}
20202019
}

src/cmd/validate.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -894,9 +894,6 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
894894
let mut validation_error_messages: Vec<String> = Vec::with_capacity(50);
895895
let flag_trim = args.flag_trim;
896896

897-
// amortize buffer allocation
898-
let mut buffer = itoa::Buffer::new();
899-
900897
// main loop to read CSV and construct batches for parallel processing.
901898
// each batch is processed via Rayon parallel iterator.
902899
// loop exits when batch is empty.
@@ -905,7 +902,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
905902
match rdr.read_byte_record(&mut record) {
906903
Ok(true) => {
907904
row_number += 1;
908-
record.push_field(buffer.format(row_number).as_bytes());
905+
record.push_field(itoa::Buffer::new().format(row_number).as_bytes());
909906
if flag_trim {
910907
record.trim();
911908
}

src/util.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1305,8 +1305,7 @@ pub fn round_num(dec_f64: f64, places: u32) -> String {
13051305

13061306
// if places is the sentinel value 9999, we don't round, just return the number as is
13071307
if places == 9999 {
1308-
let mut buffer = ryu::Buffer::new();
1309-
return buffer.format(dec_f64).to_owned();
1308+
return ryu::Buffer::new().format(dec_f64).to_owned();
13101309
}
13111310

13121311
// use from_f64_retain, so we have all the excess bits before rounding with

0 commit comments

Comments
 (0)