From 29a9a7b8e851d563c565170a86307514c8f8381b Mon Sep 17 00:00:00 2001 From: Joel Natividad <1980690+jqnatividad@users.noreply.github.com> Date: Sun, 3 Dec 2023 11:26:23 -0500 Subject: [PATCH] `excel`: minor performance tweaks - remove unnecessary allocations in hot loop - compare to f64::EPSILON instead of 0.0 - also added test for data types --- resources/test/excel-xlsx.xlsx | Bin 13325 -> 14951 bytes src/cmd/excel.rs | 12 +++++++----- tests/test_excel.rs | 21 +++++++++++++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/resources/test/excel-xlsx.xlsx b/resources/test/excel-xlsx.xlsx index b95ae627b4555e1e9e0350076151ab9cf36a6ad2..717afa3b21a1f2aed0fbeef215ffbb9d1ec8b9aa 100644 GIT binary patch delta 6629 zcmZ8mbyQT{*Pa0ehVD+0&LIQ`kVX-yK^kc#q`SF<#LzQChm-qGq`zWB8iBRh#$63=C!V^C@r-Jm)OI`eIig^eyf*UBQB1M0I9La+~1k zq1xbpA*!wX%d~EW_SZBngI>j-i(7IW#F8voOm)My)Jnx0*gWwNklu@}aLYazh+QI5 zad(wS`-lA-2LoP%hn9wPi9U`&q)HEJm#15HF^zZWh##9n&b6rn(qeUjTVc#_NezHB zJbONKwxCX@&)JjpTVgDtFW<}ZPJJgdupr?$WuvK+f|>N@P59|&52DAYSJ`Fcv}`roZ3BHUNDf{^|A*> z6uB4P9zGf5hW1{f^YZ5nH*4Gls~7a^uxUH|XrU1qS)zDwI3rGuk2)5^-9$ubnR<PsjnUn&*d+OCS) z5OG5$`%FG@%G8;@2Q-LN8pz4@j1Y}qi9Z7Fu`$}mDCK`(jiHXVUWTmS)SDW8f$y3I zJ5L@E6Q2FNL=v&%A$ItmL(-@Q^J`+TLng-XO}<>l&DN@6+Q%k9iMYHD51QMR+zw&`?+=@sEoVWOc)OAQePirv&Ns8tBsb zn_z1Sbe&O-`;rop_;{pWQY<%WSU9UxTwhnU@7F{xZG-)U)KB`dT&2T{K%01VB|aV7 zH>>xEM-B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C(EZ$F7^G?l)PYQegXI&7!W&#u;liUZdArS@5IJ@-q{p?@=nWuVEaqa^9?kE8v|* z<34Y&_*jEwVOC7_APjQBXUaYg=;u<=XJh7a;PE+sKGxShI<ilzmw@cRjgU_5QxvbtfcD|#?ysT>2z+@z7KhWjo1?j_y>=@i zOd`VL21#if*FbiZD*s4s4XD7zk%VliDkv$8!s|)|jgkWLw z0RA6_3LgNVx*Hn)xl9z@-B58N;u3Xt<`!OdF5Y~+e?6bT|LNtQq{@H#m_yx&@X-HF zD*ymw|EUJtL#c?epsGY6*guF-L!xZZ|DIz0-t7cN5s5KE|88&q0J^`mcTxZyikXD^ zuI0f)S&DIDZ_%TInb^4h`&<96l6Sn^KkOM8P|IR+*t$&raw9Cxhy4roFELP@8v5@9 za)$)@Kg@s}s0wi@>~$^_u^`9)Iqu&-Gd2J~@ejKm9+Zp(Jt--G6z~_k%KU$6{|7fC BROkQz diff --git a/src/cmd/excel.rs b/src/cmd/excel.rs index d1dcb4380..c7305fa84 100644 --- a/src/cmd/excel.rs +++ b/src/cmd/excel.rs @@ -624,9 +624,11 @@ 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), + DataType::DurationIso(ref d) => record.push_field(&d), DataType::Duration(ref d) => record.push_field(ryu_buffer.format(*d)), }; @@ -634,7 +636,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> { 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() { @@ -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 { diff --git a/tests/test_excel.rs b/tests/test_excel.rs index c6bb90b15..8d6805586 100644 --- a/tests/test_excel.rs +++ b/tests/test_excel.rs @@ -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> = 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");