From 3e8a41fbc9f0875517f94d9f39db726894daef28 Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Tue, 22 Feb 2022 04:50:49 -0500 Subject: [PATCH] Speedup unit parse (#4598) * Compact nuon tables * Speed up unit parsing a bit --- crates/nu-parser/src/parser.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index 7948cf3f29..ade875518a 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -1710,6 +1710,8 @@ pub fn parse_duration( let bytes = working_set.get_span_contents(span); let token = String::from_utf8_lossy(bytes).to_string(); + let upper = token.to_uppercase(); + let unit_groups = [ (Unit::Nanosecond, "NS", None), (Unit::Microsecond, "US", Some((Unit::Nanosecond, 1000))), @@ -1720,11 +1722,8 @@ pub fn parse_duration( (Unit::Day, "DAY", Some((Unit::Minute, 1440))), (Unit::Week, "WK", Some((Unit::Day, 7))), ]; - if let Some(unit) = unit_groups - .iter() - .find(|&x| token.to_uppercase().ends_with(x.1)) - { - let mut lhs = token.clone(); + if let Some(unit) = unit_groups.iter().find(|&x| upper.ends_with(x.1)) { + let mut lhs = token; for _ in 0..unit.1.len() { lhs.pop(); } @@ -1806,6 +1805,8 @@ pub fn parse_filesize( let bytes = working_set.get_span_contents(span); let token = String::from_utf8_lossy(bytes).to_string(); + let upper = token.to_uppercase(); + let unit_groups = [ (Unit::Kilobyte, "KB", Some((Unit::Byte, 1000))), (Unit::Megabyte, "MB", Some((Unit::Kilobyte, 1000))), @@ -1819,11 +1820,8 @@ pub fn parse_filesize( (Unit::Pebibyte, "PIB", Some((Unit::Tebibyte, 1024))), (Unit::Byte, "B", None), ]; - if let Some(unit) = unit_groups - .iter() - .find(|&x| token.to_uppercase().ends_with(x.1)) - { - let mut lhs = token.clone(); + if let Some(unit) = unit_groups.iter().find(|&x| upper.ends_with(x.1)) { + let mut lhs = token; for _ in 0..unit.1.len() { lhs.pop(); }