Skip to content

Commit

Permalink
Optimize a bit
Browse files Browse the repository at this point in the history
It's better to allocate for the smaller prefix that we're extracting
than to allocate repeatedly for the tail items, especially as
`split_off` doesn't shrink the capacity of what we go on to insert.
  • Loading branch information
traviscross committed Aug 20, 2024
1 parent 4711485 commit b95d379
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions mdbook-spec/src/std_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub fn std_links(book: &mut Book) {
.captures_iter(&generated)
.map(|cap| cap.get(1).unwrap().as_str())
.collect();
let mut urls = &mut urls[..];
let expected_len: usize = chapter_links.values().map(|l| l.len()).sum();
if urls.len() != expected_len {
eprintln!(
Expand All @@ -67,9 +68,9 @@ pub fn std_links(book: &mut Book) {
// Unflatten the urls list so that it is split back by chapter.
let mut ch_urls: HashMap<&PathBuf, Vec<_>> = HashMap::new();
for (ch_path, links) in &chapter_links {
let rest = urls.split_off(links.len());
ch_urls.insert(ch_path, urls);
urls = rest;
let xs;
(xs, urls) = urls.split_at_mut(links.len());
ch_urls.insert(ch_path, xs.into());
}

// Do this in two passes to deal with lifetimes.
Expand Down

0 comments on commit b95d379

Please sign in to comment.