Skip to content

Commit

Permalink
validate: micro-optimize
Browse files Browse the repository at this point in the history
- amortize itoa buffer allocation outside of hot loop
- add comment regarding mem::take
  • Loading branch information
jqnatividad committed Sep 27, 2024
1 parent 39989de commit 3ccc1ef
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/cmd/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,11 +662,13 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
// set RAYON_NUM_THREADS
util::njobs(args.flag_jobs);

// amortize buffer allocation
let mut buffer = itoa::Buffer::new();

// main loop to read CSV and construct batches for parallel processing.
// each batch is processed via Rayon parallel iterator.
// loop exits when batch is empty.
'batch_loop: loop {
let mut buffer = itoa::Buffer::new();
for _ in 0..batch_size {
match rdr.read_byte_record(&mut record) {
Ok(true) => {
Expand All @@ -675,6 +677,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
if flag_trim {
record.trim();
}
// we use mem::take() to avoid cloning & clearing the record
batch.push(std::mem::take(&mut record));
},
Ok(false) => break, // nothing else to add to batch
Expand Down

0 comments on commit 3ccc1ef

Please sign in to comment.