From c4ee937635ea61798b7674bf253078e9cf93f07c Mon Sep 17 00:00:00 2001 From: marin postma Date: Tue, 22 Jun 2021 10:17:39 +0200 Subject: [PATCH] optimize fromat string --- meilisearch-http/src/index/search.rs | 33 +++++++++++++--------------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/meilisearch-http/src/index/search.rs b/meilisearch-http/src/index/search.rs index b42b78d33..58132b633 100644 --- a/meilisearch-http/src/index/search.rs +++ b/meilisearch-http/src/index/search.rs @@ -572,26 +572,23 @@ impl<'a, A: AsRef<[u8]>> Formatter<'a, A> { None => Box::new(analyzed.reconstruct()), }; - tokens - .map(|(word, token)| { - // Check if we need to do highlighting or computed matches before calling - // Matcher::match since the call is expensive. - if format_options.highlight && token.is_word() { - if let Some(length) = matcher.matches(token.text()) { - if format_options.highlight { - let mut new_word = String::new(); - new_word.push_str(&self.marks.0); - new_word.push_str(&word[..length]); - new_word.push_str(&self.marks.1); - new_word.push_str(&word[length..]); - - return Cow::Owned(new_word); - } + tokens.fold(String::new(), |mut out, (word, token)| { + // Check if we need to do highlighting or computed matches before calling + // Matcher::match since the call is expensive. + if format_options.highlight && token.is_word() { + if let Some(length) = matcher.matches(token.text()) { + if format_options.highlight { + out.push_str(&self.marks.0); + out.push_str(&word[..length]); + out.push_str(&self.marks.1); + out.push_str(&word[length..]); + return out; } } - Cow::Borrowed(word) - }) - .collect::() + } + out.push_str(word); + out + }) } }