From 781c9236e1f5e7624510445782c172147f3deba4 Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Thu, 12 Jul 2018 17:00:06 +0200 Subject: [PATCH] uniq: add more fast-paths to avoid work --- src/uniq/uniq.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/uniq/uniq.rs b/src/uniq/uniq.rs index 580d27e96..2bda28f4a 100644 --- a/src/uniq/uniq.rs +++ b/src/uniq/uniq.rs @@ -113,6 +113,19 @@ impl Uniq { let slice_start = self.slice_start.unwrap_or(0); let slice_stop = self.slice_stop.unwrap_or(len); if len > 0 { + // fast path: avoid doing any work if there is no need to skip or map to lower-case + if !self.ignore_case && slice_start == 0 && slice_stop == len { + return Box::new(fields_to_check.chars()); + } + + // fast path: avoid skipping + if self.ignore_case && slice_start == 0 && slice_stop == len { + return Box::new(fields_to_check.chars().map(|c| match c { + 'a'...'z' => ((c as u8) - 32) as char, + _ => c, + })); + } + // fast path: we can avoid mapping chars to upper-case, if we don't want to ignore the case if !self.ignore_case { return Box::new(fields_to_check.chars().skip(slice_start).take(slice_stop)); @@ -123,8 +136,8 @@ impl Uniq { .chars() .skip(slice_start) .take(slice_stop) - .map(move |c| match c { - 'a'...'z' if self.ignore_case => ((c as u8) - 32) as char, + .map(|c| match c { + 'a'...'z' => ((c as u8) - 32) as char, _ => c, }), )