navid/veins / commit

eb4f3d934fd0

Navid M <navidmomtahen@gmail.com> · 2026-11-30

parents: 30fe17c70c01

Browse files at this commit · Download source

opt: fixes to rs implementation
--- a/veinsrs/src/cli/info.rs+++ b/veinsrs/src/cli/info.rs@@ -546,17 +546,16 @@          let measure = |entry: &&tree::FlatEntry| -> FileResult {             let attributes = rules.lookup(&String::from_utf8_lossy(&entry.path));-            let data = graph::read_blob_loaded(&store, entry.target)+            let mut measured = graph::BlobMeasure::default();+            let ok = graph::measure_blob_loaded(&store, entry.target, &mut measured)                 .ok()-                .flatten()-                .unwrap_or_default();+                .unwrap_or(false);             let binary = attributes.text == AttributeText::Binary-                || (attributes.text == AttributeText::Auto-                    && data.iter().take(8192).any(|byte| *byte == 0));+                || (attributes.text == AttributeText::Auto && measured.has_nul);             FileResult {                 ext: ext_of(&entry.path),-                lines: if binary { 0 } else { count_nonblank(&data) },-                bytes: data.len() as u64,+                lines: if binary || !ok { 0 } else { measured.lines },+                bytes: if ok { measured.bytes } else { 0 },                 generated: attributes.generated,             }         };@@ -836,25 +835,3 @@     } } -fn count_nonblank(data: &[u8]) -> u64 {-    let mut count = 0;-    let mut start = 0;-    while start <= data.len() {-        let end = data[start..]-            .iter()-            .position(|byte| *byte == b'\n')-            .map(|offset| start + offset)-            .unwrap_or(data.len());-        if data[start..end]-            .iter()-            .any(|byte| !matches!(*byte, b' ' | b'\t' | b'\r'))-        {-            count += 1;-        }-        if end >= data.len() {-            break;-        }-        start = end + 1;-    }-    count-}--- a/veinsrs/src/store/graph.rs+++ b/veinsrs/src/store/graph.rs@@ -275,8 +275,14 @@             output.extend_from_slice(&blob.data);         }         BlobForm::Leaf => {+            let total: u64 = blob.entries.iter().map(|entry| entry.length).sum();+            if let Ok(total) = usize::try_from(total) {+                if total <= crate::object::MAX_OBJECT_BODY {+                    output.reserve(total);+                }+            }             for entry in blob.entries {-                let Some(chunk) = store.body_loaded(entry.id, TYPE_CHUNK)? else {+                let Some(chunk) = store.body_raw_loaded(entry.id, TYPE_CHUNK)? else {                     return Ok(false);                 };                 output.extend_from_slice(&chunk);@@ -305,6 +311,92 @@ pub fn read_blob(store: &mut Store, id: Id) -> Result<Option<Vec<u8>>> {     store.preload()?;     read_blob_loaded(store, id)+}++#[derive(Default)]+pub struct BlobMeasure {+    pub bytes: u64,+    pub lines: u64,+    pub has_nul: bool,+    line_has_content: bool,+}++fn scan_blob_bytes(data: &[u8], out: &mut BlobMeasure) {+    if out.bytes < 8192 {+        let prefix = data.len().min((8192 - out.bytes) as usize);+        if data[..prefix].contains(&0) {+            out.has_nul = true;+        }+    }+    let mut start = 0;+    while start < data.len() {+        let end = data[start..]+            .iter()+            .position(|byte| *byte == b'\n')+            .map(|offset| start + offset)+            .unwrap_or(data.len());+        if !out.line_has_content+            && data[start..end]+                .iter()+                .any(|byte| !matches!(*byte, b' ' | b'\t' | b'\r'))+        {+            out.line_has_content = true;+        }+        if end >= data.len() {+            break;+        }+        if out.line_has_content {+            out.lines += 1;+        }+        out.line_has_content = false;+        start = end + 1;+    }+    out.bytes += data.len() as u64;+}++fn measure_blob_into(+    store: &Store,+    id: Id,+    output: &mut BlobMeasure,+    depth: usize,+) -> Result<bool> {+    if depth > 64 {+        return Err(Error::new("blob nesting too deep"));+    }+    let Some(body) = store.body_raw_loaded(id, TYPE_BLOB)? else {+        return Ok(false);+    };+    let blob = blob::decode(&body)?;+    match blob.form {+        BlobForm::Inline => scan_blob_bytes(&blob.data, output),+        BlobForm::Leaf => {+            for entry in blob.entries {+                let Some(chunk) = store.body_raw_loaded(entry.id, TYPE_CHUNK)? else {+                    return Ok(false);+                };+                scan_blob_bytes(&chunk, output);+            }+        }+        BlobForm::Interior => {+            for entry in blob.entries {+                if !measure_blob_into(store, entry.id, output, depth + 1)? {+                    return Ok(false);+                }+            }+        }+    }+    Ok(true)+}++pub fn measure_blob_loaded(store: &Store, id: Id, output: &mut BlobMeasure) -> Result<bool> {+    if !measure_blob_into(store, id, output, 0)? {+        return Ok(false);+    }+    if output.line_has_content {+        output.lines += 1;+        output.line_has_content = false;+    }+    Ok(true) }  #[derive(Clone)]--- a/veinsrs/src/store/reader.rs+++ b/veinsrs/src/store/reader.rs@@ -366,6 +366,17 @@             return Ok(None);         }         Ok(Some(body))+    }++    /// Read a body without re-hashing it, for callers that only inspect contents.+    pub fn body_raw_loaded(&self, id: Id, expected: u8) -> Result<Option<Vec<u8>>> {+        let Some(location) = self.find_loaded(id) else {+            return Ok(None);+        };+        if location.kind != expected {+            return Ok(None);+        }+        Ok(Some(self.get_at(location, 0)?))     }      pub fn preload(&mut self) -> Result<()> {@@ -372,6 +383,7 @@         for index in 0..self.packs.len() {             self.ensure_index(index);             self.open_file(index);+            self.ensure_mapping(index);         }         Ok(())     }--- a/veinsrs/src/worktree/snapshot.rs+++ b/veinsrs/src/worktree/snapshot.rs@@ -246,18 +246,18 @@     // Spinning up rayon costs more than it saves for the small, common case;     // only parallelise once there is enough hashing work to amortise it.     const PARALLEL_HASH_MIN_BYTES: u64 = 32 * 1024 * 1024;-    let built: Vec<Option<blob::BlobBuild>> = if job_bytes >= PARALLEL_HASH_MIN_BYTES {+    let built: Vec<Option<(blob::BlobBuild, bool)>> = if job_bytes >= PARALLEL_HASH_MIN_BYTES {         jobs.par_iter()-            .map(|index| read_build(&plans[*index].path, &plans[*index].rel, attributes))+            .map(|index| read_build(&plans[*index], attributes))             .collect()     } else {         jobs.iter()-            .map(|index| read_build(&plans[*index].path, &plans[*index].rel, attributes))+            .map(|index| read_build(&plans[*index], attributes))             .collect()     };      for (job, build) in jobs.iter().zip(built) {-        let Some(build) = build else {+        let Some((build, trusted)) = build else {             state.failed = true;             return Ok(None);         };@@ -265,6 +265,7 @@             txn.add_known(object.kind, object.id, object.body);         }         plans[*job].target = Some(build.root);+        plans[*job].trusted = plans[*job].trusted || trusted;     }      let mut entries = Vec::with_capacity(plans.len());@@ -365,21 +366,25 @@     Ok(Some(txn.add_object(TYPE_TREE, tree::encode(&entries)?))) } -fn read_build(-    path: &Path,-    relative: &[u8],-    attributes: &AttributeRules,-) -> Option<blob::BlobBuild> {-    let data = core::read_file_opt(path, 0)?;-    let path_attributes = attributes.lookup(&String::from_utf8_lossy(relative));-    if path_attributes.eol != AttributeEol::None && path_attributes.text != AttributeText::Binary {-        Some(blob::build(&attributes::normalize_eol(-            &data,-            path_attributes.eol,-        )))+fn read_build(plan: &Plan, attributes: &AttributeRules) -> Option<(blob::BlobBuild, bool)> {+    let data = core::read_file_opt(&plan.path, 0)?;+    let path_attributes = attributes.lookup(&String::from_utf8_lossy(&plan.rel));+    let build = if path_attributes.eol != AttributeEol::None+        && path_attributes.text != AttributeText::Binary+    {+        blob::build(&attributes::normalize_eol(&data, path_attributes.eol))     } else {-        Some(blob::build(&data))-    }+        blob::build(&data)+    };+    let trusted = plan.info.mtime_ns % 1_000_000 != 0+        && core::stat_path(&plan.path).is_some_and(|again| {+            again.size == plan.info.size+                && again.mtime_ns == plan.info.mtime_ns+                && again.inode == plan.info.inode+                && again.kind == plan.info.kind+                && again.executable == plan.info.executable+        });+    Some((build, trusted)) }  fn read_blob_from_bytes(txn: &mut Txn, data: &[u8]) -> Option<Id> {