Skip to content

Commit 81a5c2e

Browse files
stepanchegfacebook-github-bot
authored andcommitted
Replace regex with manual string split
Summary: I was doing typechecker, and since some recent changes (likely after D48295588), typechecker became much slower. Instead of 7% overhead, it is 50% overhead. I started looking at profiler. ``` 25.45% buck2-rt [kernel.kallsyms] [k] queued_spin_lock_slowpath ``` Or the same from strobelight: {F1072524306} ([1](https://fburl.com/strobelight/fihizjz9) [2](https://fburl.com/strobelight/8a55kesp)) Obtaining types using documentation is suboptimal. But I need to make it fast now to continue working on typechecker. (Also reported to [regex issue tracker](rust-lang/regex#1066), but they are probably aware of it). (We need to figure out how to work with regular expressions). Reviewed By: iguridi Differential Revision: D48496022 fbshipit-source-id: 5d8113ca014208ab5d1db1750f83792d02462cc5
1 parent 5caa9f1 commit 81a5c2e

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

starlark/src/docs/mod.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,26 @@ impl DocString {
168168
None
169169
}
170170

171+
fn split_summary_details(s: &str) -> Option<(&str, &str)> {
172+
let mut summary_len = 0;
173+
for line in s.split_inclusive('\n') {
174+
if line.trim().is_empty() {
175+
let details_start = summary_len + line.len();
176+
return Some((s[..summary_len].trim(), &s[details_start..]));
177+
} else {
178+
summary_len += line.len();
179+
}
180+
}
181+
None
182+
}
183+
171184
/// Do common work to parse a docstring (dedenting, splitting summary and details, etc)
172185
pub fn from_docstring(kind: DocStringKind, user_docstring: &str) -> Option<DocString> {
173186
let trimmed_docs = user_docstring.trim();
174187
if trimmed_docs.is_empty() {
175188
None
176189
} else {
177-
static SPLIT_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"\n[ ]*\n").unwrap());
178-
let split: Option<(&str, &str)> = SPLIT_RE.splitn(trimmed_docs, 2).collect_tuple();
190+
let split: Option<(&str, &str)> = Self::split_summary_details(trimmed_docs);
179191
let (summary, details) = match split {
180192
Some((summary, details)) if !summary.is_empty() && !details.is_empty() => {
181193
// Dedent the details separately so that people can have the summary on the

0 commit comments

Comments
 (0)