Rev 4694: Get rid of the Keys depth parameter. in http://bazaar.launchpad.net/~jameinel/bzr/2.1-memory-consumption

John Arbash Meinel john at arbash-meinel.com
Thu Sep 10 18:28:26 BST 2009


At http://bazaar.launchpad.net/~jameinel/bzr/2.1-memory-consumption

------------------------------------------------------------
revno: 4694
revision-id: john at arbash-meinel.com-20090910172810-9zxjgnx54q1epk7d
parent: john at arbash-meinel.com-20090910172005-sujfxgfq84dar377
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.1-memory-consumption
timestamp: Thu 2009-09-10 12:28:10 -0500
message:
  Get rid of the Keys depth parameter.
  We will favor creating a different type (Key) as I think it will be
  quite a bit more obvious what is going on.
-------------- next part --------------
=== modified file 'bzrlib/_btree_serializer_pyx.pyx'
--- a/bzrlib/_btree_serializer_pyx.pyx	2009-09-10 17:20:05 +0000
+++ b/bzrlib/_btree_serializer_pyx.pyx	2009-09-10 17:28:10 +0000
@@ -253,7 +253,7 @@
                         # key runs to the end
                         temp_ptr = ref_ptr
                     ref_list.extend(self.extract_key(temp_ptr))
-                ref_list = _keys_type_c.Keys(self.key_length, 2, *ref_list)
+                ref_list = _keys_type_c.Keys(self.key_length, *ref_list)
                 PyList_Append(ref_lists, ref_list)
                 # prepare for the next reference list
                 self._start = next_start

