[pypy-commit] pypy list-strategies: refactored switching to correct strategy when appending to EmptyList

l.diekmann noreply at buildbot.pypy.org
Fri Sep 23 13:15:33 CEST 2011


Author: Lukas Diekmann <lukas.diekmann at uni-duesseldorf.de>
Branch: list-strategies
Changeset: r47550:6d0334aa5967
Date: 2011-09-14 13:10 +0200
http://bitbucket.org/pypy/pypy/changeset/6d0334aa5967/

Log:	refactored switching to correct strategy when appending to EmptyList

diff --git a/pypy/objspace/std/listobject.py b/pypy/objspace/std/listobject.py
--- a/pypy/objspace/std/listobject.py
+++ b/pypy/objspace/std/listobject.py
@@ -281,12 +281,21 @@
     def getstorage_copy(self, w_list):
         return self.erase(None)
 
+    def switch_to_correct_strategy(self, w_list, w_item):
+        if is_W_IntObject(w_item):
+            strategy = self.space.fromcache(IntegerListStrategy)
+        elif is_W_StringObject(w_item):
+            strategy = self.space.fromcache(StringListStrategy)
+        else:
+            strategy = self.space.fromcache(ObjectListStrategy)
+
+        storage = strategy.get_empty_storage()
+        w_list.strategy = strategy
+        w_list.lstorage = storage
+
     def append(self, w_list, w_item):
-        # XXX this should be done by checking the type of the object directly
-        # here without going through __init__ and
-        # get_strategy_from_list_objects, because it is a very common path.
-        # Compare with EmptyDictStrategy.switch_to_correct_strategy
-        w_list.__init__(self.space, [w_item])
+        self.switch_to_correct_strategy(w_list, w_item)
+        w_list.append(w_item)
 
     def mul(self, w_list, times):
         return w_list.clone()
@@ -529,6 +538,9 @@
         l = [self.unwrap(w_item) for w_item in list_w]
         w_list.lstorage = self.erase(l)
 
+    def get_empty_storage(self):
+        return self.erase([])
+
     def clone(self, w_list):
         l = self.unerase(w_list.lstorage)
         storage = self.erase(l[:])


More information about the pypy-commit mailing list