-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Improve source code for highlight.rs
#146992
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Also need to check the impact on performance (likely slower). @bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
Improve source code for `highlight.rs`
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
let mut closing_tag = None; | ||
for part in &self.content { | ||
let text: &dyn Display = | ||
if part.needs_escape { &EscapeBodyText(&part.text) } else { &part.text }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, Either
impls Display
, which can be nicer than a dyn
ref (and maybe slightly more performant)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, thanks!
for part in elem.content.drain(..) { | ||
last.content.push(part); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for part in elem.content.drain(..) { | |
last.content.push(part); | |
} | |
last.content.append(&mut elem.content); |
Both shorter and might also be slightly more performant (can probably pre-reserve just enough capacity in target vector)
for elem in elements { | ||
self.elements.push(elem); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for elem in elements { | |
self.elements.push(elem); | |
} | |
self.elements.extend(elements); |
Same deal as https://github.com/rust-lang/rust/pull/146992/files#r2376863766
Code reads much better IMHO! |
Finished benchmarking commit (6020c97): comparison URL. Overall result: ❌ regressions - please read the text belowBenchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf. Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @bors rollup=never Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (primary 0.4%, secondary 12.0%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary 2.8%, secondary 5.9%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 471.213s -> 470.794s (-0.09%) |
Agreed (unsurprisingly 😆), but sadly I think this solution is unlikely to get much better performance-wise so unlikely it'll be merged. However I now have a much cleaner code, so I think I'll go back to the original "streaming content" but with a much cleaner approach. |
If I had to guess the reason for the perf regression, I would say it probably has to do with all the extra intermediate string allocations. I feel like if we had a way to delay formatting (maybe using an enum, or with |
Possibly. Want to give a try pushing it even further before I try to turn this back into a streaming algorithm? Same question for you @yotamofek. 😉 Start from my branch and open PRs with your commits so we can run perf check on them. |
I'll give it a go, but my gut says the extra string allocations aren't causing the lion's share of the regressions. Worth a shot though |
☔ The latest upstream changes (presumably #147037) made this pull request unmergeable. Please resolve the merge conflicts. |
…re HTML tags than necessary
dfcb725
to
957ca47
Compare
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
☔ The latest upstream changes (presumably #147397) made this pull request unmergeable. Please resolve the merge conflicts. |
I was very bothered by the complexity of this file, in particular the handling of
pending_elems
which was very tricky to follow.So instead, here comes a more sane approach: the content is store in a stack-like type which handles "levels" of HTML (ie, a macro expansion can contain other HTML tags which can themselves contain other, etc). Making it much simpler to keep track of what's going on.
r? @lolbinarycat