Skip to content
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

Avoid two allocations #6326

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use std::{borrow::Cow, iter};

use itertools::{MultiPeek, multipeek};
use itertools::{Itertools as _, MultiPeek, multipeek};
Copy link
Contributor

@ytmimi ytmimi Sep 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you explain why you're brining Itertools into scope?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is used for the Itertools::join on line 1077.

use rustc_span::Span;
use tracing::{debug, trace};

Expand Down Expand Up @@ -1056,8 +1056,7 @@ fn light_rewrite_comment(
config: &Config,
is_doc_comment: bool,
) -> String {
let lines: Vec<&str> = orig
.lines()
orig.lines()
.map(|l| {
// This is basically just l.trim(), but in the case that a line starts
// with `*` we want to leave one space before it, so it aligns with the
Expand All @@ -1075,8 +1074,7 @@ fn light_rewrite_comment(
// Preserve markdown's double-space line break syntax in doc comment.
trim_end_unless_two_whitespaces(left_trimmed, is_doc_comment)
})
.collect();
lines.join(&format!("\n{}", offset.to_string(config)))
.join(&format!("\n{}", offset.to_string(config)))
Comment on lines -1078 to +1077
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, so just want to make sure I'm understanding this correctly. This avoids allocating the Vec<&str> and directly allocates into a String?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly.

}

/// Trims comment characters and possibly a single space from the left of a string.
Expand Down
2 changes: 1 addition & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,7 @@ fn rewrite_int_lit(
format!(
"0x{}{}",
hex_lit,
token_lit.suffix.map_or(String::new(), |s| s.to_string())
token_lit.suffix.as_ref().map_or("", |s| s.as_str())
),
context.config.max_width(),
shape,
Expand Down
Loading