Skip to content

Commit 5165270

Browse files
committed
Auto merge of #8641 - weihanglo:fix/remove-alloc, r=Eh2406
fix: remove unnecessary allocations Remove unnecessary `str::to_string` and `str::replace` allocations by using iterators. This PR is almost identical to #8622 except it does not skip the generated header. Sorry that I did not profile the changes by myself. Seems that valgrind does not support macOS 10.15.6, I have not idea how to profile cargo subcommand. It would be great for an instruction to run a memory profiling for Rust program on macOS.
2 parents 868a1cf + 0c70319 commit 5165270

File tree

1 file changed

+3
-17
lines changed

1 file changed

+3
-17
lines changed

src/cargo/ops/lockfile.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -174,35 +174,21 @@ fn serialize_resolve(resolve: &Resolve, orig: Option<&str>) -> String {
174174
}
175175

176176
fn are_equal_lockfiles(orig: &str, current: &str, ws: &Workspace<'_>) -> bool {
177-
let mut orig = orig.to_string();
178-
if has_crlf_line_endings(&orig) {
179-
orig = orig.replace("\r\n", "\n");
180-
}
181-
182177
// If we want to try and avoid updating the lock file, parse both and
183178
// compare them; since this is somewhat expensive, don't do it in the
184179
// common case where we can update lock files.
185180
if !ws.config().lock_update_allowed() {
186181
let res: CargoResult<bool> = (|| {
187-
let old: resolver::EncodableResolve = toml::from_str(&orig)?;
182+
let old: resolver::EncodableResolve = toml::from_str(orig)?;
188183
let new: resolver::EncodableResolve = toml::from_str(current)?;
189-
Ok(old.into_resolve(&orig, ws)? == new.into_resolve(current, ws)?)
184+
Ok(old.into_resolve(orig, ws)? == new.into_resolve(current, ws)?)
190185
})();
191186
if let Ok(true) = res {
192187
return true;
193188
}
194189
}
195190

196-
current == orig
197-
}
198-
199-
fn has_crlf_line_endings(s: &str) -> bool {
200-
// Only check the first line.
201-
if let Some(lf) = s.find('\n') {
202-
s[..lf].ends_with('\r')
203-
} else {
204-
false
205-
}
191+
orig.lines().eq(current.lines())
206192
}
207193

208194
fn emit_package(dep: &toml::value::Table, out: &mut String) {

0 commit comments

Comments
 (0)