Skip to content

Commit

Permalink
fix: fix performance regression on resolver::parse_doc_string. Use pc…
Browse files Browse the repository at this point in the history
…re2 to replace std::Regex
  • Loading branch information
He1pa committed Jul 24, 2023
1 parent 2b999ca commit 5da4d2b
Show file tree
Hide file tree
Showing 3 changed files with 65 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
27 changes: 17 additions & 10 deletions kclvm/sema/src/resolver/doc.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
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 {
s.replace("\t", &" ".repeat(spaces_per_tab))
}
Expand Down

0 comments on commit 5da4d2b

Please sign in to comment.