[Request] [+- ] D11412: rhg: Don’t compare ambiguous files one byte at a time

SimonSapin phabricator at mercurial-scm.org
Mon Sep 13 18:14:57 UTC 2021


SimonSapin created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  Even though the use of `BufReader` reduces the number of syscalls to read
  the file from disk, `.bytes()` yields a separate `Result` for every byte.
  Creating those results and dispatching on them is most likely costly.
  
  Instead, this commit opts for simplicity by reading the entire file into memory
  and comparing a single pair of byte strings. Note that memory already needs to
  contain the entire previous contents of the file, as read from the filelog.
  So with an extremely large file this doubles memory use but does not make it
  grow by orders of magnitude.
  
  At first I wrote code that still avoids reading the entire file into memory
  and compares one buffer at a time with `BufReader`. Find this code below for
  posterity. However its correctness is subtle. I ended up preferring the
  simplicity of the obviously-correct single comparison.
  
    rust
    let mut reader = BufReader::new(fobj);
    let mut expected = &contents_in_p1[..];
    loop {
        let buf = reader.fill_buf().when_reading_file(&fs_path)?;
        if buf.is_empty() {
            // Found EOF
            return Ok(expected.is_empty());
        } else if let Some(rest) = expected.drop_prefix(buf) {
            // What we read so far matches the expected content, continue reading
            let buf_len = buf.len();
            reader.consume(buf_len);
            expected = rest
        } else {
            // Found different content
            return Ok(false);
        }
    }

REPOSITORY
  rHG Mercurial

BRANCH
  default

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

AFFECTED FILES
  rust/Cargo.lock
  rust/rhg/src/commands/status.rs

CHANGE DETAILS

diff --git a/rust/rhg/src/commands/status.rs b/rust/rhg/src/commands/status.rs
--- a/rust/rhg/src/commands/status.rs
+++ b/rust/rhg/src/commands/status.rs
@@ -10,17 +10,13 @@
 use clap::{Arg, SubCommand};
 use hg;
 use hg::dirstate_tree::dispatch::DirstateMapMethods;
-use hg::errors::{HgError, IoResultExt};
+use hg::errors::HgError;
 use hg::manifest::Manifest;
 use hg::matchers::AlwaysMatcher;
 use hg::repo::Repo;
 use hg::utils::hg_path::{hg_path_to_os_string, HgPath};
 use hg::{HgPathCow, StatusOptions};
 use log::{info, warn};
-use std::convert::TryInto;
-use std::fs;
-use std::io::BufReader;
-use std::io::Read;
 
 pub const HELP_TEXT: &str = "
 Show changed files in the working directory
@@ -279,26 +275,7 @@
     })?;
     let contents_in_p1 = filelog_entry.data()?;
 
-    let fs_path = repo
-        .working_directory_vfs()
-        .join(hg_path_to_os_string(hg_path).expect("HgPath conversion"));
-    let hg_data_len: u64 = match contents_in_p1.len().try_into() {
-        Ok(v) => v,
-        Err(_) => {
-            // conversion of data length to u64 failed,
-            // good luck for any file to have this content
-            return Ok(true);
-        }
-    };
-    let fobj = fs::File::open(&fs_path).when_reading_file(&fs_path)?;
-    if fobj.metadata().when_reading_file(&fs_path)?.len() != hg_data_len {
-        return Ok(true);
-    }
-    for (fs_byte, &hg_byte) in BufReader::new(fobj).bytes().zip(contents_in_p1)
-    {
-        if fs_byte.when_reading_file(&fs_path)? != hg_byte {
-            return Ok(true);
-        }
-    }
-    Ok(false)
+    let fs_path = hg_path_to_os_string(hg_path).expect("HgPath conversion");
+    let fs_contents = repo.working_directory_vfs().read(fs_path)?;
+    return Ok(contents_in_p1 == &*fs_contents);
 }
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -1,7 +1,5 @@
 # This file is automatically @generated by Cargo.
 # It is not intended for manual editing.
-version = 3
-
 [[package]]
 name = "adler"
 version = "0.2.3"



To: SimonSapin, #hg-reviewers
Cc: mercurial-patches, mercurial-devel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mercurial-scm.org/pipermail/mercurial-patches/attachments/20210913/a34bd0af/attachment-0001.html>


More information about the Mercurial-patches mailing list