From 3bbbd2556d0f1fd0190db868f3d0bbec30d660eb Mon Sep 17 00:00:00 2001 From: ljedrz Date: Fri, 31 May 2024 17:36:08 +0200 Subject: [PATCH] perf: allow Sanitizer::str_till_eol to omit allocations Signed-off-by: ljedrz --- .../environment/src/helpers/sanitizer.rs | 33 +++++++++++++++---- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/console/network/environment/src/helpers/sanitizer.rs b/console/network/environment/src/helpers/sanitizer.rs index bceed0e0b1..c34508bf24 100644 --- a/console/network/environment/src/helpers/sanitizer.rs +++ b/console/network/environment/src/helpers/sanitizer.rs @@ -116,12 +116,33 @@ impl Sanitizer { /// /// Discard any leading newline. fn str_till_eol(string: &str) -> ParserResult<&str> { - map( - recognize(Self::till(alt((value((), tag("\\\n")), value((), Sanitizer::parse_safe_char))), Self::eol)), - |i| { - if i.as_bytes().last() == Some(&b'\n') { &i[0..i.len() - 1] } else { i } - }, - )(string) + // A heuristic approach is applied here in order to avoid + // costly parsing operations in the most common scenarios. + if let Some((before, after)) = string.split_once('\n') { + let is_multiline = before.ends_with('\\'); + + if !is_multiline { + let contains_unsafe_chars = !before.chars().all(is_char_supported); + + if !contains_unsafe_chars { + Ok((after, before)) + } else { + recognize(Self::till(value((), Sanitizer::parse_safe_char), Self::eol))(before) + } + } else { + map( + recognize(Self::till( + alt((value((), tag("\\\n")), value((), Sanitizer::parse_safe_char))), + Self::eol, + )), + |i| { + if i.as_bytes().last() == Some(&b'\n') { &i[0..i.len() - 1] } else { i } + }, + )(string) + } + } else { + Ok((string, "")) + } } /// Parse a string until `*/` is encountered.