=== modified file 'bzrlib/_keys_type_c.c'
--- a/bzrlib/_keys_type_c.c	2009-09-09 21:36:55 +0000
+++ b/bzrlib/_keys_type_c.c	2009-09-10 17:28:10 +0000
@@ -35,7 +35,6 @@
 typedef struct {
     PyObject_HEAD
     // unsigned char key_width;
-    // unsigned char depth;
     // unsigned char num_keys;
     // unsigned char flags; /* not used yet */
     unsigned int info; /* Broken down into 4 1-byte fields */
@@ -47,12 +46,11 @@
 static PyObject *Keys_item(Keys *self, Py_ssize_t offset);
 
 static inline void
-Keys_set_info(Keys *self, int key_width, int depth,
+Keys_set_info(Keys *self, int key_width,
                           int num_keys, int flags)
 {
     self->info = ((unsigned int)key_width)
                  | (((unsigned int) num_keys) << 8)
-                 | (((unsigned int) depth) << 16)
                  | (((unsigned int) flags) << 24);
 }
 
@@ -69,12 +67,6 @@
 }
 
 static inline int 
-Keys_get_depth(Keys *self)
-{
-    return (int)((self->info >> 16) & 0xFF);
-}
-
-static inline int 
 Keys_get_flags(Keys *self)
 {
     return (int)((self->info >> 24) & 0xFF);
@@ -108,7 +100,6 @@
     Py_ssize_t i;
     long key_width;
     long num_keys;
-    long depth;
     long num_key_bits;
     long flags = 0; /* Not used */
 	PyObject *obj= NULL;
@@ -124,8 +115,8 @@
         return NULL;
     }
     num_args = PyTuple_GET_SIZE(args);
-    if (num_args < 2) {
-        PyErr_SetString(PyExc_TypeError, "Keys.__init__(width, depth, ...)"
+    if (num_args < 1) {
+        PyErr_SetString(PyExc_TypeError, "Keys.__init__(width, ...)"
             " takes at least two arguments.");
         return NULL;
     }
@@ -134,36 +125,17 @@
         return NULL;
     }
     if (key_width <= 0) {
-        PyErr_SetString(PyExc_ValueError, "Keys.__init__(width, depth, ...)"
+        PyErr_SetString(PyExc_ValueError, "Keys.__init__(width, ...)"
             " width should be a positive integer.");
         return NULL;
     }
     if (key_width > 256) {
-        PyErr_SetString(PyExc_ValueError, "Keys.__init__(width, depth, ...)"
+        PyErr_SetString(PyExc_ValueError, "Keys.__init__(width, ...)"
             " width must be <= 256");
         return NULL;
     }
-    depth = PyInt_AsLong(PyTuple_GET_ITEM(args, 1));
-    if (depth == -1 && PyErr_Occurred()) {
-        return NULL;
-    }
-    if (depth <= 0) {
-        PyErr_SetString(PyExc_ValueError, "Keys.__init__(width, depth, ...)"
-            " depth must be > 0");
-        return NULL;
-    }
-    if (depth > 256) {
-        PyErr_SetString(PyExc_ValueError, "Keys.__init__(width, depth, ...)"
-            " depth must be <= 256");
-        return NULL;
-    }
-    if (depth != 2) {
-        PyErr_SetString(PyExc_ValueError, "Keys.__init__(width, depth, ...)"
-            " only depth == 2 currently supported");
-        return NULL;
-    }
     /* First arg is the key width, the rest are the actual key items */
-    num_key_bits = num_args - 2;
+    num_key_bits = num_args - 1;
     num_keys = num_key_bits / key_width;
     if (num_keys * key_width != num_key_bits) {
         PyErr_SetString(PyExc_ValueError, "Keys.__init__(width, ...), was"
@@ -178,11 +150,10 @@
     }
     self = (Keys *)(type->tp_alloc(type, num_key_bits));
     // self->key_width = (unsigned char)key_width;
-    // self->depth = (unsigned char)depth;
     // self->num_keys = (unsigned char)num_keys;
-    Keys_set_info(self, key_width, depth, num_keys, flags);
+    Keys_set_info(self, key_width, num_keys, flags);
     for (i = 0; i < num_key_bits; i++) {
-        obj = PyTuple_GET_ITEM(args, i + 2);
+        obj = PyTuple_GET_ITEM(args, i + 1);
         if (!PyString_CheckExact(obj)) {
             PyErr_SetString(PyExc_TypeError, "Keys.__init__(width, ...)"
                 " requires that all key bits are strings.");
@@ -197,7 +168,7 @@
 }
 
 static PyObject *
-Keys_as_tuples(Keys *self)
+Keys_as_tuple(Keys *self)
 {
     PyObject *as_tuple = NULL;
     PyObject *a_key = NULL;
@@ -218,22 +189,22 @@
     }
     return as_tuple;
 Err:
-    Py_DECREF(as_tuple);
+    Py_XDECREF(as_tuple);
     return NULL;
 }
 
 static long
 Keys_hash(Keys *self)
 {
-    PyObject *as_tuples = NULL;
+    PyObject *as_tuple = NULL;
     long hash = -1;
 
-    as_tuples = Keys_as_tuples(self);
-    if (as_tuples == NULL) {
+    as_tuple = Keys_as_tuple(self);
+    if (as_tuple == NULL) {
         return -1;
     }
-    hash = PyTuple_Type.tp_hash(as_tuples);
-    Py_DECREF(as_tuples);
+    hash = PyTuple_Type.tp_hash(as_tuple);
+    Py_DECREF(as_tuple);
     return hash;
 }
 
@@ -245,7 +216,7 @@
     PyObject *result;
     
     if (Keys_CheckExact(v)) {
-        vt = Keys_as_tuples((Keys *)v);
+        vt = Keys_as_tuple((Keys *)v);
         vt_to_decref = vt;
     } else if (PyTuple_Check(v)) {
         vt = v;
@@ -255,7 +226,7 @@
 		return Py_NotImplemented;
     }
     if (Keys_CheckExact(w)) {
-        wt = Keys_as_tuples((Keys *)w);
+        wt = Keys_as_tuple((Keys *)w);
         wt_to_decref = wt;
     } else if (PyTuple_Check(w)) {
         wt = w;
@@ -278,7 +249,7 @@
     PyObject *as_tpl;
     PyObject *result;
 
-    as_tpl = Keys_as_tuples(self);
+    as_tpl = Keys_as_tuple(self);
     if (as_tpl == NULL) {
         return NULL;
     }
@@ -335,7 +306,7 @@
 static char Keys_get_key_doc[] = "get_keys(offset)";
 
 static PyMethodDef Keys_methods[] = {
-    {"as_tuples", (PyCFunction)Keys_as_tuples, METH_VARARGS},
+    {"as_tuple", (PyCFunction)Keys_as_tuple, METH_VARARGS},
     {NULL, NULL} /* sentinel */
 };
 

=== modified file 'bzrlib/_keys_type_py.py'
--- a/bzrlib/_keys_type_py.py	2009-09-09 21:23:08 +0000
+++ b/bzrlib/_keys_type_py.py	2009-09-10 17:28:10 +0000
@@ -21,15 +21,11 @@
 """
 
 
-def Keys(width, depth, *args):
+def Keys(width, *args):
     if not isinstance(width, int):
         raise TypeError('width must be an integer.')
-    if not isinstance(depth, int):
-        raise TypeError('depth must be an integer.')
     if width <= 0 or width > 256:
         raise ValueError('width must be in the range 1 => 256')
-    if depth <= 0 or depth > 256:
-        raise ValueError('depth must be in the range 1 => 256')
     num_keys = len(args) // width
     if (num_keys * width != len(args)):
         raise ValueError('number of entries not a multiple of width')

=== modified file 'bzrlib/tests/test__keys_type.py'
--- a/bzrlib/tests/test__keys_type.py	2009-09-09 21:23:08 +0000
+++ b/bzrlib/tests/test__keys_type.py	2009-09-10 17:28:10 +0000
@@ -62,36 +62,33 @@
 class TestKeysType(tests.TestCase):
 
     def test_create(self):
-        #k = self.module.Keys(1, 1, 'foo')
-        k = self.module.Keys(1, 2, 'foo', 'bar')
-        #k = self.module.Keys(2, 1, 'foo', 'bar')
+        k = self.module.Keys(1, 'foo', 'bar')
+        k = self.module.Keys(2, 'foo', 'bar')
 
     def test_create_bad_args(self):
         self.assertRaises(TypeError, self.module.Keys)
         self.assertRaises(TypeError, self.module.Keys, 'foo')
-        self.assertRaises(TypeError, self.module.Keys, 0)
-        self.assertRaises(ValueError, self.module.Keys, 0, 2)
-        self.assertRaises(ValueError, self.module.Keys, 1, 0)
-        self.assertRaises(ValueError, self.module.Keys, -1, 2)
-        self.assertRaises(ValueError, self.module.Keys, -200, 2)
-        self.assertRaises(ValueError, self.module.Keys, 2, 2, 'foo')
-        self.assertRaises(ValueError, self.module.Keys, 257, 2)
+        self.assertRaises(ValueError, self.module.Keys, 0)
+        self.assertRaises(ValueError, self.module.Keys, -1)
+        self.assertRaises(ValueError, self.module.Keys, -200)
+        self.assertRaises(ValueError, self.module.Keys, 2, 'foo')
+        self.assertRaises(ValueError, self.module.Keys, 257)
         lots_of_args = ['a']*300
         # too many args
-        self.assertRaises(ValueError, self.module.Keys, 1, 2, *lots_of_args)
-        self.assertRaises(TypeError, self.module.Keys, 1, 2, 'foo', 10)
+        self.assertRaises(ValueError, self.module.Keys, 1, *lots_of_args)
+        self.assertRaises(TypeError, self.module.Keys, 1, 'foo', 10)
 
     def test_create_and_del_correct_refcount(self):
         s = 'my custom' + ' foo bar'
         n_ref = sys.getrefcount(s)
-        k = self.module.Keys(1, 2, s)
+        k = self.module.Keys(1, s)
         self.assertEqual(n_ref + 1, sys.getrefcount(s))
         del k
         self.assertEqual(n_ref, sys.getrefcount(s))
 
     def test_get_item(self):
         f = 'fo' + 'o'
-        k = self.module.Keys(1, 2, f, 'bar')
+        k = self.module.Keys(1, f, 'bar')
         self.assertEqual(('foo',), k[0])
         self.assertEqual(('bar',), k[1])
         self.assertRaises(IndexError, k.__getitem__, 2)
@@ -107,29 +104,29 @@
         self.assertEqual(2, len(k))
 
     def test_get_wide_key(self):
-        k = self.module.Keys(2, 2, 'foo', 'bar', 'baz', 'bing')
+        k = self.module.Keys(2, 'foo', 'bar', 'baz', 'bing')
         self.assertEqual(('foo', 'bar'), k[0])
         self.assertEqual(('baz', 'bing'), k[1])
         self.assertRaises(IndexError, k.__getitem__, 2)
         self.assertEqual(2, len(k))
 
     def test_as_tuple(self):
-        k = self.module.Keys(2, 2, 'foo', 'bar', 'baz', 'bing')
-        if getattr(k, 'as_tuples', None) is not None:
-            t = k.as_tuples()
+        k = self.module.Keys(2, 'foo', 'bar', 'baz', 'bing')
+        if getattr(k, 'as_tuple', None) is not None:
+            t = k.as_tuple()
         else:
             t = k # The pure-python form is in tuples already
         self.assertEqual((('foo', 'bar'), ('baz', 'bing')), t)
 
     def test_repr(self):
-        k = self.module.Keys(2, 2, 'foo', 'bar', 'baz', 'bing')
+        k = self.module.Keys(2, 'foo', 'bar', 'baz', 'bing')
         self.assertEqual("(('foo', 'bar'), ('baz', 'bing'))", repr(k))
 
     def test_compare(self):
-        k1 = self.module.Keys(2, 2, 'foo', 'bar')
-        k2 = self.module.Keys(2, 2, 'baz', 'bing')
-        k3 = self.module.Keys(2, 2, 'foo', 'zzz')
-        k4 = self.module.Keys(2, 2, 'foo', 'bar')
+        k1 = self.module.Keys(2, 'foo', 'bar')
+        k2 = self.module.Keys(2, 'baz', 'bing')
+        k3 = self.module.Keys(2, 'foo', 'zzz')
+        k4 = self.module.Keys(2, 'foo', 'bar')
         # Comparison should be done on the keys themselves, and not based on
         # object id, etc.
         self.assertTrue(k1 == k1)
@@ -142,18 +139,18 @@
         self.assertTrue(k1 == (('foo', 'bar'),))
 
     def test_sorted(self):
-        k1 = self.module.Keys(2, 2, 'foo', 'bar', 'baz', 'bing', 'foo', 'zzz')
+        k1 = self.module.Keys(2, 'foo', 'bar', 'baz', 'bing', 'foo', 'zzz')
         self.assertEqual([('baz', 'bing'), ('foo', 'bar'), ('foo', 'zzz')],
                          sorted(k1))
 
-        k1 = self.module.Keys(2, 2, 'foo', 'bar')
-        k2 = self.module.Keys(2, 2, 'baz', 'bing')
-        k3 = self.module.Keys(2, 2, 'foo', 'zzz')
+        k1 = self.module.Keys(2, 'foo', 'bar')
+        k2 = self.module.Keys(2, 'baz', 'bing')
+        k3 = self.module.Keys(2, 'foo', 'zzz')
         self.assertEqual([(('baz', 'bing'),), (('foo', 'bar'),),
                           (('foo', 'zzz'),)], sorted([k1, k2, k3]))
 
     def test_hash(self):
-        k1 = self.module.Keys(2, 2, 'foo', 'bar', 'baz', 'bing', 'foo', 'zzz')
+        k1 = self.module.Keys(2, 'foo', 'bar', 'baz', 'bing', 'foo', 'zzz')
         as_tuple =(('foo', 'bar'), ('baz', 'bing'), ('foo', 'zzz')) 
         self.assertEqual(hash(k1), hash(as_tuple))
         x = {k1: 'foo'}



More information about the bazaar-commits mailing list