From 13642ce6b89685d5bbbb8af94ef82fa371e4086c Mon Sep 17 00:00:00 2001 From: Ken Jin Date: Sat, 4 Jun 2022 00:41:18 +0800 Subject: [PATCH 1/7] gh-93382: Cache result of `PyCode_GetCode` in codeobject (GH-93383) Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Co-authored-by: Dennis Sweeney <36520290+sweeneyde@users.noreply.github.com> --- Include/cpython/code.h | 1 + ...2-05-31-16-36-30.gh-issue-93382.Jf6gAj.rst | 1 + Objects/codeobject.c | 7 ++++ Programs/test_frozenmain.h | 42 ------------------- 4 files changed, 9 insertions(+), 42 deletions(-) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2022-05-31-16-36-30.gh-issue-93382.Jf6gAj.rst diff --git a/Include/cpython/code.h b/Include/cpython/code.h index ba7324b48d8675..f544ea87406e25 100644 --- a/Include/cpython/code.h +++ b/Include/cpython/code.h @@ -88,6 +88,7 @@ typedef uint16_t _Py_CODEUNIT; PyObject *co_qualname; /* unicode (qualname, for reference) */ \ PyObject *co_linetable; /* bytes object that holds location info */ \ PyObject *co_weakreflist; /* to support weakrefs to code objects */ \ + void *_co_code; /* cached co_code object/attribute */ \ /* Scratch space for extra data relating to the code object. \ Type is a void* to keep the format private in codeobject.c to force \ people to go through the proper APIs. */ \ diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-05-31-16-36-30.gh-issue-93382.Jf6gAj.rst b/Misc/NEWS.d/next/Core and Builtins/2022-05-31-16-36-30.gh-issue-93382.Jf6gAj.rst new file mode 100644 index 00000000000000..04b9e8ba481874 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2022-05-31-16-36-30.gh-issue-93382.Jf6gAj.rst @@ -0,0 +1 @@ +Speed up the :c:func:`PyCode_GetCode` function which also improves accessing the :attr:`~types.CodeType.co_code` attribute in Python. diff --git a/Objects/codeobject.c b/Objects/codeobject.c index c2b29be1fe8693..09413493b8b71a 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -334,6 +334,7 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con) /* not set */ co->co_weakreflist = NULL; co->co_extra = NULL; + co->_co_code = NULL; co->co_warmup = QUICKENING_INITIAL_WARMUP_VALUE; memcpy(_PyCode_CODE(co), PyBytes_AS_STRING(con->code), @@ -1367,12 +1368,17 @@ deopt_code(_Py_CODEUNIT *instructions, Py_ssize_t len) PyObject * _PyCode_GetCode(PyCodeObject *co) { + if (co->_co_code != NULL) { + return Py_NewRef(co->_co_code); + } PyObject *code = PyBytes_FromStringAndSize((const char *)_PyCode_CODE(co), _PyCode_NBYTES(co)); if (code == NULL) { return NULL; } deopt_code((_Py_CODEUNIT *)PyBytes_AS_STRING(code), Py_SIZE(co)); + assert(co->_co_code == NULL); + co->_co_code = (void *)Py_NewRef(code); return code; } @@ -1531,6 +1537,7 @@ code_dealloc(PyCodeObject *co) Py_XDECREF(co->co_qualname); Py_XDECREF(co->co_linetable); Py_XDECREF(co->co_exceptiontable); + Py_XDECREF(co->_co_code); if (co->co_weakreflist != NULL) { PyObject_ClearWeakRefs((PyObject*)co); } diff --git a/Programs/test_frozenmain.h b/Programs/test_frozenmain.h index 1c279134e94dc9..e69de29bb2d1d6 100644 --- a/Programs/test_frozenmain.h +++ b/Programs/test_frozenmain.h @@ -1,42 +0,0 @@ -// Auto-generated by Programs/freeze_test_frozenmain.py -unsigned char M_test_frozenmain[] = { - 227,0,0,0,0,0,0,0,0,0,0,0,0,8,0,0, - 0,0,0,0,0,115,176,0,0,0,151,0,100,0,100,1, - 108,0,90,0,100,0,100,1,108,1,90,1,2,0,101,2, - 100,2,166,1,0,0,171,1,0,0,0,0,0,0,0,0, - 1,0,2,0,101,2,100,3,101,0,106,3,0,0,0,0, - 0,0,0,0,166,2,0,0,171,2,0,0,0,0,0,0, - 0,0,1,0,2,0,101,1,106,4,0,0,0,0,0,0, - 0,0,166,0,0,0,171,0,0,0,0,0,0,0,0,0, - 100,4,25,0,0,0,0,0,0,0,0,0,90,5,100,5, - 68,0,93,25,90,6,2,0,101,2,100,6,101,6,155,0, - 100,7,101,5,101,6,25,0,0,0,0,0,0,0,0,0, - 155,0,157,4,166,1,0,0,171,1,0,0,0,0,0,0, - 0,0,1,0,140,26,100,1,83,0,41,8,233,0,0,0, - 0,78,122,18,70,114,111,122,101,110,32,72,101,108,108,111, - 32,87,111,114,108,100,122,8,115,121,115,46,97,114,103,118, - 218,6,99,111,110,102,105,103,41,5,218,12,112,114,111,103, - 114,97,109,95,110,97,109,101,218,10,101,120,101,99,117,116, - 97,98,108,101,218,15,117,115,101,95,101,110,118,105,114,111, - 110,109,101,110,116,218,17,99,111,110,102,105,103,117,114,101, - 95,99,95,115,116,100,105,111,218,14,98,117,102,102,101,114, - 101,100,95,115,116,100,105,111,122,7,99,111,110,102,105,103, - 32,122,2,58,32,41,7,218,3,115,121,115,218,17,95,116, - 101,115,116,105,110,116,101,114,110,97,108,99,97,112,105,218, - 5,112,114,105,110,116,218,4,97,114,103,118,218,11,103,101, - 116,95,99,111,110,102,105,103,115,114,2,0,0,0,218,3, - 107,101,121,169,0,243,0,0,0,0,250,18,116,101,115,116, - 95,102,114,111,122,101,110,109,97,105,110,46,112,121,250,8, - 60,109,111,100,117,108,101,62,114,17,0,0,0,1,0,0, - 0,115,152,0,0,0,248,240,6,0,1,11,128,10,128,10, - 128,10,216,0,24,208,0,24,208,0,24,208,0,24,224,0, - 5,128,5,208,6,26,209,0,27,212,0,27,208,0,27,216, - 0,5,128,5,128,106,144,35,148,40,209,0,27,212,0,27, - 208,0,27,216,9,38,208,9,26,212,9,38,209,9,40,212, - 9,40,168,24,212,9,50,128,6,240,2,6,12,2,240,0, - 7,1,42,240,0,7,1,42,128,67,240,14,0,5,10,128, - 69,208,10,40,144,67,208,10,40,208,10,40,152,54,160,35, - 156,59,208,10,40,208,10,40,209,4,41,212,4,41,208,4, - 41,208,4,41,240,15,7,1,42,240,0,7,1,42,114,15, - 0,0,0, -}; From 0f9511fc80c8e36769b3612ead6f2ceb18f46811 Mon Sep 17 00:00:00 2001 From: Ken Jin Date: Sat, 4 Jun 2022 20:15:13 +0800 Subject: [PATCH 2/7] Update test_frozenmain.h --- Programs/test_frozenmain.h | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/Programs/test_frozenmain.h b/Programs/test_frozenmain.h index e69de29bb2d1d6..eec2e0cc6048fa 100644 --- a/Programs/test_frozenmain.h +++ b/Programs/test_frozenmain.h @@ -0,0 +1,42 @@ +// Auto-generated by Programs/freeze_test_frozenmain.py +unsigned char M_test_frozenmain[] = { + 227,0,0,0,0,0,0,0,0,0,0,0,0,8,0,0, + 0,0,0,0,0,243,176,0,0,0,151,0,100,0,100,1, + 108,0,90,0,100,0,100,1,108,1,90,1,2,0,101,2, + 100,2,166,1,0,0,171,1,0,0,0,0,0,0,0,0, + 1,0,2,0,101,2,100,3,101,0,106,3,0,0,0,0, + 0,0,0,0,166,2,0,0,171,2,0,0,0,0,0,0, + 0,0,1,0,2,0,101,1,106,4,0,0,0,0,0,0, + 0,0,166,0,0,0,171,0,0,0,0,0,0,0,0,0, + 100,4,25,0,0,0,0,0,0,0,0,0,90,5,100,5, + 68,0,93,25,90,6,2,0,101,2,100,6,101,6,155,0, + 100,7,101,5,101,6,25,0,0,0,0,0,0,0,0,0, + 155,0,157,4,166,1,0,0,171,1,0,0,0,0,0,0, + 0,0,1,0,140,26,100,1,83,0,41,8,233,0,0,0, + 0,78,122,18,70,114,111,122,101,110,32,72,101,108,108,111, + 32,87,111,114,108,100,122,8,115,121,115,46,97,114,103,118, + 218,6,99,111,110,102,105,103,41,5,218,12,112,114,111,103, + 114,97,109,95,110,97,109,101,218,10,101,120,101,99,117,116, + 97,98,108,101,218,15,117,115,101,95,101,110,118,105,114,111, + 110,109,101,110,116,218,17,99,111,110,102,105,103,117,114,101, + 95,99,95,115,116,100,105,111,218,14,98,117,102,102,101,114, + 101,100,95,115,116,100,105,111,122,7,99,111,110,102,105,103, + 32,122,2,58,32,41,7,218,3,115,121,115,218,17,95,116, + 101,115,116,105,110,116,101,114,110,97,108,99,97,112,105,218, + 5,112,114,105,110,116,218,4,97,114,103,118,218,11,103,101, + 116,95,99,111,110,102,105,103,115,114,3,0,0,0,218,3, + 107,101,121,169,0,243,0,0,0,0,250,18,116,101,115,116, + 95,102,114,111,122,101,110,109,97,105,110,46,112,121,250,8, + 60,109,111,100,117,108,101,62,114,18,0,0,0,1,0,0, + 0,115,152,0,0,0,248,240,6,0,1,11,128,10,128,10, + 128,10,216,0,24,208,0,24,208,0,24,208,0,24,224,0, + 5,128,5,208,6,26,209,0,27,212,0,27,208,0,27,216, + 0,5,128,5,128,106,144,35,148,40,209,0,27,212,0,27, + 208,0,27,216,9,38,208,9,26,212,9,38,209,9,40,212, + 9,40,168,24,212,9,50,128,6,240,2,6,12,2,240,0, + 7,1,42,240,0,7,1,42,128,67,240,14,0,5,10,128, + 69,208,10,40,144,67,208,10,40,208,10,40,152,54,160,35, + 156,59,208,10,40,208,10,40,209,4,41,212,4,41,208,4, + 41,208,4,41,240,15,7,1,42,240,0,7,1,42,114,16, + 0,0,0, +}; From 56b017deb679b3819b21b5ef9027c85adbc0ca30 Mon Sep 17 00:00:00 2001 From: Ken Jin Date: Sun, 5 Jun 2022 14:57:25 +0800 Subject: [PATCH 3/7] Address Brandt's review --- Objects/codeobject.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 09413493b8b71a..1421c574b3ea03 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1377,8 +1377,7 @@ _PyCode_GetCode(PyCodeObject *co) return NULL; } deopt_code((_Py_CODEUNIT *)PyBytes_AS_STRING(code), Py_SIZE(co)); - assert(co->_co_code == NULL); - co->_co_code = (void *)Py_NewRef(code); + Py_XSETREF(co->_co_code, Py_NewRef(code)); return code; } From 5af4e7630844cc79e031e305c637d1f839fc4e65 Mon Sep 17 00:00:00 2001 From: Ken Jin Date: Sat, 11 Jun 2022 14:02:44 +0800 Subject: [PATCH 4/7] Apply suggestions by Mark --- Include/cpython/code.h | 2 +- .../2022-05-31-16-36-30.gh-issue-93382.Jf6gAj.rst | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Include/cpython/code.h b/Include/cpython/code.h index f544ea87406e25..364a335e5e410a 100644 --- a/Include/cpython/code.h +++ b/Include/cpython/code.h @@ -88,7 +88,7 @@ typedef uint16_t _Py_CODEUNIT; PyObject *co_qualname; /* unicode (qualname, for reference) */ \ PyObject *co_linetable; /* bytes object that holds location info */ \ PyObject *co_weakreflist; /* to support weakrefs to code objects */ \ - void *_co_code; /* cached co_code object/attribute */ \ + PyObject *_co_code; /* cached co_code object/attribute */ \ /* Scratch space for extra data relating to the code object. \ Type is a void* to keep the format private in codeobject.c to force \ people to go through the proper APIs. */ \ diff --git a/Misc/NEWS.d/next/Core and Builtins/2022-05-31-16-36-30.gh-issue-93382.Jf6gAj.rst b/Misc/NEWS.d/next/Core and Builtins/2022-05-31-16-36-30.gh-issue-93382.Jf6gAj.rst index 04b9e8ba481874..1fe821edf5a148 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2022-05-31-16-36-30.gh-issue-93382.Jf6gAj.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2022-05-31-16-36-30.gh-issue-93382.Jf6gAj.rst @@ -1 +1,2 @@ -Speed up the :c:func:`PyCode_GetCode` function which also improves accessing the :attr:`~types.CodeType.co_code` attribute in Python. +Cache the result of :c:func:`PyCode_GetCode` function to restore the O(1) +lookup of the :attr:`~types.CodeType.co_code` attribute. From be1baada036ad94f12e1900e2796cb5fb235c443 Mon Sep 17 00:00:00 2001 From: Ken Jin Date: Sun, 12 Jun 2022 00:26:14 +0800 Subject: [PATCH 5/7] Fix memory leak in deepfrozen code objects Co-Authored-By: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> --- Objects/codeobject.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 1421c574b3ea03..34893c104a364b 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -2091,6 +2091,7 @@ _PyStaticCode_Dealloc(PyCodeObject *co) deopt_code(_PyCode_CODE(co), Py_SIZE(co)); co->co_warmup = QUICKENING_INITIAL_WARMUP_VALUE; PyMem_Free(co->co_extra); + Py_CLEAR(co->_co_code); co->co_extra = NULL; if (co->co_weakreflist != NULL) { PyObject_ClearWeakRefs((PyObject *)co); From 887120f6b6a4332db18260cc9b5f937e585a72ef Mon Sep 17 00:00:00 2001 From: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com> Date: Wed, 22 Jun 2022 23:46:27 +0800 Subject: [PATCH 6/7] add to deepfreeze.py --- Objects/codeobject.c | 3 ++- Tools/scripts/deepfreeze.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 508534aa0f9c6d..8b9ca890431c6c 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1431,7 +1431,8 @@ _PyCode_GetCode(PyCodeObject *co) return NULL; } deopt_code((_Py_CODEUNIT *)PyBytes_AS_STRING(code), Py_SIZE(co)); - Py_XSETREF(co->_co_code, Py_NewRef(code)); + assert(co->_co_code == NULL); + co->_co_code = Py_NewRef(code); return code; } diff --git a/Tools/scripts/deepfreeze.py b/Tools/scripts/deepfreeze.py index 50d0b345ed407d..43a7a98fcc503c 100644 --- a/Tools/scripts/deepfreeze.py +++ b/Tools/scripts/deepfreeze.py @@ -279,6 +279,7 @@ def generate_code(self, name: str, code: types.CodeType) -> str: self.write(f".co_name = {co_name},") self.write(f".co_qualname = {co_qualname},") self.write(f".co_linetable = {co_linetable},") + self.write(f"._co_code = NULL,") self.write("._co_linearray = NULL,") self.write(f".co_code_adaptive = {co_code_adaptive},") name_as_code = f"(PyCodeObject *)&{name}" From b34659cb2e6d6402531126ef1aff02b240525f04 Mon Sep 17 00:00:00 2001 From: Ken <28750310+Fidget-Spinner@users.noreply.github.com> Date: Thu, 23 Jun 2022 18:28:23 +0800 Subject: [PATCH 7/7] make regen-abidump --- Doc/data/python3.11.abi | 1051 ++++++++++++++++++++------------------- 1 file changed, 527 insertions(+), 524 deletions(-) diff --git a/Doc/data/python3.11.abi b/Doc/data/python3.11.abi index 6e478d78a9e15a..79b3ca3995c246 100644 --- a/Doc/data/python3.11.abi +++ b/Doc/data/python3.11.abi @@ -5858,119 +5858,122 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + + + + - + - + - + - + - + - + - + - + - + - + @@ -7451,93 +7454,93 @@ - - - + + + - - + + - - - - + + + + - - - - + + + + - - - - - - - + + + + + + + - - - + + + - - - + + + - - - - + + + + - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + @@ -7591,8 +7594,8 @@ - - + + @@ -8279,7 +8282,7 @@ - + @@ -9255,307 +9258,307 @@ - + - + - + - + - + - + - - + + - - + + - - - - - - - + + + + + + + - - - + + + - - + + - - - + + + - - + + - + - - + + - - + + - + - + - + - + - - - - + + + + - + - - - - - + + + + + - + - - - - - + + + + + - - - - + + + + - + - + - - + + - - - - + + + + - + - - - - + + + + - - - - + + + + - - - + + + - - - - + + + + - + - + - + - + - + - - + + - - + + - - - - + + + + - - - - + + + + - - + + - + - + - + - + - - + + - + - + - - - - + + + + - + - + - - - + + + - - + + - - - - + + + + - - - + + + - - + + - - + + - - + + - - + + - - - + + + @@ -10031,48 +10034,48 @@ - - + + - + - - - + + + - - - + + + - - - + + + - - + + - - + + - - - + + + - - + + - - + + @@ -10104,83 +10107,83 @@ - - + + - - - - + + + + - - - + + + - - - + + + - - + + - - + + - - - + + + - + - - - + + + - - - - + + + + - - - + + + - - + + - - + + - + - - - + + + - - - + + + - - + + @@ -10742,8 +10745,8 @@ - - + + @@ -11101,54 +11104,54 @@ - + - - - - - - - + + + + + + + - - - - - - - + + + + + + + - - - - - - - + + + + + + + - - - - + + + + - - - - + + + + - - - - + + + + @@ -11169,32 +11172,32 @@ - + - - + + - - + + - + - - - + + + - + - + @@ -11208,33 +11211,33 @@ - - + + - + - + - - + + - + - + - + - - - + + + @@ -11360,131 +11363,131 @@ - - - - + + + + - - - + + + - - - - + + + + - - + + - - + + - - - - - - - - - - - - + + + + + + + + + + + + - - - - + + + + - - - + + + - + - - - - + + + + - - - + + + - - + + - + - + - - - + + + - - - - + + + + - - + + - - + + - + - - + + - - + + - - + + - + - + - + - + - - + + @@ -13014,7 +13017,7 @@ - + @@ -13085,9 +13088,9 @@ - - - + + + @@ -13171,7 +13174,7 @@ - + @@ -13205,8 +13208,8 @@ - - + + @@ -13238,7 +13241,7 @@ - + @@ -14204,41 +14207,41 @@ - + - + - + - + - + - + - - + + - - + + - - + + - - + + - + @@ -14531,7 +14534,7 @@ - + @@ -14540,7 +14543,7 @@ - + @@ -14785,7 +14788,7 @@ - + @@ -14812,7 +14815,7 @@ - + @@ -14872,7 +14875,7 @@ - + @@ -14916,7 +14919,7 @@ - + @@ -15043,7 +15046,7 @@ - + @@ -15588,7 +15591,7 @@ - + @@ -15602,7 +15605,7 @@ - + @@ -15759,80 +15762,80 @@ - - - - + + + + - - - + + + - - - + + + - - - - + + + + - - - + + + - + - - - - - - - + + + + + + + - - - - - - - + + + + + + + - - - - + + + + - - - + + + - - - - + + + + - - + + - - - + + + @@ -16274,10 +16277,10 @@ - - - - + + + + @@ -16365,7 +16368,7 @@ - +