[pypy-svn] r49389 - in pypy/dist/pypy/rlib: . test

fijal at codespeak.net fijal at codespeak.net
Wed Dec 5 15:52:56 CET 2007


Author: fijal
Date: Wed Dec  5 15:52:55 2007
New Revision: 49389

Modified:
   pypy/dist/pypy/rlib/objectmodel.py
   pypy/dist/pypy/rlib/test/test_objectmodel.py
Log:
A special hint that allows one to specify expected size of a list.


Modified: pypy/dist/pypy/rlib/objectmodel.py
==============================================================================
--- pypy/dist/pypy/rlib/objectmodel.py	(original)
+++ pypy/dist/pypy/rlib/objectmodel.py	Wed Dec  5 15:52:55 2007
@@ -350,3 +350,24 @@
         self.key = key
         self.hash = hash
 
+# ------------------------- optimization hints ------------------------------
+
+def newlist(sizehint=0):
+    return []
+
+class Entry(ExtRegistryEntry):
+    _about_ = newlist
+
+    def compute_result_annotation(self, s_sizehint):
+        return self.bookkeeper.newlist()
+
+    def specialize_call(self, orig_hop, i_sizehint):
+        from pypy.rpython.rlist import rtype_newlist
+        from pypy.rpython.lltypesystem import lltype
+        # fish a bit hop
+        hop = orig_hop.copy()
+        v = hop.args_v[0]
+        r, s = hop.r_s_popfirstarg()
+        if s.is_constant():
+            v = hop.inputconst(r, s.const)
+        return rtype_newlist(hop, v_sizehint=v)

Modified: pypy/dist/pypy/rlib/test/test_objectmodel.py
==============================================================================
--- pypy/dist/pypy/rlib/test/test_objectmodel.py	(original)
+++ pypy/dist/pypy/rlib/test/test_objectmodel.py	Wed Dec  5 15:52:55 2007
@@ -2,6 +2,7 @@
 from pypy.rlib.objectmodel import *
 from pypy.translator.translator import TranslationContext, graphof
 from pypy.rpython.test.tool import BaseRtypingTest, LLRtypeMixin, OORtypeMixin
+from pypy.conftest import option
 
 def strange_key_eq(key1, key2):
     return key1[0] == key2[0]   # only the 1st character is relevant
@@ -302,3 +303,46 @@
     specialize.arg(1)(f)
 
     assert f._annspecialcase_ == 'specialize:arg(1)'
+
+def getgraph(f, argtypes):
+    from pypy.translator.translator import TranslationContext, graphof
+    from pypy.translator.backendopt.all import backend_optimizations
+    t = TranslationContext()
+    a = t.buildannotator()
+    typer = t.buildrtyper()
+    a.build_types(f, argtypes)
+    typer.specialize()
+    backend_optimizations(t)
+    graph = graphof(t, f)
+    if option.view:
+        graph.show()
+    return graph
+
+def test_newlist():
+    from pypy.annotation.model import SomeInteger
+    def f(z):
+        x = newlist(sizehint=38)
+        if z < 0:
+            x.append(1)
+        return len(x)
+
+    graph = getgraph(f, [SomeInteger()])
+    for llop in graph.startblock.operations:
+        if llop.opname == 'malloc_varsize':
+            break
+    assert llop.args[2].value == 38
+
+def test_newlist_nonconst():
+    from pypy.annotation.model import SomeInteger
+    from pypy.objspace.flow.model import Variable
+    def f(z):
+        x = newlist(sizehint=z)
+        return len(x)
+
+    graph = getgraph(f, [SomeInteger()])
+    for llop in graph.startblock.operations:
+        if llop.opname == 'malloc_varsize':
+            break
+    assert llop.args[2] is graph.startblock.inputargs[0]
+
+    


More information about the pypy-svn mailing list