Skip to content

Commit

Permalink
Auto merge of #8641 - weihanglo:fix/remove-alloc, r=Eh2406
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Aug 23, 2020
2 parents 868a1cf + 0c70319 commit 5165270
Showing 1 changed file with 3 additions and 17 deletions.
20 changes: 3 additions & 17 deletions src/cargo/ops/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,35 +174,21 @@ fn serialize_resolve(resolve: &Resolve, orig: Option<&str>) -> String {
}

fn are_equal_lockfiles(orig: &str, current: &str, ws: &Workspace<'_>) -> bool {
let mut orig = orig.to_string();
if has_crlf_line_endings(&orig) {
orig = orig.replace("\r\n", "\n");
}

// If we want to try and avoid updating the lock file, parse both and
// compare them; since this is somewhat expensive, don't do it in the
// common case where we can update lock files.
if !ws.config().lock_update_allowed() {
let res: CargoResult<bool> = (|| {
let old: resolver::EncodableResolve = toml::from_str(&orig)?;
let old: resolver::EncodableResolve = toml::from_str(orig)?;
let new: resolver::EncodableResolve = toml::from_str(current)?;
Ok(old.into_resolve(&orig, ws)? == new.into_resolve(current, ws)?)
Ok(old.into_resolve(orig, ws)? == new.into_resolve(current, ws)?)
})();
if let Ok(true) = res {
return true;
}
}

current == orig
}

fn has_crlf_line_endings(s: &str) -> bool {
// Only check the first line.
if let Some(lf) = s.find('\n') {
s[..lf].ends_with('\r')
} else {
false
}
orig.lines().eq(current.lines())
}

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

0 comments on commit 5165270

Please sign in to comment.