Skip to content

Commit c8113a7

Browse files
authored
Rollup merge of #147237 - yotamofek:pr/rustdoc/highlight/optimize-end_expansion, r=GuillaumeGomez
[rustdoc] Cleanup "highlight::end_expansion" ~Looks like a ~5% improvement on the highlight benchmark. Obviously, highlighting is only a small part of rustdoc's runtime, so improvement won't be as large on rustc-perf (if there's even an improvement), but holding fingers for a nice gain.~ Perf seems neutral, but IMHO this is a nice small cleanup regardless. r? `@GuillaumeGomez` (& perf run please!)
2 parents c3b51b3 + 78c2e58 commit c8113a7

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

src/librustdoc/html/highlight.rs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use std::borrow::Cow;
99
use std::collections::VecDeque;
1010
use std::fmt::{self, Display, Write};
11-
use std::iter;
11+
use std::{cmp, iter};
1212

1313
use rustc_data_structures::fx::FxIndexMap;
1414
use rustc_lexer::{Cursor, FrontmatterAllowed, LiteralKind, TokenKind};
@@ -345,33 +345,26 @@ fn end_expansion<'a, W: Write>(
345345
token_handler.pending_elems.push((Cow::Borrowed("</span>"), Some(Class::Expansion)));
346346
return Some(expanded_code);
347347
}
348-
if expansion_start_tags.is_empty() && token_handler.closing_tags.is_empty() {
349-
// No need tag opened so we can just close expansion.
350-
token_handler.pending_elems.push((Cow::Borrowed("</span></span>"), Some(Class::Expansion)));
351-
return None;
352-
}
353348

354-
// If tags were opened inside the expansion, we need to close them and re-open them outside
355-
// of the expansion span.
356-
let mut out = String::new();
357-
let mut end = String::new();
349+
let skip = iter::zip(token_handler.closing_tags.as_slice(), expansion_start_tags)
350+
.position(|(tag, start_tag)| tag != start_tag)
351+
.unwrap_or_else(|| cmp::min(token_handler.closing_tags.len(), expansion_start_tags.len()));
358352

359-
let mut closing_tags = token_handler.closing_tags.iter().peekable();
360-
let mut start_closing_tags = expansion_start_tags.iter().peekable();
353+
let tags = iter::chain(
354+
expansion_start_tags.iter().skip(skip),
355+
token_handler.closing_tags.iter().skip(skip),
356+
);
361357

362-
while let (Some(tag), Some(start_tag)) = (closing_tags.peek(), start_closing_tags.peek())
363-
&& tag == start_tag
364-
{
365-
closing_tags.next();
366-
start_closing_tags.next();
358+
let mut elem = Cow::Borrowed("</span></span>");
359+
360+
for (tag, _) in tags.clone() {
361+
elem.to_mut().push_str(tag);
367362
}
368-
for (tag, class) in start_closing_tags.chain(closing_tags) {
369-
out.push_str(tag);
370-
end.push_str(&format!("<span class=\"{}\">", class.as_html()));
363+
for (_, class) in tags {
364+
write!(elem.to_mut(), "<span class=\"{}\">", class.as_html()).unwrap();
371365
}
372-
token_handler
373-
.pending_elems
374-
.push((Cow::Owned(format!("</span></span>{out}{end}")), Some(Class::Expansion)));
366+
367+
token_handler.pending_elems.push((elem, Some(Class::Expansion)));
375368
None
376369
}
377370

0 commit comments

Comments
 (0)