]> git.michaelhowe.org Git - packages/b/bup.git/commitdiff
_helpers.extract_bits(): rewrite git.extract_bits() in C.
authorAvery Pennarun <apenwarr@gmail.com>
Fri, 27 Aug 2010 03:19:49 +0000 (20:19 -0700)
committerAvery Pennarun <apenwarr@gmail.com>
Fri, 27 Aug 2010 03:20:43 +0000 (20:20 -0700)
That makes our memtest run just slightly faster: 2.8 seconds instead of 3.0
seconds, which catches us back up with the pre-interpolation-search code.
Thus we should now be able to release this patch without feeling embarrassed
:)

Signed-off-by: Avery Pennarun <apenwarr@gmail.com>
lib/bup/_helpers.c
lib/bup/git.py

index 510ab3f15032e09cccc351b4dd0745d1b4d0daab..75d26030545fc9b59041937c5e35a46401bdd0cc 100644 (file)
@@ -77,6 +77,25 @@ static PyObject *firstword(PyObject *self, PyObject *args)
 }
 
 
+static PyObject *extract_bits(PyObject *self, PyObject *args)
+{
+    unsigned char *buf = NULL;
+    int len = 0, nbits = 0;
+    uint32_t v, mask;
+
+    if (!PyArg_ParseTuple(args, "t#i", &buf, &len, &nbits))
+       return NULL;
+    
+    if (len < 4)
+       return NULL;
+    
+    mask = (1<<nbits) - 1;
+    v = ntohl(*(uint32_t *)buf);
+    v = (v >> (32-nbits)) & mask;
+    return Py_BuildValue("I", v);
+}
+
+
 // I would have made this a lower-level function that just fills in a buffer
 // with random values, and then written those values from python.  But that's
 // about 20% slower in my tests, and since we typically generate random
@@ -185,6 +204,8 @@ static PyMethodDef faster_methods[] = {
        "Count the number of matching prefix bits between two strings." },
     { "firstword", firstword, METH_VARARGS,
         "Return an int corresponding to the first 32 bits of buf." },
+    { "extract_bits", extract_bits, METH_VARARGS,
+       "Take the first 'nbits' bits from 'buf' and return them as an int." },
     { "write_random", write_random, METH_VARARGS,
        "Write random bytes to the given file descriptor" },
     { "open_noatime", open_noatime, METH_VARARGS,
index 5ec94ebfa3f3c1f85705fc72e47ac24e723e3446..8ff5ac05fd3211599eb77e87134871d1d24533db 100644 (file)
@@ -192,12 +192,7 @@ class PackIdx:
         return int(self.fanout[255])
 
 
-def extract_bits(buf, nbits):
-    """Take the first 'nbits' bits from 'buf' and return them as an integer."""
-    mask = (1<<nbits) - 1
-    v = _helpers.firstword(buf)
-    v = (v >> (32-nbits)) & mask
-    return v
+extract_bits = _helpers.extract_bits
 
 
 class PackMidx: