Skip to content

Commit

Permalink
fix: fix performance regression on resolver::parse_doc_string (#619)
Browse files Browse the repository at this point in the history
fix: fix performance regression on resolver::parse_doc_string. Use pcre2 to replace std::Regex
  • Loading branch information
He1pa committed Jul 24, 2023
1 parent 2b999ca commit 78e7b20
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
48 changes: 46 additions & 2 deletions kclvm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions kclvm/sema/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ compiler_base_span = {path = "../../compiler_base/span", version = "0.0.2"}
compiler_base_session = {path = "../../compiler_base/session"}
compiler_base_macros = "0.0.1"
compiler_base_error = "0.0.8"
lazy_static = "1.4.0"
pcre2 = "*"

[dev-dependencies]
kclvm-parser = {path = "../parser"}
Expand Down
25 changes: 15 additions & 10 deletions kclvm/sema/src/resolver/doc.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
use regex::Regex;
use pcre2::bytes::Regex;
use std::collections::HashSet;
use std::iter::Iterator;
use std::str;

lazy_static::lazy_static! {
static ref RE: Regex = Regex::new(r#"(?s)^(['\"]{3})(.*?)(['\"]{3})$"#).unwrap();
}

/// strip leading and trailing triple quotes from the original docstring content
fn strip_quotes(original: &mut String) {
let quote = original.chars().next().unwrap();
let pattern = format!("(?s)^{char}{{3}}(.*?){char}{{3}}$", char = quote);
let re = Regex::new(&pattern).unwrap();
let caps = re.captures(&original);
let result = match caps {
Some(caps) => caps,
None => return,
};
let content = result[1].to_owned();
*original = content;
if quote != '"' && quote != '\'' {
return;
}
if let Ok(Some(mat)) = RE.find(original.as_bytes()) {
let content = str::from_utf8(&original.as_bytes()[mat.start() + 3..mat.end() - 3])
.unwrap()
.to_owned();
*original = content;
}
}

fn expand_tabs(s: &str, spaces_per_tab: usize) -> String {
Expand Down

0 comments on commit 78e7b20

Please sign in to comment.