[pypy-commit] pypy cpyext-avoid-roundtrip: (antocuni, arigo): hoorray: finally add an optimized special case to make_ref for integers, so that they are allocated from the free list

antocuni pypy.commits at gmail.com
Mon Oct 9 12:42:43 EDT 2017


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: cpyext-avoid-roundtrip
Changeset: r92687:79a097e55085
Date: 2017-10-09 18:35 +0200
http://bitbucket.org/pypy/pypy/changeset/79a097e55085/

Log:	(antocuni, arigo): hoorray: finally add an optimized special case to
	make_ref for integers, so that they are allocated from the free list

diff --git a/pypy/module/cpyext/pyobject.py b/pypy/module/cpyext/pyobject.py
--- a/pypy/module/cpyext/pyobject.py
+++ b/pypy/module/cpyext/pyobject.py
@@ -297,6 +297,12 @@
     """Increment the reference counter of the PyObject and return it.
     Can be called with either a PyObject or a W_Root.
     """
+    if not is_pyobj(obj):
+        w_obj = obj
+        if w_obj is not None and space.type(w_obj) is space.w_int:
+            state = space.fromcache(State)
+            intval = space.int_w(w_obj)
+            return state.C.PyInt_FromLong(intval)
     return get_pyobj_and_incref(space, obj, w_userdata, immortal=False)
 
 @specialize.ll()
diff --git a/pypy/module/cpyext/test/test_intobject.py b/pypy/module/cpyext/test/test_intobject.py
--- a/pypy/module/cpyext/test/test_intobject.py
+++ b/pypy/module/cpyext/test/test_intobject.py
@@ -50,6 +50,19 @@
         p_x = state.C.PyInt_FromLong(12345678)
         decref(space, p_x)
         p_y = state.C.PyInt_FromLong(87654321)
+        # check that the address is the same, i.e. that the freelist did its
+        # job
+        assert p_x == p_y
+        decref(space, p_y)
+
+    def test_freelist_make_ref(self, space):
+        w_x = space.newint(12345678)
+        w_y = space.newint(87654321)
+        p_x = make_ref(space, w_x)
+        decref(space, p_x)
+        p_y = make_ref(space, w_y)
+        # check that the address is the same: note that w_x does NOT keep p_x
+        # alive, because in make_ref we have a special case for ints
         assert p_x == p_y
         decref(space, p_y)
 


More information about the pypy-commit mailing list