Skip to content

Commit 17780b5

Browse files
committed
perf: excel microoptimize trim path; remove need for temporary float work var
1 parent 876d2c2 commit 17780b5

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

src/cmd/excel.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -942,8 +942,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
942942
};
943943

944944
// amortize allocations
945-
let mut record = csv::StringRecord::with_capacity(500, col_count);
946-
let mut trimmed_record = csv::StringRecord::with_capacity(500, col_count);
945+
let mut record = csv::StringRecord::with_capacity(512, col_count);
947946

948947
// get headers
949948
info!("exporting sheet ({sheet})...");
@@ -961,6 +960,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
961960

962961
if trim {
963962
record.trim();
963+
let mut trimmed_record = csv::StringRecord::with_capacity(512, col_count);
964964
record.iter().for_each(|field| {
965965
if field.contains('\n') {
966966
trimmed_record.push_field(&field.replace('\n', " "));
@@ -1003,13 +1003,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
10031003
rows.par_chunks(chunk_size)
10041004
.map(|chunk| {
10051005
// amortize allocations
1006-
let mut record = csv::StringRecord::with_capacity(500, col_count);
1007-
let mut trimmed_record = if trim {
1008-
csv::StringRecord::with_capacity(500, col_count)
1009-
} else {
1010-
csv::StringRecord::new()
1011-
};
1012-
let mut float_val;
1006+
let mut record = csv::StringRecord::with_capacity(512, col_count);
10131007
let mut work_date;
10141008
let mut error_buffer = String::new();
10151009
let mut formatted_date = String::new();
@@ -1028,21 +1022,20 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
10281022
Data::Empty => record.push_field(""),
10291023
Data::String(ref s) => record.push_field(s),
10301024
Data::Int(ref i) => record.push_field(itoa_buf.format(*i)),
1031-
Data::Float(ref f) => {
1032-
float_val = *f;
1025+
Data::Float(ref float_val) => {
10331026
// push the ryu-formatted float value if its
10341027
// not an integer or the candidate
10351028
// integer is too big or too small to be an i64
10361029
#[allow(clippy::cast_precision_loss)]
10371030
if float_val.fract().abs() > f64::EPSILON
1038-
|| float_val > i64::MAX as f64
1039-
|| float_val < i64::MIN as f64
1031+
|| *float_val > i64::MAX as f64
1032+
|| *float_val < i64::MIN as f64
10401033
{
1041-
record.push_field(ryu_buf.format_finite(float_val));
1034+
record.push_field(ryu_buf.format_finite(*float_val));
10421035
} else {
10431036
// its an i64 integer. We can't use ryu to format it, because it
10441037
// will be formatted as a float (have a ".0"). So we use itoa.
1045-
record.push_field(itoa_buf.format(float_val as i64));
1038+
record.push_field(itoa_buf.format(*float_val as i64));
10461039
}
10471040
},
10481041
Data::DateTime(ref edt) => {
@@ -1118,6 +1111,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
11181111
}
11191112

11201113
if trim {
1114+
let mut trimmed_record = csv::StringRecord::with_capacity(512, col_count);
11211115
// record.trim() is faster than trimming each field piecemeal
11221116
record.trim();
11231117
record.iter().for_each(|field| {

0 commit comments

Comments
 (0)