Patchwork [11,of,14] sparse-revlog: add a `_trimchunk` function in C

login
register
mail settings
Submitter Boris Feld
Date Nov. 12, 2018, 9:55 a.m.
Message ID <0ea42453fa491793d1e1.1542016546@localhost.localdomain>
Download mbox | patch
Permalink /patch/36519/
State Accepted
Headers show

Comments

Boris Feld - Nov. 12, 2018, 9:55 a.m.
# HG changeset patch
# User Boris Feld <boris.feld@octobus.net>
# Date 1541785523 -3600
#      Fri Nov 09 18:45:23 2018 +0100
# Node ID 0ea42453fa491793d1e145f5093b65e84cb65e97
# Parent  036a7425b869b5b8e3e7f528ed6d992c48ebb52e
# EXP-Topic sparse-perf
# Available At https://bitbucket.org/octobus/mercurial-devel/
#              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r 0ea42453fa49
sparse-revlog: add a `_trimchunk` function in C

We are about to implement a native version of `slicechunktodensity`. For
clarity, we introduce the helper functions first.

This function is a native implementation of the python function `_trimchunk`
in `mercurial/revlogutils/deltas.py`.
Yuya Nishihara - Nov. 13, 2018, 12:54 p.m.
On Mon, 12 Nov 2018 10:55:46 +0100, Boris Feld wrote:
> # HG changeset patch
> # User Boris Feld <boris.feld@octobus.net>
> # Date 1541785523 -3600
> #      Fri Nov 09 18:45:23 2018 +0100
> # Node ID 0ea42453fa491793d1e145f5093b65e84cb65e97
> # Parent  036a7425b869b5b8e3e7f528ed6d992c48ebb52e
> # EXP-Topic sparse-perf
> # Available At https://bitbucket.org/octobus/mercurial-devel/
> #              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r 0ea42453fa49
> sparse-revlog: add a `_trimchunk` function in C
> 
> We are about to implement a native version of `slicechunktodensity`. For
> clarity, we introduce the helper functions first.
> 
> This function is a native implementation of the python function `_trimchunk`
> in `mercurial/revlogutils/deltas.py`.
> 
> diff --git a/mercurial/cext/revlog.c b/mercurial/cext/revlog.c
> --- a/mercurial/cext/revlog.c
> +++ b/mercurial/cext/revlog.c
> @@ -1018,6 +1018,29 @@ static inline long index_segment_span(in
>  	        index_get_length(self, end_rev));
>  }
>  
> +/* returns revs[startidx:endidx] without empty trailing revs */
> +static PyObject *_trimchunk(indexObject *self, PyObject *revs, long startidx,
> +                            long endidx)
> +{
> +	while (endidx > 1 && endidx > startidx) {
> +		PyObject *rev = PyList_GET_ITEM(revs, endidx - 1);
> +		if (rev == NULL) {
> +			return NULL;
> +		}

PyList_GET_ITEM() should never fail.

> +		Py_ssize_t r = PyInt_AsLong(rev);
> +		if (r == -1 && PyErr_Occurred()) {
> +			return NULL;
> +		}
> +		if (index_get_length(self, r - 1) != 0) {
> +			break;
> +		}
> +		endidx -= 1;
> +	}
> +	PyObject *chunk = PyList_GetSlice(revs, startidx, endidx);
> +	Py_INCREF(chunk);
> +	return chunk;

Excessive incref? PyList_GetSlice() is documented to return new reference.

Patch

diff --git a/mercurial/cext/revlog.c b/mercurial/cext/revlog.c
--- a/mercurial/cext/revlog.c
+++ b/mercurial/cext/revlog.c
@@ -1018,6 +1018,29 @@  static inline long index_segment_span(in
 	        index_get_length(self, end_rev));
 }
 
+/* returns revs[startidx:endidx] without empty trailing revs */
+static PyObject *_trimchunk(indexObject *self, PyObject *revs, long startidx,
+                            long endidx)
+{
+	while (endidx > 1 && endidx > startidx) {
+		PyObject *rev = PyList_GET_ITEM(revs, endidx - 1);
+		if (rev == NULL) {
+			return NULL;
+		}
+		Py_ssize_t r = PyInt_AsLong(rev);
+		if (r == -1 && PyErr_Occurred()) {
+			return NULL;
+		}
+		if (index_get_length(self, r - 1) != 0) {
+			break;
+		}
+		endidx -= 1;
+	}
+	PyObject *chunk = PyList_GetSlice(revs, startidx, endidx);
+	Py_INCREF(chunk);
+	return chunk;
+}
+
 static inline int nt_level(const char *node, Py_ssize_t level)
 {
 	int v = node[level >> 1];