Rev 5377: Make _LeafNode inherit from dict (is-a rather than have-a) in http://bazaar.launchpad.net/~jameinel/bzr/2.3-btree-chk-leaf

John Arbash Meinel john at arbash-meinel.com
Wed Aug 4 05:36:30 BST 2010


At http://bazaar.launchpad.net/~jameinel/bzr/2.3-btree-chk-leaf

------------------------------------------------------------
revno: 5377
revision-id: john at arbash-meinel.com-20100804043626-pwawyjkb6r6wzs9a
parent: john at arbash-meinel.com-20100804043324-1ldc2v2w1kza7ox4
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.3-btree-chk-leaf
timestamp: Tue 2010-08-03 23:36:26 -0500
message:
  Make _LeafNode inherit from dict (is-a rather than have-a)
  
  It avoids having another object layer, and indirection via attribute
  and forwarding functions.
  
  TIMEIT shows:
    14.6us raw dict
    26.6us subclass
    57.9us wrapper class
-------------- next part --------------
=== modified file 'bzrlib/btree_index.py'
--- a/bzrlib/btree_index.py	2010-08-03 20:56:39 +0000
+++ b/bzrlib/btree_index.py	2010-08-04 04:36:26 +0000
@@ -602,10 +602,10 @@
         """In memory index's have no known corruption at the moment."""
 
 
-class _LeafNode(object):
+class _LeafNode(dict):
     """A leaf node for a serialised B+Tree index."""
 
-    __slots__ = ('_keys', 'min_key', 'max_key')
+    __slots__ = ('min_key', 'max_key')
 
     def __init__(self, bytes, key_length, ref_list_length):
         """Parse bytes to create a leaf node object."""
@@ -617,26 +617,17 @@
             self.max_key = key_list[-1][0]
         else:
             self.min_key = self.max_key = None
-        self._keys = dict(key_list)
-
-    def __len__(self):
-        return len(self._keys)
-
-    def __contains__(self, key):
-        return key in self._keys
-
-    def __getitem__(self, key):
-        return self._keys[key]
+        super(_LeafNode, self).__init__(key_list)
 
     def all_items(self):
         """Return a sorted list of (key, (value, refs)) items"""
-        items = self._keys.items()
+        items = self.items()
         items.sort()
         return items
 
     def all_keys(self):
         """Return a sorted list of all keys."""
-        keys = self._keys.keys()
+        keys = self.keys()
         keys.sort()
         return keys
 



More information about the bazaar-commits mailing list