-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Rustdoc: formatting to buffers #64044
Conversation
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
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.
Left a bunch of comments. Super +1 on removal of ? everywhere!
src/librustdoc/html/format.rs
Outdated
self.buffer.push_str(s); | ||
} | ||
|
||
crate fn write_fmt(&mut self, v: fmt::Arguments) { |
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.
This probably wants a comment explaining that this works with write! macro, but doesn’t require ?
src/librustdoc/html/format.rs
Outdated
#[derive(Debug, Clone)] | ||
pub struct Buffer { | ||
crate for_html: bool, | ||
crate buffer: String, |
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.
Make private?
src/librustdoc/html/layout.rs
Outdated
@@ -26,15 +26,15 @@ pub struct Page<'a> { | |||
} | |||
|
|||
pub fn render<T: fmt::Display, S: fmt::Display>( | |||
dst: &mut dyn io::Write, | |||
dst: &mut Buffer, |
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.
Would be cool to rename all fmt, v, dst, buffer etc just to buf.
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.
I'm trying to keep the diff as minimal as possible which means avoiding renaming for the sake of renaming.
@@ -11,6 +11,12 @@ pub struct Layout { | |||
pub favicon: String, | |||
pub external_html: ExternalHtml, | |||
pub krate: String, | |||
/// The given user css file which allow to customize the generated | |||
/// documentation theme. | |||
pub css_file_extension: Option<PathBuf>, |
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.
Unrelated, but maybe extension_css_file?
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.
css_file_extension
seems good to me. Why would you prefer extension_css_file
?
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.
to not confuse with .css
file extension.
src/librustdoc/html/render.rs
Outdated
for implementor in &all_implementors { | ||
try_err!(writeln!(&mut v, "{}", *implementor), &mydst); | ||
v.push_str(&format!("{}", *implementor)); |
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.
to_string instead of format?
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.
src/librustdoc/html/render.rs
Outdated
@@ -1247,19 +1241,18 @@ themePicker.onblur = handleThemeButtonsBlur; | |||
// identically even with rustdoc running in parallel. | |||
all_implementors.sort(); | |||
|
|||
let mut v = Vec::new(); | |||
try_err!(writeln!(&mut v, "(function() {{var implementors = {{}};"), &mydst); | |||
let mut v = String::from("(function() {{var implementors = {{}};\n"); |
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.
It’s a bug I think? No need for double braces
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.
Indeed, it was using writeln!
before but doesn't make sense anymore to keep double braces.
src/librustdoc/html/render.rs
Outdated
fn print(self, buffer: &mut Buffer) { | ||
let cx = self.cx; | ||
let it = self.item; | ||
fn print_sidebar(cx: &Context, it: &clean::Item, buffer: &mut Buffer) { |
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.
Let’s make buf the first argument consistently?
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.
I could, but I think it's not too likely we want to do that in this PR since there are existing functions where the fmt::Formatter was in the middle of the function or so and moving that around is, while not generally error-prone due to types, quite noisy. I'd like to avoid doing it in this PR :)
src/librustdoc/html/format.rs
Outdated
self.buffer.insert_str(idx, s); | ||
} | ||
|
||
crate fn push_str(&mut self, s: &str) { |
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.
There’s write_str already
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.
This is intentional to keep the API both compatible with String and fmt::Formatter as we want to avoid noise in this PR at least.
Pushed up fixup commits to address comments, will squash them in when we get to landing point (want to avoid rebasing so as to not lose review comments and/or disrupt in-progress reviews). |
The job Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
Awesome! Doing most file writing into one place is a very good idea to improve performances. However, I'm very worried about memory consumption. Do you have a comparison before/after by any chance? |
We were always writing into |
8a1d72b
to
a9c6ef6
Compare
Rebased to squash in commits and hopefully fixed CI |
Then all good for me (weird that I remembered that used more IO than Thanks a lot for doing it! |
@bors r=GuillaumeGomez |
📌 Commit a9c6ef6 has been approved by |
…GuillaumeGomez Rustdoc: formatting to buffers This should be reviewed commit-by-commit. I've not attempted to fully flesh out what the end state of this PR could look like yet as I wanted to get it up for some early feedback (I already think this has some wins, too, so we could land it as-is). The primary idea with `Buffer` is that it internally tracks whether we're printing to HTML or text, and the goal is that eventually instead of branch on `fmt.alternate()` anywhere, we'd call a helper like `buf.nbsp()` which would either return ` ` or ` ` depending on the target we're printing to. Obviously, that's not included in this PR, in part because it was already getting quite big.
⌛ Testing commit a9c6ef6 with merge 982018376261ae04983bab41ff19efff50984eb9... |
Seems like this failed in #64115 (comment), @bors r- |
(It could also be #64096) |
@bors retry |
Almost certainly this PR, @bors r- |
This avoids needlessly creating and threading the buffers through when we only use them once.
This means that callers can pass in a closure like `|buf| some_function(..., &mut buf)` and pass in arbitrary arguments to that function without complicating the trait definition. We also keep the impl for str and String, since it's useful to be able to just pass in "" or format!("{}"...) results in some cases. This changes Print's definition to take self, instead of &self, because otherwise FnOnce cannot be called directly. We could instead take FnMut or even Fn, but that seems like it'd merely complicate matters -- most of the time, the FnOnce does not constrain us at all anyway. If it does, a custom Print impl for &'_ SomeStruct is not all that painful.
8f399bd
to
02c5c5c
Compare
@bors r=GuillaumeGomez |
📌 Commit 02c5c5c has been approved by |
Rustdoc: formatting to buffers This should be reviewed commit-by-commit. I've not attempted to fully flesh out what the end state of this PR could look like yet as I wanted to get it up for some early feedback (I already think this has some wins, too, so we could land it as-is). The primary idea with `Buffer` is that it internally tracks whether we're printing to HTML or text, and the goal is that eventually instead of branch on `fmt.alternate()` anywhere, we'd call a helper like `buf.nbsp()` which would either return ` ` or ` ` depending on the target we're printing to. Obviously, that's not included in this PR, in part because it was already getting quite big.
☀️ Test successful - checks-azure |
This should be reviewed commit-by-commit.
I've not attempted to fully flesh out what the end state of this PR could look like yet as I wanted to get it up for some early feedback (I already think this has some wins, too, so we could land it as-is).
The primary idea with
depending on the target we're printing to. Obviously, that's not included in this PR, in part because it was already getting quite big.
Buffer
is that it internally tracks whether we're printing to HTML or text, and the goal is that eventually instead of branch onfmt.alternate()
anywhere, we'd call a helper likebuf.nbsp()
which would either return
or