Skip to content

Commit

Permalink
Merge pull request #2563 from ProvableHQ/perf/parsing_sanitizer2
Browse files Browse the repository at this point in the history
[Perf] Use heuristics to avoid allocations in Sanitizer::str_till_eol
  • Loading branch information
zosorock authored Nov 12, 2024
2 parents 60e4602 + c9c83f9 commit 60a0aa5
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions console/network/environment/src/helpers/sanitizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,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.
Expand Down

0 comments on commit 60a0aa5

Please sign in to comment.