Skip to content

Commit

Permalink
excel: minor performance tweaks
Browse files Browse the repository at this point in the history
- remove unnecessary allocations in hot loop
- compare to f64::EPSILON instead of 0.0
- also added test for data types
  • Loading branch information
jqnatividad committed Dec 3, 2023
1 parent c531578 commit 29a9a7b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
Binary file modified resources/test/excel-xlsx.xlsx
Binary file not shown.
12 changes: 7 additions & 5 deletions src/cmd/excel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,17 +624,19 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
cell_date_flag = true;
},
DataType::Error(ref e) => record.push_field(&format!("{e:?}")),
DataType::Bool(ref b) => record.push_field(&b.to_string()),
DataType::DateTimeIso(ref dt) => record.push_field(&dt.to_string()),
DataType::DurationIso(ref d) => record.push_field(&d.to_string()),
DataType::Bool(ref b) => {
record.push_field(if *b { "true" } else { "false" })
},
DataType::DateTimeIso(ref dt) => record.push_field(&dt),

Check warning

Code scanning / clippy

this expression creates a reference which is immediately dereferenced by the compiler Warning

this expression creates a reference which is immediately dereferenced by the compiler
DataType::DurationIso(ref d) => record.push_field(&d),

Check warning

Code scanning / clippy

this expression creates a reference which is immediately dereferenced by the compiler Warning

this expression creates a reference which is immediately dereferenced by the compiler
DataType::Duration(ref d) => record.push_field(ryu_buffer.format(*d)),
};

#[allow(clippy::cast_precision_loss)]
if float_flag {
if cell_date_flag {
// its a date, so convert it
work_date = if float_val.fract() > 0.0 {
work_date = if float_val.fract() > f64::EPSILON {
// if it has a fractional part, then its a datetime
if let Some(dt) = cell.as_datetime() {
if date_format.is_empty() {
Expand Down Expand Up @@ -680,7 +682,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
// its not a date, so just push the ryu-formatted float value if its
// not an integer or the candidate
// integer is too big or too small to be an i64
} else if float_val.fract().abs() > 0.0
} else if float_val.fract().abs() > f64::EPSILON
|| float_val > i64::MAX as f64
|| float_val < i64::MIN as f64
{
Expand Down
21 changes: 21 additions & 0 deletions tests/test_excel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,27 @@ fn excel_date_xlsx_date_format() {
assert_eq!(got, expected);
}

#[test]
fn excel_xlsx_data_types() {
let wrk = Workdir::new("excel_xlsx_data_types");

let xlsx_file = wrk.load_test_file("excel-xlsx.xlsx");

let mut cmd = wrk.command("excel");
cmd.arg("--sheet").arg("data types").arg(xlsx_file);

let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["int", "float", "bool", "date", "duration", "string", "emojis", "foreign"],
svec!["1", "1.1", "true", "2001-09-11", "0.4305555555555556", "The", "The", "敏捷的棕色狐狸在森林里奔跑"],
svec!["2", "1.32434354545454", "false", "2023-10-07", "0.989849537037037", "quick", "🍔", "Franz jagt im komplett verwahrlosten Taxi quer durch Bayern"],
svec!["3", "0.423546456564534", "1", "1941-12-07", "1.2815162037037038", "brown", "is", "Le rusé goupil franchit d'un bond le chien somnolent."],
svec!["4", "-54545.6565756785", "0", "2001-09-11 08:30:00", "0.9791666666666666", "fox", "💩", "El rápido zorro marrón"],
svec!["5", "-5446563454.43546", "true", "1945-08-06 08:15:00", "0.0004629629629629629", "jumped", "🙀", "いろはにほへとちりぬるをわかよたれそつねならむうゐのおくやまけふこえてあさきゆめみしゑひもせす"]
];
assert_eq!(got, expected);
}

#[test]
fn excel_date_xlsx() {
let wrk = Workdir::new("excel_date_xls");
Expand Down

0 comments on commit 29a9a7b

Please sign in to comment.