Commits

Philip Jenvey committed 7ff6c62

add resizelist_hint

  • Participants
  • Parent commits 068c748
  • Branches length-hint

Comments (0)

Files changed (2)

File pypy/rlib/objectmodel.py

         hop.exception_is_here()
         return rtype_newlist(hop, v_sizehint=v)
 
+def resizelist_hint(l, sizehint):
+    """Reallocate the underlying list to the specified sizehint"""
+    return l
+
+class Entry(ExtRegistryEntry):
+    _about_ = resizelist_hint
+
+    def compute_result_annotation(self, s_l, s_sizehint):
+        from pypy.annotation.model import SomeInteger, SomeList
+        assert isinstance(s_l, SomeList)
+        assert isinstance(s_sizehint, SomeInteger)
+        s_l.listdef.listitem.resize()
+        return s_l
+
+    def specialize_call(self, hop, i_sizehint=None):
+        from pypy.rpython.lltypesystem.rlist import _ll_list_resize
+        v_list, v_sizehint = hop.inputargs(*hop.args_r)
+        hop.exception_is_here()
+        hop.llops.gendirectcall(_ll_list_resize, v_list, v_sizehint)
+        return v_list
+
 # ____________________________________________________________
 #
 # id-like functions.  The idea is that calling hash() or id() is not

File pypy/rlib/test/test_objectmodel.py

             break
     assert llop.args[2] is graph.startblock.inputargs[0]
     
+def test_resizelist_hint():
+    from pypy.annotation.model import SomeInteger
+    def f(z):
+        x = []
+        resizelist_hint(x, 39)
+        if z < 0:
+            x.append(1)
+        return len(x)
+
+    graph = getgraph(f, [SomeInteger()])
+    for llop in graph.startblock.operations:
+        if llop.opname == 'direct_call':
+            break
+    call_name = llop.args[0].value._obj.graph.name
+    call_arg2 = llop.args[2].value
+    assert call_name.startswith('_ll_list_resize_really')
+    assert call_arg2 == 39