bpo-44338: Port LOAD_GLOBAL to PEP 659 adaptive interpreter (GH-26638) · python/cpython@eecbc7c

@@ -350,10 +350,7 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)

350350

/* not set */

351351

co->co_weakreflist = NULL;

352352

co->co_extra = NULL;

353-

co->co_opcache_map = NULL;

354-

co->co_opcache = NULL;

355-

co->co_opcache_flag = 0;

356-

co->co_opcache_size = 0;

353+357354

co->co_warmup = QUICKENING_INITIAL_WARMUP_VALUE;

358355

co->co_quickened = NULL;

359356

}

@@ -912,55 +909,6 @@ new_linesiterator(PyCodeObject *code)

912909

return li;

913910

}

914911915-916-

/******************

917-

* the opcache

918-

******************/

919-920-

int

921-

_PyCode_InitOpcache(PyCodeObject *co)

922-

{

923-

Py_ssize_t co_size = PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT);

924-

co->co_opcache_map = (unsigned char *)PyMem_Calloc(co_size, 1);

925-

if (co->co_opcache_map == NULL) {

926-

return -1;

927-

}

928-929-

const _Py_CODEUNIT *opcodes = (const _Py_CODEUNIT*)PyBytes_AS_STRING(co->co_code);

930-

Py_ssize_t opts = 0;

931-932-

for (Py_ssize_t i = 0; i < co_size;) {

933-

unsigned char opcode = _Py_OPCODE(opcodes[i]);

934-

i++; // 'i' is now aligned to (next_instr - first_instr)

935-936-

// TODO: LOAD_METHOD

937-

if (opcode == LOAD_GLOBAL || opcode == LOAD_ATTR) {

938-

opts++;

939-

co->co_opcache_map[i] = (unsigned char)opts;

940-

if (opts > 254) {

941-

break;

942-

}

943-

}

944-

}

945-946-

if (opts) {

947-

co->co_opcache = (_PyOpcache *)PyMem_Calloc(opts, sizeof(_PyOpcache));

948-

if (co->co_opcache == NULL) {

949-

PyMem_Free(co->co_opcache_map);

950-

return -1;

951-

}

952-

}

953-

else {

954-

PyMem_Free(co->co_opcache_map);

955-

co->co_opcache_map = NULL;

956-

co->co_opcache = NULL;

957-

}

958-959-

co->co_opcache_size = (unsigned char)opts;

960-

return 0;

961-

}

962-963-964912

/******************

965913

* "extra" frame eval info (see PEP 523)

966914

******************/

@@ -1207,15 +1155,6 @@ code_new_impl(PyTypeObject *type, int argcount, int posonlyargcount,

12071155

static void

12081156

code_dealloc(PyCodeObject *co)

12091157

{

1210-

if (co->co_opcache != NULL) {

1211-

PyMem_Free(co->co_opcache);

1212-

}

1213-

if (co->co_opcache_map != NULL) {

1214-

PyMem_Free(co->co_opcache_map);

1215-

}

1216-

co->co_opcache_flag = 0;

1217-

co->co_opcache_size = 0;

1218-12191158

if (co->co_extra != NULL) {

12201159

PyInterpreterState *interp = _PyInterpreterState_GET();

12211160

_PyCodeObjectExtra *co_extra = co->co_extra;

@@ -1442,12 +1381,11 @@ code_sizeof(PyCodeObject *co, PyObject *Py_UNUSED(args))

14421381

res += co->co_ncellvars * sizeof(Py_ssize_t);

14431382

}

144413831445-

if (co->co_opcache != NULL) {

1446-

assert(co->co_opcache_map != NULL);

1447-

// co_opcache_map

1448-

res += PyBytes_GET_SIZE(co->co_code) / sizeof(_Py_CODEUNIT);

1449-

// co_opcache

1450-

res += co->co_opcache_size * sizeof(_PyOpcache);

1384+

if (co->co_quickened != NULL) {

1385+

Py_ssize_t count = co->co_quickened[0].entry.zero.cache_count;

1386+

count += (PyBytes_GET_SIZE(co->co_code)+sizeof(SpecializedCacheEntry)-1)/

1387+

sizeof(SpecializedCacheEntry);

1388+

res += count * sizeof(SpecializedCacheEntry);

14511389

}

1452139014531391

return PyLong_FromSsize_t(res);