Patchwork [2,of,7,V5] sparse-revlog: add a `index_get_length` function in C

login
register
mail settings
Submitter Boris Feld
Date Nov. 22, 2018, 6:08 p.m.
Message ID <18864760091a1622d040.1542910084@localhost.localdomain>
Download mbox | patch
Permalink /patch/36716/
State Accepted
Headers show

Comments

Boris Feld - Nov. 22, 2018, 6:08 p.m.
# HG changeset patch
# User Boris Feld <boris.feld@octobus.net>
# Date 1541785378 -3600
#      Fri Nov 09 18:42:58 2018 +0100
# Node ID 18864760091a1622d0404e9a87923cf2b1b82082
# Parent  b6fff7b07488608fe8ea86ffb69a74037ed15cbe
# EXP-Topic sparse-perf
# Available At https://bitbucket.org/octobus/mercurial-devel/
#              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r 18864760091a
sparse-revlog: add a `index_get_length` function in C

We are about to implement a native version of `slicechunktodensity`. For
clarity, we introduce the helper functions first. This new function provides
an efficient way to retrieve some of the information needed by
`slicechunktodensity`.
Yuya Nishihara - Nov. 24, 2018, 1:06 a.m.
On Thu, 22 Nov 2018 19:08:04 +0100, Boris Feld wrote:
> # HG changeset patch
> # User Boris Feld <boris.feld@octobus.net>
> # Date 1541785378 -3600
> #      Fri Nov 09 18:42:58 2018 +0100
> # Node ID 18864760091a1622d0404e9a87923cf2b1b82082
> # Parent  b6fff7b07488608fe8ea86ffb69a74037ed15cbe
> # EXP-Topic sparse-perf
> # Available At https://bitbucket.org/octobus/mercurial-devel/
> #              hg pull https://bitbucket.org/octobus/mercurial-devel/ -r 18864760091a
> sparse-revlog: add a `index_get_length` function in C

> diff --git a/mercurial/cext/revlog.c b/mercurial/cext/revlog.c
> --- a/mercurial/cext/revlog.c
> +++ b/mercurial/cext/revlog.c
> @@ -218,6 +218,31 @@ static inline int64_t index_get_start(in
>  	return (int64_t)(offset >> 16);
>  }
>  
> +static inline int index_get_length(indexObject *self, Py_ssize_t rev)
> +{
> +	if (rev >= self->length) {
> +		PyObject *tuple;
> +		PyObject *pylong;
> +		long ret;
> +		tuple = PyList_GET_ITEM(self->added, rev - self->length);
> +		pylong = PyTuple_GET_ITEM(tuple, 1);
> +		ret = PyInt_AsLong(pylong);
> +		if (ret == -1 && PyErr_Occurred()) {
> +			return -1;
> +		}
> +		if (ret < 0 || ret > (long)INT_MAX) {
> +			PyErr_Format(PyExc_OverflowError,
> +			             "revlog entry size out of bound (%llu)",
> +			             (long long)ret);

Changed this to %ld ret as well.

> +			return -1;
> +		}
> +		return (int)ret;
> +	} else {
> +		const char *data = index_deref(self, rev);
> +		return (int)getbe32(data + 8);

Here, (int)getbe32(data + 8) may be negative. We have to check the underflow
so that Python interpreter wouldn't confused by bad NULL return.

Can you send a follow up?

Patch

diff --git a/mercurial/cext/revlog.c b/mercurial/cext/revlog.c
--- a/mercurial/cext/revlog.c
+++ b/mercurial/cext/revlog.c
@@ -218,6 +218,31 @@  static inline int64_t index_get_start(in
 	return (int64_t)(offset >> 16);
 }
 
+static inline int index_get_length(indexObject *self, Py_ssize_t rev)
+{
+	if (rev >= self->length) {
+		PyObject *tuple;
+		PyObject *pylong;
+		long ret;
+		tuple = PyList_GET_ITEM(self->added, rev - self->length);
+		pylong = PyTuple_GET_ITEM(tuple, 1);
+		ret = PyInt_AsLong(pylong);
+		if (ret == -1 && PyErr_Occurred()) {
+			return -1;
+		}
+		if (ret < 0 || ret > (long)INT_MAX) {
+			PyErr_Format(PyExc_OverflowError,
+			             "revlog entry size out of bound (%llu)",
+			             (long long)ret);
+			return -1;
+		}
+		return (int)ret;
+	} else {
+		const char *data = index_deref(self, rev);
+		return (int)getbe32(data + 8);
+	}
+}
+
 /*
  * RevlogNG format (all in big endian, data may be inlined):
  *    6 bytes: offset