Skip to content

Commit

Permalink
refactor: use reserve() instead of with_capacity();use simdjson::Bo…
Browse files Browse the repository at this point in the history
…rrowedValue instead of OwnedValue to minimize allocations
  • Loading branch information
jqnatividad committed Nov 8, 2024
1 parent 7c7b551 commit 05319d9
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/cmd/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ use rayon::{
prelude::IntoParallelRefIterator,
};
use serde::Deserialize;
use simd_json::BorrowedValue;

use crate::{
config::{Config, Delimiter, DEFAULT_WTR_BUFFER_CAPACITY},
Expand Down Expand Up @@ -267,7 +268,8 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
.map(|record| {
let curr_record = record;

let mut context = simd_json::owned::Object::with_capacity(headers_len);
let mut context = simd_json::borrowed::Object::default();
context.reserve(headers_len);
let mut row_number = 0_u64;

if no_headers {
Expand All @@ -279,22 +281,22 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
// set the last field to QSV_ROWNO
row_number = atoi_simd::parse::<u64>(field.as_bytes()).unwrap();
context.insert(
QSV_ROWNO.to_owned(),
simd_json::OwnedValue::String(field.to_owned()),
std::borrow::Cow::Borrowed(QSV_ROWNO),
BorrowedValue::String(std::borrow::Cow::Borrowed(field)),
);
} else {
context.insert(
format!("_c{}", i + 1),
simd_json::OwnedValue::String(field.to_owned()),
format!("_c{}", i + 1).into(),
BorrowedValue::String(std::borrow::Cow::Borrowed(field)),
);
}
}
} else {
// Use header names
for (header, field) in headers.iter().zip(curr_record.iter()) {
context.insert(
header.to_string(),
simd_json::OwnedValue::String(field.to_owned()),
std::borrow::Cow::Borrowed(header),
BorrowedValue::String(std::borrow::Cow::Borrowed(field)),
);
// when headers are defined, the last one is QSV_ROWNO
if header == QSV_ROWNO {
Expand Down

0 comments on commit 05319d9

Please sign in to comment.