Skip to content

Commit c538f99

Browse files
authored
Unrolled build for #151368
Rollup merge of #151368 - GuillaumeGomez:librustdoc-perf, r=yotamofek Rustdoc performance improvements A few things I had sitting around for a while. Let's check what perf says about them r? @yotamofek
2 parents 4742769 + bd45311 commit c538f99

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

src/librustdoc/html/render/write_shared.rs

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use std::str::FromStr;
2626
use std::{fmt, fs};
2727

2828
use indexmap::IndexMap;
29-
use regex::Regex;
3029
use rustc_ast::join_path_syms;
3130
use rustc_data_structures::flock;
3231
use rustc_data_structures::fx::{FxHashSet, FxIndexMap, FxIndexSet};
@@ -376,12 +375,15 @@ fn hack_get_external_crate_names(
376375
};
377376
// this is only run once so it's fine not to cache it
378377
// !dot_matches_new_line: all crates on same line. greedy: match last bracket
379-
let regex = Regex::new(r"\[.*\]").unwrap();
380-
let Some(content) = regex.find(&content) else {
381-
return Err(Error::new("could not find crates list in crates.js", path));
382-
};
383-
let content: Vec<String> = try_err!(serde_json::from_str(content.as_str()), &path);
384-
Ok(content)
378+
if let Some(start) = content.find('[')
379+
&& let Some(end) = content[start..].find(']')
380+
{
381+
let content: Vec<String> =
382+
try_err!(serde_json::from_str(&content[start..=start + end]), &path);
383+
Ok(content)
384+
} else {
385+
Err(Error::new("could not find crates list in crates.js", path))
386+
}
385387
}
386388

387389
#[derive(Serialize, Deserialize, Clone, Default, Debug)]
@@ -504,33 +506,35 @@ impl Hierarchy {
504506

505507
fn add_path(self: &Rc<Self>, path: &Path) {
506508
let mut h = Rc::clone(self);
507-
let mut elems = path
509+
let mut components = path
508510
.components()
509-
.filter_map(|s| match s {
510-
Component::Normal(s) => Some(s.to_owned()),
511-
Component::ParentDir => Some(OsString::from("..")),
512-
_ => None,
513-
})
511+
.filter(|component| matches!(component, Component::Normal(_) | Component::ParentDir))
514512
.peekable();
515-
loop {
516-
let cur_elem = elems.next().expect("empty file path");
517-
if cur_elem == ".." {
518-
if let Some(parent) = h.parent.upgrade() {
513+
514+
assert!(components.peek().is_some(), "empty file path");
515+
while let Some(component) = components.next() {
516+
match component {
517+
Component::Normal(s) => {
518+
if components.peek().is_none() {
519+
h.elems.borrow_mut().insert(s.to_owned());
520+
break;
521+
}
522+
h = {
523+
let mut children = h.children.borrow_mut();
524+
525+
if let Some(existing) = children.get(s) {
526+
Rc::clone(existing)
527+
} else {
528+
let new_node = Rc::new(Self::with_parent(s.to_owned(), &h));
529+
children.insert(s.to_owned(), Rc::clone(&new_node));
530+
new_node
531+
}
532+
};
533+
}
534+
Component::ParentDir if let Some(parent) = h.parent.upgrade() => {
519535
h = parent;
520536
}
521-
continue;
522-
}
523-
if elems.peek().is_none() {
524-
h.elems.borrow_mut().insert(cur_elem);
525-
break;
526-
} else {
527-
let entry = Rc::clone(
528-
h.children
529-
.borrow_mut()
530-
.entry(cur_elem.clone())
531-
.or_insert_with(|| Rc::new(Self::with_parent(cur_elem, &h))),
532-
);
533-
h = entry;
537+
_ => {}
534538
}
535539
}
536540
}

0 commit comments

Comments
 (0)