changeset 52806:0a0ed46ef6d6

rust-pyo3-revlog: _index_slicechunktodensity Again, some small win on the copy front, hoping these add up.
author Georges Racinet <georges.racinet@cloudcrane.io>
date Tue, 24 Dec 2024 01:29:07 +0100
parents a8debfd85d55
children 0fc15732fe7b
files rust/hg-pyo3/src/revlog/mod.rs
diffstat 1 files changed, 33 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-pyo3/src/revlog/mod.rs	Tue Dec 24 00:25:49 2024 +0100
+++ b/rust/hg-pyo3/src/revlog/mod.rs	Tue Dec 24 01:29:07 2024 +0100
@@ -604,6 +604,39 @@
         Ok((py_chain, py_stopped).into_pyobject(py)?.unbind())
     }
 
+    /// slice planned chunk read to reach a density threshold
+    fn _index_slicechunktodensity(
+        slf: &Bound<'_, Self>,
+        py: Python<'_>,
+        revs: &Bound<'_, PyAny>,
+        target_density: f64,
+        min_gap_size: usize,
+    ) -> PyResult<PyObject> {
+        let as_nested_vec =
+            Self::with_index_read(slf, |idx| {
+                let revs: Vec<_> = rev_pyiter_collect(revs, idx)?;
+                Ok(idx.slice_chunk_to_density(
+                    &revs,
+                    target_density,
+                    min_gap_size,
+                ))
+            })?;
+        let res_len = as_nested_vec.len();
+
+        // cannot build the outer sequence from iterator, because
+        // `rev_py_list()` returns `Result<T>` instead of `T`.
+        let mut res = Vec::with_capacity(res_len);
+        for chunk in as_nested_vec {
+            res.push(revs_py_list(py, chunk)?.into_any());
+        }
+        // This is just to do the same as C, not sure why it does this
+        Ok(if res_len == 1 {
+            PyTuple::new(py, res)?.unbind().into_any()
+        } else {
+            PyList::new(py, res)?.unbind().into_any()
+        })
+    }
+
     fn _index___len__(slf: &Bound<'_, Self>) -> PyResult<usize> {
         Self::with_index_read(slf, |idx| Ok(idx.len()))
     }