Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -1872,6 +1872,33 @@ def for_iter_generator():
self.assert_specialized(for_iter_generator, "FOR_ITER_GEN")
self.assert_no_opcode(for_iter_generator, "FOR_ITER")

@cpython_only
@requires_specialization_ft
def test_call_list_append(self):
# gh-141367: only exact lists should use
# CALL_LIST_APPEND instruction after specialization.

r = range(_testinternalcapi.SPECIALIZATION_THRESHOLD)

def list_append(l):
for _ in r:
l.append(1)

list_append([])
self.assert_specialized(list_append, "CALL_LIST_APPEND")
self.assert_no_opcode(list_append, "CALL_METHOD_DESCRIPTOR_O")
self.assert_no_opcode(list_append, "CALL")

def my_list_append(l):
for _ in r:
l.append(1)

class MyList(list): pass
my_list_append(MyList())
self.assert_specialized(my_list_append, "CALL_METHOD_DESCRIPTOR_O")
self.assert_no_opcode(my_list_append, "CALL_LIST_APPEND")
self.assert_no_opcode(my_list_append, "CALL")


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Use ``CALL_LIST_APPEND`` instruction only for lists, not for list
subclasses, to avoid unnecessary deopt. Patch by Mikhail Efimov.
10 changes: 8 additions & 2 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "pycore_pylifecycle.h" // _PyOS_URandomNonblock()
#include "pycore_runtime.h" // _Py_ID()
#include "pycore_unicodeobject.h" // _PyUnicodeASCIIIter_Type
#include "pycore_pystate.h" // _PyThreadState_GET()

#include <stdlib.h> // rand()

Expand Down Expand Up @@ -1627,8 +1628,13 @@ specialize_method_descriptor(PyMethodDescrObject *descr, _Py_CODEUNIT *instr,
bool pop = (next.op.code == POP_TOP);
int oparg = instr->op.arg;
if ((PyObject *)descr == list_append && oparg == 1 && pop) {
specialize(instr, CALL_LIST_APPEND);
return 0;
PyThreadState *tstate = _PyThreadState_GET();
_PyStackRef *stack_pointer = tstate->current_frame->stackpointer;
PyObject *self = PyStackRef_AsPyObjectBorrow(stack_pointer[-2]);
if (PyList_CheckExact(self)) {
specialize(instr, CALL_LIST_APPEND);
return 0;
}
}
specialize(instr, CALL_METHOD_DESCRIPTOR_O);
return 0;
Expand Down
Loading