From a0bf9b85618bcb7c8187c35848e2501b2c2d1bbf Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Sat, 4 Apr 2026 10:46:17 +0530 Subject: [PATCH 1/2] relax GUARD_CALLABLE checks for self type checks --- Python/optimizer_bytecodes.c | 44 ++++++++++++++++++++---------------- Python/optimizer_cases.c.h | 44 ++++++++++++++++++++---------------- 2 files changed, 48 insertions(+), 40 deletions(-) diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c index 52a320fc70f7ed..854efc1ff0135f 100644 --- a/Python/optimizer_bytecodes.c +++ b/Python/optimizer_bytecodes.c @@ -1326,16 +1326,17 @@ dummy_func(void) { if (!sym_is_null(self_or_null)) { total_args++; } - PyObject *self = NULL; + PyTypeObject *self_type = NULL; if (!sym_is_null(self_or_null)) { - self = sym_get_const(ctx, self_or_null); - } else { - self = sym_get_const(ctx, args[0]); + self_type = sym_get_type(self_or_null); + } + else { + self_type = sym_get_type(args[0]); } PyTypeObject *d_type = ((PyMethodDescrObject *)callable_o)->d_common.d_type; if (total_args == 2 && ((PyMethodDescrObject *)callable_o)->d_method->ml_flags == METH_O && - self && Py_IS_TYPE(self, d_type)) { + self_type == d_type) { ADD_OP(_NOP, 0, 0); } } @@ -1351,16 +1352,17 @@ dummy_func(void) { if (!sym_is_null(self_or_null)) { total_args++; } - PyObject *self = NULL; + PyTypeObject *self_type = NULL; if (!sym_is_null(self_or_null)) { - self = sym_get_const(ctx, self_or_null); - } else { - self = sym_get_const(ctx, args[0]); + self_type = sym_get_type(self_or_null); + } + else { + self_type = sym_get_type(args[0]); } PyTypeObject *d_type = ((PyMethodDescrObject *)callable_o)->d_common.d_type; if (total_args != 0 && ((PyMethodDescrObject *)callable_o)->d_method->ml_flags == (METH_FASTCALL|METH_KEYWORDS) && - self && Py_IS_TYPE(self, d_type)) { + self_type == d_type) { ADD_OP(_NOP, 0, 0); } } @@ -1376,16 +1378,17 @@ dummy_func(void) { if (!sym_is_null(self_or_null)) { total_args++; } - PyObject *self = NULL; + PyTypeObject *self_type = NULL; if (!sym_is_null(self_or_null)) { - self = sym_get_const(ctx, self_or_null); - } else { - self = sym_get_const(ctx, args[0]); + self_type = sym_get_type(self_or_null); + } + else { + self_type = sym_get_type(args[0]); } PyTypeObject *d_type = ((PyMethodDescrObject *)callable_o)->d_common.d_type; if (total_args == 1 && ((PyMethodDescrObject *)callable_o)->d_method->ml_flags == METH_NOARGS && - self && Py_IS_TYPE(self, d_type)) { + self_type == d_type) { ADD_OP(_NOP, 0, 0); } } @@ -1434,16 +1437,17 @@ dummy_func(void) { if (!sym_is_null(self_or_null)) { total_args++; } - PyObject *self = NULL; + PyTypeObject *self_type = NULL; if (!sym_is_null(self_or_null)) { - self = sym_get_const(ctx, self_or_null); - } else { - self = sym_get_const(ctx, args[0]); + self_type = sym_get_type(self_or_null); + } + else { + self_type = sym_get_type(args[0]); } PyTypeObject *d_type = ((PyMethodDescrObject *)callable_o)->d_common.d_type; if (total_args != 0 && ((PyMethodDescrObject *)callable_o)->d_method->ml_flags == METH_FASTCALL && - self && Py_IS_TYPE(self, d_type)) { + self_type == d_type) { ADD_OP(_NOP, 0, 0); } } diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h index 52ca6b2531e3b4..2f9fd6ced09126 100644 --- a/Python/optimizer_cases.c.h +++ b/Python/optimizer_cases.c.h @@ -4077,16 +4077,17 @@ if (!sym_is_null(self_or_null)) { total_args++; } - PyObject *self = NULL; + PyTypeObject *self_type = NULL; if (!sym_is_null(self_or_null)) { - self = sym_get_const(ctx, self_or_null); - } else { - self = sym_get_const(ctx, args[0]); + self_type = sym_get_type(self_or_null); + } + else { + self_type = sym_get_type(args[0]); } PyTypeObject *d_type = ((PyMethodDescrObject *)callable_o)->d_common.d_type; if (total_args == 2 && ((PyMethodDescrObject *)callable_o)->d_method->ml_flags == METH_O && - self && Py_IS_TYPE(self, d_type)) { + self_type == d_type) { ADD_OP(_NOP, 0, 0); } } @@ -4167,16 +4168,17 @@ if (!sym_is_null(self_or_null)) { total_args++; } - PyObject *self = NULL; + PyTypeObject *self_type = NULL; if (!sym_is_null(self_or_null)) { - self = sym_get_const(ctx, self_or_null); - } else { - self = sym_get_const(ctx, args[0]); + self_type = sym_get_type(self_or_null); + } + else { + self_type = sym_get_type(args[0]); } PyTypeObject *d_type = ((PyMethodDescrObject *)callable_o)->d_common.d_type; if (total_args != 0 && ((PyMethodDescrObject *)callable_o)->d_method->ml_flags == (METH_FASTCALL|METH_KEYWORDS) && - self && Py_IS_TYPE(self, d_type)) { + self_type == d_type) { ADD_OP(_NOP, 0, 0); } } @@ -4230,16 +4232,17 @@ if (!sym_is_null(self_or_null)) { total_args++; } - PyObject *self = NULL; + PyTypeObject *self_type = NULL; if (!sym_is_null(self_or_null)) { - self = sym_get_const(ctx, self_or_null); - } else { - self = sym_get_const(ctx, args[0]); + self_type = sym_get_type(self_or_null); + } + else { + self_type = sym_get_type(args[0]); } PyTypeObject *d_type = ((PyMethodDescrObject *)callable_o)->d_common.d_type; if (total_args == 1 && ((PyMethodDescrObject *)callable_o)->d_method->ml_flags == METH_NOARGS && - self && Py_IS_TYPE(self, d_type)) { + self_type == d_type) { ADD_OP(_NOP, 0, 0); } } @@ -4293,16 +4296,17 @@ if (!sym_is_null(self_or_null)) { total_args++; } - PyObject *self = NULL; + PyTypeObject *self_type = NULL; if (!sym_is_null(self_or_null)) { - self = sym_get_const(ctx, self_or_null); - } else { - self = sym_get_const(ctx, args[0]); + self_type = sym_get_type(self_or_null); + } + else { + self_type = sym_get_type(args[0]); } PyTypeObject *d_type = ((PyMethodDescrObject *)callable_o)->d_common.d_type; if (total_args != 0 && ((PyMethodDescrObject *)callable_o)->d_method->ml_flags == METH_FASTCALL && - self && Py_IS_TYPE(self, d_type)) { + self_type == d_type) { ADD_OP(_NOP, 0, 0); } } From 4dcaa4fc1a3a0a8bebb821a1c085ac3e14ef486a Mon Sep 17 00:00:00 2001 From: Kumar Aditya Date: Sat, 4 Apr 2026 10:49:48 +0530 Subject: [PATCH 2/2] use sym_is_not_null --- Python/optimizer_bytecodes.c | 16 ++++++++-------- Python/optimizer_cases.c.h | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c index 854efc1ff0135f..86efb74c6d9d7f 100644 --- a/Python/optimizer_bytecodes.c +++ b/Python/optimizer_bytecodes.c @@ -1323,11 +1323,11 @@ dummy_func(void) { PyObject *callable_o = sym_get_const(ctx, callable); if (callable_o && sym_matches_type(callable, &PyMethodDescr_Type)) { int total_args = oparg; - if (!sym_is_null(self_or_null)) { + if (sym_is_not_null(self_or_null)) { total_args++; } PyTypeObject *self_type = NULL; - if (!sym_is_null(self_or_null)) { + if (sym_is_not_null(self_or_null)) { self_type = sym_get_type(self_or_null); } else { @@ -1349,11 +1349,11 @@ dummy_func(void) { PyObject *callable_o = sym_get_const(ctx, callable); if (callable_o && sym_matches_type(callable, &PyMethodDescr_Type)) { int total_args = oparg; - if (!sym_is_null(self_or_null)) { + if (sym_is_not_null(self_or_null)) { total_args++; } PyTypeObject *self_type = NULL; - if (!sym_is_null(self_or_null)) { + if (sym_is_not_null(self_or_null)) { self_type = sym_get_type(self_or_null); } else { @@ -1375,11 +1375,11 @@ dummy_func(void) { PyObject *callable_o = sym_get_const(ctx, callable); if (callable_o && sym_matches_type(callable, &PyMethodDescr_Type)) { int total_args = oparg; - if (!sym_is_null(self_or_null)) { + if (sym_is_not_null(self_or_null)) { total_args++; } PyTypeObject *self_type = NULL; - if (!sym_is_null(self_or_null)) { + if (sym_is_not_null(self_or_null)) { self_type = sym_get_type(self_or_null); } else { @@ -1434,11 +1434,11 @@ dummy_func(void) { PyObject *callable_o = sym_get_const(ctx, callable); if (callable_o && sym_matches_type(callable, &PyMethodDescr_Type)) { int total_args = oparg; - if (!sym_is_null(self_or_null)) { + if (sym_is_not_null(self_or_null)) { total_args++; } PyTypeObject *self_type = NULL; - if (!sym_is_null(self_or_null)) { + if (sym_is_not_null(self_or_null)) { self_type = sym_get_type(self_or_null); } else { diff --git a/Python/optimizer_cases.c.h b/Python/optimizer_cases.c.h index 2f9fd6ced09126..a8be9bbd994c52 100644 --- a/Python/optimizer_cases.c.h +++ b/Python/optimizer_cases.c.h @@ -4074,11 +4074,11 @@ PyObject *callable_o = sym_get_const(ctx, callable); if (callable_o && sym_matches_type(callable, &PyMethodDescr_Type)) { int total_args = oparg; - if (!sym_is_null(self_or_null)) { + if (sym_is_not_null(self_or_null)) { total_args++; } PyTypeObject *self_type = NULL; - if (!sym_is_null(self_or_null)) { + if (sym_is_not_null(self_or_null)) { self_type = sym_get_type(self_or_null); } else { @@ -4165,11 +4165,11 @@ PyObject *callable_o = sym_get_const(ctx, callable); if (callable_o && sym_matches_type(callable, &PyMethodDescr_Type)) { int total_args = oparg; - if (!sym_is_null(self_or_null)) { + if (sym_is_not_null(self_or_null)) { total_args++; } PyTypeObject *self_type = NULL; - if (!sym_is_null(self_or_null)) { + if (sym_is_not_null(self_or_null)) { self_type = sym_get_type(self_or_null); } else { @@ -4229,11 +4229,11 @@ PyObject *callable_o = sym_get_const(ctx, callable); if (callable_o && sym_matches_type(callable, &PyMethodDescr_Type)) { int total_args = oparg; - if (!sym_is_null(self_or_null)) { + if (sym_is_not_null(self_or_null)) { total_args++; } PyTypeObject *self_type = NULL; - if (!sym_is_null(self_or_null)) { + if (sym_is_not_null(self_or_null)) { self_type = sym_get_type(self_or_null); } else { @@ -4293,11 +4293,11 @@ PyObject *callable_o = sym_get_const(ctx, callable); if (callable_o && sym_matches_type(callable, &PyMethodDescr_Type)) { int total_args = oparg; - if (!sym_is_null(self_or_null)) { + if (sym_is_not_null(self_or_null)) { total_args++; } PyTypeObject *self_type = NULL; - if (!sym_is_null(self_or_null)) { + if (sym_is_not_null(self_or_null)) { self_type = sym_get_type(self_or_null); } else {