Patchwork D9136: rust-parsers: use in-place mutation instead of allocating a new `Vec`

login
register
mail settings
Submitter phabricator
Date Oct. 1, 2020, 7:58 a.m.
Message ID <differential-rev-PHID-DREV-zec4qhccjfktvyng6kwd-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/47357/
State Superseded
Headers show

Comments

phabricator - Oct. 1, 2020, 7:58 a.m.
Alphare created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  This is not done for the `dirstate-tree` feature, since it lacks `iter_mut`.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D9136

AFFECTED FILES
  rust/hg-core/src/dirstate/parsers.rs

CHANGE DETAILS




To: Alphare, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/rust/hg-core/src/dirstate/parsers.rs b/rust/hg-core/src/dirstate/parsers.rs
--- a/rust/hg-core/src/dirstate/parsers.rs
+++ b/rust/hg-core/src/dirstate/parsers.rs
@@ -107,12 +107,11 @@ 
     let expected_size = expected_size + PARENT_SIZE * 2;
 
     let mut packed = Vec::with_capacity(expected_size);
-    let mut new_state_map = vec![];
 
     packed.extend(&parents.p1);
     packed.extend(&parents.p2);
 
-    for (filename, entry) in state_map.iter() {
+    for (filename, entry) in state_map.iter_mut() {
         let new_filename = filename.to_owned();
         let mut new_mtime: i32 = entry.mtime;
         if entry.state == EntryState::Normal && entry.mtime == now {
@@ -126,13 +125,10 @@ 
             // contents of the file if the size is the same. This prevents
             // mistakenly treating such files as clean.
             new_mtime = -1;
-            new_state_map.push((
-                filename.to_owned(),
-                DirstateEntry {
-                    mtime: new_mtime,
-                    ..*entry
-                },
-            ));
+            *entry = DirstateEntry {
+                mtime: new_mtime,
+                ..*entry
+            };
         }
         let mut new_filename = new_filename.into_vec();
         if let Some(copy) = copy_map.get(filename) {
@@ -152,8 +148,6 @@ 
         return Err(DirstatePackError::BadSize(expected_size, packed.len()));
     }
 
-    state_map.extend(new_state_map);
-
     Ok(packed)
 }
 /// `now` is the duration in seconds since the Unix epoch