From d86745bf998900fa2160e60abc7d03250c967b2a Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Sun, 23 Aug 2020 08:19:16 +0800 Subject: [PATCH 1/2] fix: remove unnecessary allocations --- src/cargo/ops/lockfile.rs | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/cargo/ops/lockfile.rs b/src/cargo/ops/lockfile.rs index 9ed0c76d27b..873fe0daaa0 100644 --- a/src/cargo/ops/lockfile.rs +++ b/src/cargo/ops/lockfile.rs @@ -174,34 +174,33 @@ 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 = (|| { - 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 + let mut orig_iter = orig.lines(); + let mut current_iter = current.lines(); + loop { + match (orig_iter.next(), current_iter.next()) { + (Some(o), Some(c)) => { + if o != c { + return false; + } + } + (Some(_), None) => return false, + (None, Some(_)) => return false, + (None, None) => return true, + } } } From 0c70319afe062b5e7f3ae1d2c03c17c970d3dceb Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Sun, 23 Aug 2020 09:07:54 +0800 Subject: [PATCH 2/2] refactor: compare lockfile use Iterator.eq --- src/cargo/ops/lockfile.rs | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/src/cargo/ops/lockfile.rs b/src/cargo/ops/lockfile.rs index 873fe0daaa0..ea659c095e5 100644 --- a/src/cargo/ops/lockfile.rs +++ b/src/cargo/ops/lockfile.rs @@ -188,20 +188,7 @@ fn are_equal_lockfiles(orig: &str, current: &str, ws: &Workspace<'_>) -> bool { } } - let mut orig_iter = orig.lines(); - let mut current_iter = current.lines(); - loop { - match (orig_iter.next(), current_iter.next()) { - (Some(o), Some(c)) => { - if o != c { - return false; - } - } - (Some(_), None) => return false, - (None, Some(_)) => return false, - (None, None) => return true, - } - } + orig.lines().eq(current.lines()) } fn emit_package(dep: &toml::value::Table, out: &mut String) {