bpo-43693: Compute deref offsets in compiler (gh-25152)

Merges locals and cells into a single array.
Saves a pointer in the interpreter and means that we don't need the LOAD_CLOSURE opcode any more

https://bugs.python.org/issue43693
This commit is contained in:
Mark Shannon 2021-06-04 01:03:54 +01:00 committed by GitHub
parent 35002aa8f6
commit b2bf2bc1ec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 222 additions and 209 deletions

View file

@ -7435,6 +7435,24 @@ guarantee_lineno_for_exits(struct assembler *a, int firstlineno) {
}
}
static void
offset_derefs(basicblock *entryblock, int nlocals)
{
for (basicblock *b = entryblock; b != NULL; b = b->b_next) {
for (int i = 0; i < b->b_iused; i++) {
struct instr *inst = &b->b_instr[i];
switch(inst->i_opcode) {
case LOAD_CLOSURE:
case LOAD_DEREF:
case STORE_DEREF:
case DELETE_DEREF:
case LOAD_CLASSDEREF:
inst->i_oparg += nlocals;
}
}
}
}
static PyCodeObject *
assemble(struct compiler *c, int addNone)
{
@ -7490,6 +7508,9 @@ assemble(struct compiler *c, int addNone)
a.a_entry = entryblock;
a.a_nblocks = nblocks;
assert(PyDict_GET_SIZE(c->u->u_varnames) < INT_MAX);
offset_derefs(entryblock, (int)PyDict_GET_SIZE(c->u->u_varnames));
consts = consts_dict_keys_inorder(c->u->u_consts);
if (consts == NULL) {
goto error;