Rev 4684: Have a pure-python implementation that works as tuples of tuples of strings, in http://bazaar.launchpad.net/~jameinel/bzr/2.1-memory-consumption

John Arbash Meinel john at arbash-meinel.com
Tue Sep 8 22:35:15 BST 2009


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

------------------------------------------------------------
revno: 4684
revision-id: john at arbash-meinel.com-20090908213501-njweb8wujinok4ty
parent: john at arbash-meinel.com-20090908212324-qk7t0b3vtkio8bmg
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.1-memory-consumption
timestamp: Tue 2009-09-08 16:35:01 -0500
message:
  Have a pure-python implementation that works as tuples of tuples of strings,
  which is what we have today. There is a bit of extra overhead, as we now do type checking, etc.
  We may want to make the type checking optional...
-------------- next part --------------
=== modified file 'bzrlib/_keys_type_c.c'
--- a/bzrlib/_keys_type_c.c	2009-09-08 21:23:24 +0000
+++ b/bzrlib/_keys_type_c.c	2009-09-08 21:35:01 +0000
@@ -13,7 +13,7 @@
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-*/
+ */
 
 #include <Python.h>
 
@@ -67,11 +67,12 @@
     Keys *self;
 
 	if (type != &KeysType) {
+        PyErr_SetString(PyExc_TypeError, "we only support creating Keys");
         PyErr_BadInternalCall();
         return NULL;
     }
     if (!PyTuple_CheckExact(args)) {
-        PyErr_BadInternalCall();
+        PyErr_SetString(PyExc_TypeError, "args must be a tuple");
         return NULL;
     }
     num_args = PyTuple_GET_SIZE(args);

=== added file 'bzrlib/_keys_type_py.py'
--- a/bzrlib/_keys_type_py.py	1970-01-01 00:00:00 +0000
+++ b/bzrlib/_keys_type_py.py	2009-09-08 21:35:01 +0000
@@ -0,0 +1,42 @@
+# Copyright (C) 2009 Canonical Ltd
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+"""The pure-python implementation of the Keys type.
+
+Note that it is generally just implemented as using tuples of tuples of
+strings.
+"""
+
+
+def Keys(width, *args):
+    if not isinstance(width, int):
+        raise TypeError('width must be an integer.')
+    if width <= 0 or width > 256:
+        raise ValueError('width 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')
+    if num_keys > 256:
+        raise ValueError('too many keys [must be <= 256 keys]')
+    result = []
+    for i in xrange(0, num_keys):
+        start = i*width
+        key = args[start:start+width]
+        for bit in key:
+            if not isinstance(bit, str):
+                raise TypeError('key bits must be strings')
+        result.append(key)
+    return tuple(result)

=== modified file 'bzrlib/tests/test__keys_type.py'
--- a/bzrlib/tests/test__keys_type.py	2009-09-08 21:23:24 +0000
+++ b/bzrlib/tests/test__keys_type.py	2009-09-08 21:35:01 +0000
@@ -19,7 +19,7 @@
 import sys
 
 from bzrlib import (
-    # _keys_py,
+    _keys_type_py,
     errors,
     tests,
     )
@@ -28,7 +28,7 @@
 def load_tests(standard_tests, module, loader):
     """Parameterize tests for all versions of groupcompress."""
     scenarios = [
-    #    ('python', {'module': _keys_py}),
+        ('python', {'module': _keys_type_py}),
     ]
     suite = loader.suiteClass()
     if CompiledKeysType.available():
@@ -93,8 +93,12 @@
         self.assertRaises(IndexError, k.__getitem__, 2)
         n_refs = sys.getrefcount(f)
         f_key = k[0]
-        self.assertEqual(n_refs + 1, sys.getrefcount(f))
+        # The pure-python version returns a tuple it already created, rather
+        # than creating a new one, so the refcount doesn't change
+        self.assertTrue(n_refs + 1 >= sys.getrefcount(f) >= n_refs)
         del f_key
+        # This is the important check, that the final refcount should be
+        # unchanged
         self.assertEqual(n_refs, sys.getrefcount(f))
         self.assertEqual(2, len(k))
 



More information about the bazaar-commits mailing list