gh-148871: Add CONSTANT_EMPTY_TUPLE to LOAD_COMMON_CONSTANT (GH-149688) · devdanzin/cpython@f2fa291 · GitHub
Skip to content

Commit f2fa291

Browse files
authored
pythongh-148871: Add CONSTANT_EMPTY_TUPLE to LOAD_COMMON_CONSTANT (pythonGH-149688)
1 parent c35b0f2 commit f2fa291

8 files changed

Lines changed: 20 additions & 9 deletions

File tree

Include/internal/pycore_magic_number.h

Lines changed: 2 additions & 1 deletion

Include/internal/pycore_opcode_utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ extern "C" {
8181
#define CONSTANT_FALSE 10
8282
#define CONSTANT_MINUS_ONE 11
8383
#define CONSTANT_BUILTIN_FROZENSET 12
84-
#define NUM_COMMON_CONSTANTS 13
84+
#define CONSTANT_EMPTY_TUPLE 13
85+
#define NUM_COMMON_CONSTANTS 14
8586

8687
/* Values used in the oparg for RESUME */
8788
#define RESUME_AT_FUNC_START 0

Lib/dis.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,8 @@ def _get_const_value(op, arg, co_consts):
698698
if op == LOAD_SMALL_INT:
699699
return arg
700700
if op == LOAD_COMMON_CONSTANT:
701-
# Opargs 0-6 are callables; 7-11 are literal values.
702-
if 7 <= arg <= 11:
701+
# Opargs 0-6 and 12 are callables; 7-11 and 13 are literal values.
702+
if 7 <= arg <= 11 or arg == 13:
703703
return _common_constants[arg]
704704
return UNKNOWN
705705
argval = UNKNOWN

Lib/opcode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
builtins.set,
4545
# Append-only — must match CONSTANT_* in
4646
# Include/internal/pycore_opcode_utils.h.
47-
None, "", True, False, -1, builtins.frozenset]
47+
None, "", True, False, -1, builtins.frozenset, ()]
4848
_nb_ops = _opcode.get_nb_ops()
4949

5050
hascompare = [opmap["COMPARE_OP"]]

Lib/test/test_peepholer.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,10 +1307,10 @@ def test_build_empty_tuple(self):
13071307
('RETURN_VALUE', None, 0),
13081308
]
13091309
after = [
1310-
('LOAD_CONST', 0, 0),
1310+
('LOAD_COMMON_CONSTANT', opcode._common_constants.index(()), 0),
13111311
('RETURN_VALUE', None, 0),
13121312
]
1313-
self.cfg_optimization_test(before, after, consts=[], expected_consts=[()])
1313+
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
13141314

13151315
def test_fold_tuple_of_constants(self):
13161316
before = [
@@ -1365,10 +1365,10 @@ def test_fold_constant_intrinsic_list_to_tuple(self):
13651365
('RETURN_VALUE', None, 0)
13661366
]
13671367
after = [
1368-
('LOAD_CONST', 0, 0),
1368+
('LOAD_COMMON_CONSTANT', opcode._common_constants.index(()), 0),
13691369
('RETURN_VALUE', None, 0)
13701370
]
1371-
self.cfg_optimization_test(before, after, consts=[], expected_consts=[()])
1371+
self.cfg_optimization_test(before, after, consts=[], expected_consts=[])
13721372

13731373
# multiple BUILD_LIST 0: ([], 1, [], 2)
13741374
same = [
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
The empty tuple ``()`` is now loaded via :opcode:`LOAD_COMMON_CONSTANT`
2+
instead of :opcode:`LOAD_CONST`, removing it from per-code-object
3+
:attr:`~codeobject.co_consts` tuples.

Python/flowgraph.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,10 @@ maybe_instr_make_load_common_const(cfg_instr *instr, PyObject *newconst)
14681468
&& PyUnicode_GET_LENGTH(newconst) == 0) {
14691469
oparg = CONSTANT_EMPTY_STR;
14701470
}
1471+
else if (PyTuple_CheckExact(newconst)
1472+
&& PyTuple_GET_SIZE(newconst) == 0) {
1473+
oparg = CONSTANT_EMPTY_TUPLE;
1474+
}
14711475
else if (PyLong_CheckExact(newconst)) {
14721476
int overflow;
14731477
long val = PyLong_AsLongAndOverflow(newconst, &overflow);

Python/pylifecycle.c

Lines changed: 2 additions & 0 deletions

0 commit comments

Comments
 (0)