Skip to content

Commit 6dd9b64

Browse files
brandtbuchermethane
authored andcommitted
bpo-38328: Speed up the creation time of constant list and set display. (GH-17114)
1 parent e4db1f0 commit 6dd9b64

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

Lib/test/test_sys.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1222,7 +1222,7 @@ def get_gen(): yield 1
12221222
# list
12231223
samples = [[], [1,2,3], ['1', '2', '3']]
12241224
for sample in samples:
1225-
check(sample, vsize('Pn') + len(sample)*self.P)
1225+
check(list(sample), vsize('Pn') + len(sample)*self.P)
12261226
# sortwrapper (list)
12271227
# XXX
12281228
# cmpwrapper (list)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Sped up the creation time of constant :class:`list` and :class:`set` displays.
2+
Patch by Brandt Bucher.

Python/compile.c

+22
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ static int compiler_visit_slice(struct compiler *, slice_ty,
197197
expr_context_ty);
198198

199199
static int inplace_binop(struct compiler *, operator_ty);
200+
static int are_all_items_const(asdl_seq *, Py_ssize_t, Py_ssize_t);
200201
static int expr_constant(expr_ty);
201202

202203
static int compiler_with(struct compiler *, stmt_ty, int);
@@ -3655,6 +3656,27 @@ starunpack_helper(struct compiler *c, asdl_seq *elts,
36553656
{
36563657
Py_ssize_t n = asdl_seq_LEN(elts);
36573658
Py_ssize_t i, nsubitems = 0, nseen = 0;
3659+
if (n > 2 && are_all_items_const(elts, 0, n)) {
3660+
PyObject *folded = PyTuple_New(n);
3661+
if (folded == NULL) {
3662+
return 0;
3663+
}
3664+
PyObject *val;
3665+
for (i = 0; i < n; i++) {
3666+
val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3667+
Py_INCREF(val);
3668+
PyTuple_SET_ITEM(folded, i, val);
3669+
}
3670+
if (outer_op == BUILD_SET_UNPACK) {
3671+
Py_SETREF(folded, PyFrozenSet_New(folded));
3672+
if (folded == NULL) {
3673+
return 0;
3674+
}
3675+
}
3676+
ADDOP_LOAD_CONST_NEW(c, folded);
3677+
ADDOP_I(c, outer_op, 1);
3678+
return 1;
3679+
}
36583680
for (i = 0; i < n; i++) {
36593681
expr_ty elt = asdl_seq_GET(elts, i);
36603682
if (elt->kind == Starred_kind) {

0 commit comments

Comments
 (0)