Patchwork D7300: rust-status: refactor dispatch case for normal files

login
register
mail settings
Submitter phabricator
Date Nov. 7, 2019, 10:18 a.m.
Message ID <differential-rev-PHID-DREV-iyr56msufluylsam2zro-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/42895/
State Superseded
Headers show

Comments

phabricator - Nov. 7, 2019, 10:18 a.m.
Alphare created this revision.
Herald added subscribers: mercurial-devel, kevincox, durin42.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  This should make the code easier to read and more idiomatic.

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

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

CHANGE DETAILS




To: Alphare, #hg-reviewers
Cc: durin42, kevincox, mercurial-devel
phabricator - Nov. 7, 2019, 6:24 p.m.
kevincox added inline comments.
kevincox accepted this revision.

INLINE COMMENTS

> status.rs:31
>  
>  #[inline]
> +/// Dates and times that are outside the 31-bit signed range are compared

I see a lot of these and in general I would recommend against it. The compiler is smart. Unless profiling shows an advantage in a specific case I would leave them out.

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST ACTION
  https://phab.mercurial-scm.org/D7300/new/

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

To: Alphare, #hg-reviewers, kevincox
Cc: durin42, kevincox, mercurial-devel

Patch

diff --git a/rust/hg-core/src/dirstate/status.rs b/rust/hg-core/src/dirstate/status.rs
--- a/rust/hg-core/src/dirstate/status.rs
+++ b/rust/hg-core/src/dirstate/status.rs
@@ -29,6 +29,18 @@ 
 }
 
 #[inline]
+/// Dates and times that are outside the 31-bit signed range are compared
+/// modulo 2^31. This should prevent hg from behaving badly with very large
+/// files or corrupt dates while still having a high probability of detecting
+/// changes. (issue2608)
+/// TODO I haven't found a way of having `b` be `Into<i32>`, since `From<u64>`
+/// is not defined for `i32`, and there is no `As` trait. This forces the
+/// caller to cast `b` as `i32`.
+fn mod_compare(a: i32, b: i32) -> bool {
+    a & i32::max_value() != b & i32::max_value()
+}
+
+#[inline]
 /// The file corresponding to the dirstate entry was found on the filesystem.
 fn dispatch_found(
     filename: impl AsRef<HgPath>,
@@ -55,26 +67,17 @@ 
 
     match state {
         EntryState::Normal => {
-            // Dates and times that are outside the 31-bit signed
-            // range are compared modulo 2^31. This should prevent
-            // it from behaving badly with very large files or
-            // corrupt dates while still having a high probability
-            // of detecting changes. (issue2608)
-            let range_mask = 0x7fffffff;
-
-            let size_changed = (size != st_size as i32)
-                && size != (st_size as i32 & range_mask);
+            let size_changed = mod_compare(size, st_size as i32);
             let mode_changed =
                 (mode ^ st_mode as i32) & 0o100 != 0o000 && check_exec;
-            if size >= 0
-                            && (size_changed || mode_changed)
-                            || size == -2  // other parent
-                            || copy_map.contains_key(filename.as_ref())
+            let metadata_changed = size >= 0 && (size_changed || mode_changed);
+            let other_parent = size == -2;
+            if metadata_changed
+                || other_parent
+                || copy_map.contains_key(filename.as_ref())
             {
                 Dispatch::Modified
-            } else if mtime != st_mtime as i32
-                && mtime != (st_mtime as i32 & range_mask)
-            {
+            } else if mod_compare(mtime, st_mtime as i32) {
                 Dispatch::Unsure
             } else if st_mtime == last_normal_time {
                 // the file may have just been marked as normal and