[pypy-commit] pypy set-strategies: added fastpath for initialization of lists with iterables using int- or stringstrategy
l.diekmann
noreply at buildbot.pypy.org
Wed Jan 11 14:10:01 CET 2012
Author: Lukas Diekmann <lukas.diekmann at uni-duesseldorf.de>
Branch: set-strategies
Changeset: r51221:29acb5e48ac9
Date: 2012-01-11 14:09 +0100
http://bitbucket.org/pypy/pypy/changeset/29acb5e48ac9/
Log: added fastpath for initialization of lists with iterables using int-
or stringstrategy
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
@@ -1042,6 +1042,21 @@
elif isinstance(w_iterable, W_TupleObject):
W_ListObject(space, w_iterable.wrappeditems[:]).copy_into(w_list)
return
+
+ intlist = space.listview_int(w_iterable)
+ if intlist is not None:
+ w_list.strategy = strategy = space.fromcache(IntegerListStrategy)
+ # need to copy because intlist can share with w_iterable
+ w_list.lstorage = strategy.erase(intlist[:])
+ return
+
+ strlist = space.listview_str(w_iterable)
+ if strlist is not None:
+ w_list.strategy = strategy = space.fromcache(StringListStrategy)
+ # need to copy because intlist can share with w_iterable
+ w_list.lstorage = strategy.erase(strlist[:])
+ return
+
w_list.__init__(space, [])
# xxx special hack for speed
from pypy.interpreter.generator import GeneratorIterator
diff --git a/pypy/objspace/std/test/test_liststrategies.py b/pypy/objspace/std/test/test_liststrategies.py
--- a/pypy/objspace/std/test/test_liststrategies.py
+++ b/pypy/objspace/std/test/test_liststrategies.py
@@ -463,6 +463,34 @@
w_res = listobject.list_pop__List_ANY(space, w_l, space.w_None) # does not crash
assert space.unwrap(w_res) == 3
+ def test_create_list_from_set(self):
+ from pypy.objspace.std.setobject import W_SetObject
+ from pypy.objspace.std.setobject import _initialize_set
+
+ space = self.space
+ w = space.wrap
+
+ w_l = W_ListObject(space, [space.wrap(1), space.wrap(2), space.wrap(3)])
+
+ w_set = W_SetObject(self.space)
+ _initialize_set(self.space, w_set, w_l)
+ w_set.iter = None # make sure fast path is used
+
+ w_l2 = W_ListObject(space, [])
+ space.call_method(w_l2, "__init__", w_set)
+
+ w_l2.sort(False)
+ assert space.eq_w(w_l, w_l2)
+
+ w_l = W_ListObject(space, [space.wrap("a"), space.wrap("b"), space.wrap("c")])
+ _initialize_set(self.space, w_set, w_l)
+
+ space.call_method(w_l2, "__init__", w_set)
+
+ w_l2.sort(False)
+ assert space.eq_w(w_l, w_l2)
+
+
class TestW_ListStrategiesDisabled:
def setup_class(cls):
More information about the pypy-commit
mailing list