Mercurial > hg
changeset 47965:f9e6f2bb721d
rhg: Don’t compare ambiguous files one byte at a time
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);
}
}
```
Differential Revision: https://phab.mercurial-scm.org/D11412
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Mon, 13 Sep 2021 18:48:48 +0200 |
parents | 796206e74b10 |
children | 681851d6409b |
files | rust/rhg/src/commands/status.rs |
diffstat | 1 files changed, 4 insertions(+), 27 deletions(-) [+] |
line wrap: on
line diff
--- a/rust/rhg/src/commands/status.rs Mon Sep 13 18:09:10 2021 +0200 +++ b/rust/rhg/src/commands/status.rs Mon Sep 13 18:48:48 2021 +0200 @@ -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); }