Skip to content

bpo-46329: Avoid PUSH_NULL when followed by LOAD_GLOBAL #31933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Doc/library/dis.rst
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ the following command can be used to display the disassembly of
1 0 RESUME 0

2 2 PUSH_NULL
4 LOAD_GLOBAL 0 (len)
4 LOAD_GLOBAL 1 (NULL + len)
6 LOAD_FAST 0 (alist)
8 PRECALL 1
10 CALL 1
Expand Down Expand Up @@ -996,8 +996,11 @@ iterations of the loop.

.. opcode:: LOAD_GLOBAL (namei)

Loads the global named ``co_names[namei]`` onto the stack.
Loads the global named ``co_names[namei>>1]`` onto the stack.

.. versionchanged:: 3.11
If the low bit of ``namei`` is set, then a ``NULL`` is pushed to the
stack before the global variable.

.. opcode:: LOAD_FAST (var_num)

Expand Down
8 changes: 7 additions & 1 deletion Lib/dis.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
MAKE_FUNCTION_FLAGS = ('defaults', 'kwdefaults', 'annotations', 'closure')

LOAD_CONST = opmap['LOAD_CONST']
LOAD_GLOBAL = opmap['LOAD_GLOBAL']
BINARY_OP = opmap['BINARY_OP']

CACHE = opmap["CACHE"]
Expand Down Expand Up @@ -430,7 +431,12 @@ def _get_instructions_bytes(code, varname_from_oparg=None,
if op in hasconst:
argval, argrepr = _get_const_info(op, arg, co_consts)
elif op in hasname:
argval, argrepr = _get_name_info(arg, get_name)
if op == LOAD_GLOBAL:
argval, argrepr = _get_name_info(arg//2, get_name)
if (arg & 1) and argrepr:
argrepr = "NULL + " + argrepr
else:
argval, argrepr = _get_name_info(arg, get_name)
elif op in hasjabs:
argval = arg*2
argrepr = "to " + repr(argval)
Expand Down
3 changes: 2 additions & 1 deletion Lib/importlib/_bootstrap_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ def _write_atomic(path, data, mode=0o666):
# Python 3.11a5 3485 (Add an oparg to GET_AWAITABLE)
# Python 3.11a6 3486 (Use inline caching for PRECALL and CALL)
# Python 3.11a6 3487 (Remove the adaptive "oparg counter" mechanism)
# Python 3.11a6 3488 (LOAD_GLOBAL can push additional NULL)

# Python 3.12 will start with magic number 3500

Expand All @@ -409,7 +410,7 @@ def _write_atomic(path, data, mode=0o666):
# Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array
# in PC/launcher.c must also be updated.

MAGIC_NUMBER = (3487).to_bytes(2, 'little') + b'\r\n'
MAGIC_NUMBER = (3488).to_bytes(2, 'little') + b'\r\n'
_RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c

_PYCACHE = '__pycache__'
Expand Down
355 changes: 170 additions & 185 deletions Lib/test/test_dis.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Use low bit of ``LOAD_GLOBAL`` to indicate whether to push a ``NULL`` before
the global. Helps streamline the call sequence a bit.
18 changes: 13 additions & 5 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2947,7 +2947,9 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int

TARGET(LOAD_GLOBAL) {
PREDICTED(LOAD_GLOBAL);
PyObject *name = GETITEM(names, oparg);
int push_null = oparg & 1;
PEEK(0) = NULL;
PyObject *name = GETITEM(names, oparg>>1);
PyObject *v;
if (PyDict_CheckExact(GLOBALS())
&& PyDict_CheckExact(BUILTINS()))
Expand All @@ -2970,7 +2972,6 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
/* Slow-path if globals or builtins is not a dict */

/* namespace 1: globals */
name = GETITEM(names, oparg);
v = PyObject_GetItem(GLOBALS(), name);
if (v == NULL) {
if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Expand All @@ -2992,6 +2993,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
}
/* Skip over inline cache */
JUMPBY(INLINE_CACHE_ENTRIES_LOAD_GLOBAL);
STACK_GROW(push_null);
PUSH(v);
DISPATCH();
}
Expand All @@ -3000,7 +3002,7 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
assert(cframe.use_tracing == 0);
_PyLoadGlobalCache *cache = (_PyLoadGlobalCache *)next_instr;
if (cache->counter == 0) {
PyObject *name = GETITEM(names, oparg);
PyObject *name = GETITEM(names, oparg>>1);
next_instr--;
if (_Py_Specialize_LoadGlobal(GLOBALS(), BUILTINS(), next_instr, name) < 0) {
goto error;
Expand All @@ -3025,10 +3027,13 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
PyDictUnicodeEntry *entries = DK_UNICODE_ENTRIES(dict->ma_keys);
PyObject *res = entries[cache->index].me_value;
DEOPT_IF(res == NULL, LOAD_GLOBAL);
int push_null = oparg & 1;
PEEK(0) = NULL;
JUMPBY(INLINE_CACHE_ENTRIES_LOAD_GLOBAL);
STAT_INC(LOAD_GLOBAL, hit);
STACK_GROW(push_null+1);
Py_INCREF(res);
PUSH(res);
SET_TOP(res);
NOTRACE_DISPATCH();
}

Expand All @@ -3047,10 +3052,13 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
PyDictUnicodeEntry *entries = DK_UNICODE_ENTRIES(bdict->ma_keys);
PyObject *res = entries[cache->index].me_value;
DEOPT_IF(res == NULL, LOAD_GLOBAL);
int push_null = oparg & 1;
PEEK(0) = NULL;
JUMPBY(INLINE_CACHE_ENTRIES_LOAD_GLOBAL);
STAT_INC(LOAD_GLOBAL, hit);
STACK_GROW(push_null+1);
Py_INCREF(res);
PUSH(res);
SET_TOP(res);
NOTRACE_DISPATCH();
}

Expand Down
15 changes: 13 additions & 2 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ stack_effect(int opcode, int oparg, int jump)
return -1;

case LOAD_GLOBAL:
return 1;
return (oparg & 1) + 1;

/* Exception handling pseudo-instructions */
case SETUP_FINALLY:
Expand Down Expand Up @@ -4185,8 +4185,12 @@ compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
assert(op);
arg = compiler_add_o(dict, mangled);
Py_DECREF(mangled);
if (arg < 0)
if (arg < 0) {
return 0;
}
if (op == LOAD_GLOBAL) {
arg <<= 1;
}
return compiler_addop_i(c, op, arg);
}

Expand Down Expand Up @@ -8812,6 +8816,13 @@ optimize_basic_block(struct compiler *c, basicblock *bb, PyObject *consts)
break;
case KW_NAMES:
break;
case PUSH_NULL:
if (nextop == LOAD_GLOBAL && (inst[1].i_opcode & 1) == 0) {
inst->i_opcode = NOP;
inst->i_oparg = 0;
inst[1].i_oparg |= 1;
}
break;
default:
/* All HAS_CONST opcodes should be handled with LOAD_CONST */
assert (!HAS_CONST(inst->i_opcode));
Expand Down