mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
bpo-44338: Port LOAD_GLOBAL to PEP 659 adaptive interpreter (GH-26638)
* Add specializations of LOAD_GLOBAL. * Add more stats. * Remove old opcache; it is no longer used. * Add NEWS
This commit is contained in:
parent
fafcfff926
commit
eecbc7c390
9 changed files with 209 additions and 337 deletions
|
@ -350,10 +350,7 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
|
|||
/* not set */
|
||||
co->co_weakreflist = NULL;
|
||||
co->co_extra = NULL;
|
||||
co->co_opcache_map = NULL;
|
||||
co->co_opcache = NULL;
|
||||
co->co_opcache_flag = 0;
|
||||
co->co_opcache_size = 0;
|
||||
|
||||
co->co_warmup = QUICKENING_INITIAL_WARMUP_VALUE;
|
||||
co->co_quickened = NULL;
|
||||
}
|
||||
|
@ -912,55 +909,6 @@ new_linesiterator(PyCodeObject *code)
|
|||
return li;
|
||||
}
|
||||
|
||||
|
||||
/******************
|
||||
* the opcache
|
||||
******************/
|
||||
|
||||
int
|
||||
_PyCode_InitOpcache(PyCodeObject *co)
|
||||
{
|
||||
Py_ssize_t co_size = PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT);
|
||||
co->co_opcache_map = (unsigned char *)PyMem_Calloc(co_size, 1);
|
||||
if (co->co_opcache_map == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
const _Py_CODEUNIT *opcodes = (const _Py_CODEUNIT*)PyBytes_AS_STRING(co->co_code);
|
||||
Py_ssize_t opts = 0;
|
||||
|
||||
for (Py_ssize_t i = 0; i < co_size;) {
|
||||
unsigned char opcode = _Py_OPCODE(opcodes[i]);
|
||||
i++; // 'i' is now aligned to (next_instr - first_instr)
|
||||
|
||||
// TODO: LOAD_METHOD
|
||||
if (opcode == LOAD_GLOBAL || opcode == LOAD_ATTR) {
|
||||
opts++;
|
||||
co->co_opcache_map[i] = (unsigned char)opts;
|
||||
if (opts > 254) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (opts) {
|
||||
co->co_opcache = (_PyOpcache *)PyMem_Calloc(opts, sizeof(_PyOpcache));
|
||||
if (co->co_opcache == NULL) {
|
||||
PyMem_Free(co->co_opcache_map);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
PyMem_Free(co->co_opcache_map);
|
||||
co->co_opcache_map = NULL;
|
||||
co->co_opcache = NULL;
|
||||
}
|
||||
|
||||
co->co_opcache_size = (unsigned char)opts;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/******************
|
||||
* "extra" frame eval info (see PEP 523)
|
||||
******************/
|
||||
|
@ -1207,15 +1155,6 @@ code_new_impl(PyTypeObject *type, int argcount, int posonlyargcount,
|
|||
static void
|
||||
code_dealloc(PyCodeObject *co)
|
||||
{
|
||||
if (co->co_opcache != NULL) {
|
||||
PyMem_Free(co->co_opcache);
|
||||
}
|
||||
if (co->co_opcache_map != NULL) {
|
||||
PyMem_Free(co->co_opcache_map);
|
||||
}
|
||||
co->co_opcache_flag = 0;
|
||||
co->co_opcache_size = 0;
|
||||
|
||||
if (co->co_extra != NULL) {
|
||||
PyInterpreterState *interp = _PyInterpreterState_GET();
|
||||
_PyCodeObjectExtra *co_extra = co->co_extra;
|
||||
|
@ -1442,12 +1381,11 @@ code_sizeof(PyCodeObject *co, PyObject *Py_UNUSED(args))
|
|||
res += co->co_ncellvars * sizeof(Py_ssize_t);
|
||||
}
|
||||
|
||||
if (co->co_opcache != NULL) {
|
||||
assert(co->co_opcache_map != NULL);
|
||||
// co_opcache_map
|
||||
res += PyBytes_GET_SIZE(co->co_code) / sizeof(_Py_CODEUNIT);
|
||||
// co_opcache
|
||||
res += co->co_opcache_size * sizeof(_PyOpcache);
|
||||
if (co->co_quickened != NULL) {
|
||||
Py_ssize_t count = co->co_quickened[0].entry.zero.cache_count;
|
||||
count += (PyBytes_GET_SIZE(co->co_code)+sizeof(SpecializedCacheEntry)-1)/
|
||||
sizeof(SpecializedCacheEntry);
|
||||
res += count * sizeof(SpecializedCacheEntry);
|
||||
}
|
||||
|
||||
return PyLong_FromSsize_t(res);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue