From e30237a714c0fff0deab219e2db7b7351a3f2df4 Mon Sep 17 00:00:00 2001 From: Simon Vandel Sillesen Date: Sat, 21 Jul 2018 15:26:38 +0200 Subject: [PATCH] uniq: avoid allocations for the iterator --- src/uniq/uniq.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/uniq/uniq.rs b/src/uniq/uniq.rs index 2bda28f4a..fb6918449 100644 --- a/src/uniq/uniq.rs +++ b/src/uniq/uniq.rs @@ -44,10 +44,6 @@ struct Uniq { zero_terminated: bool, } -fn iter_ne<'a>(lhs: Box + 'a>, rhs: Box + 'a>) -> bool { - lhs.ne(rhs) -} - impl Uniq { pub fn print_uniq( &self, @@ -107,7 +103,16 @@ impl Uniq { } } - fn cmp_key<'a>(&'a self, line: &'a str) -> Box + 'a> { + fn cmp_keys(&self, first: &str, second: &str) -> bool { + self.cmp_key(first, |first_iter| { + self.cmp_key(second, |second_iter| first_iter.ne(second_iter)) + }) + } + + fn cmp_key(&self, line: &str, mut closure: F) -> bool + where + F: FnMut(&mut Iterator) -> bool, + { let fields_to_check = self.skip_fields(line); let len = fields_to_check.len(); let slice_start = self.slice_start.unwrap_or(0); @@ -115,12 +120,12 @@ impl Uniq { 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()); + return closure(&mut 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 { + return closure(&mut fields_to_check.chars().map(|c| match c { 'a'...'z' => ((c as u8) - 32) as char, _ => c, })); @@ -128,11 +133,11 @@ impl Uniq { // 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)); + return closure(&mut fields_to_check.chars().skip(slice_start).take(slice_stop)); } - Box::new( - fields_to_check + closure( + &mut fields_to_check .chars() .skip(slice_start) .take(slice_stop) @@ -142,7 +147,7 @@ impl Uniq { }), ) } else { - Box::new(fields_to_check.chars()) + closure(&mut fields_to_check.chars()) } }