Skip to content
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

bpo-38328: Speed up the creation time of constant list and set literals. #17114

Merged
merged 12 commits into from
Nov 26, 2019
2 changes: 1 addition & 1 deletion Lib/test/test_sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ def get_gen(): yield 1
# list
samples = [[], [1,2,3], ['1', '2', '3']]
for sample in samples:
check(sample, vsize('Pn') + len(sample)*self.P)
check(list(sample), vsize('Pn') + len(sample)*self.P)
methane marked this conversation as resolved.
Show resolved Hide resolved
# sortwrapper (list)
# XXX
# cmpwrapper (list)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Sped up the creation time of constant :class:`list` and :class:`set` displays.
Patch by Brandt Bucher.
22 changes: 22 additions & 0 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ static int compiler_visit_slice(struct compiler *, slice_ty,
expr_context_ty);

static int inplace_binop(struct compiler *, operator_ty);
static int are_all_items_const(asdl_seq *, Py_ssize_t, Py_ssize_t);
static int expr_constant(expr_ty);

static int compiler_with(struct compiler *, stmt_ty, int);
Expand Down Expand Up @@ -3655,6 +3656,27 @@ starunpack_helper(struct compiler *c, asdl_seq *elts,
{
Py_ssize_t n = asdl_seq_LEN(elts);
Py_ssize_t i, nsubitems = 0, nseen = 0;
if (n > 1 && are_all_items_const(elts, 0, n)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is n > 1 really good threshold? How about n > 2?

$ python3 -m timeit -s "a=frozenset((1,2))" -- "{*a}"
5000000 loops, best of 5: 63.4 nsec per loop
$ python3 -m timeit "{1,2}"
5000000 loops, best of 5: 61.5 nsec per loop

$ python3 -m timeit "[*(1,2)]"
5000000 loops, best of 5: 46.5 nsec per loop
$ python3 -m timeit "[1,2]"
5000000 loops, best of 5: 46.6 nsec per loop

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only chose n > 1 because it always emits fewer opcodes than before the change. If you think n > 2 is a better heuristic, I'm fine with that.

Copy link
Member Author

@brandtbucher brandtbucher Nov 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think your first example is only slower because it pays the price of a LOAD_NAME instead of a LOAD_CONST (just a guess, though). It's not a direct comparison, unfortunately.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fewer opcodes is not always more efficient.
BUILD_LIST_UNPACK calls PyList_New(0) and PyList_Extend(iterable). It causes over-allocation.
On the other hand, BUILD_LIST just calls PyList_New(n).

So I think we should be conservative about choosing the threshold.

Copy link
Member Author

@brandtbucher brandtbucher Nov 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback @methane. I think this makes sense.

I've bumped the threshold to your recommended value of n > 2. Anything else?

PyObject *folded = PyTuple_New(n);
if (folded == NULL) {
return 0;
}
PyObject *val;
for (i = 0; i < n; i++) {
val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
Py_INCREF(val);
PyTuple_SET_ITEM(folded, i, val);
}
if (outer_op == BUILD_SET_UNPACK) {
Py_SETREF(folded, PyFrozenSet_New(folded));
if (folded == NULL) {
return 0;
}
}
ADDOP_LOAD_CONST_NEW(c, folded);
ADDOP_I(c, outer_op, 1);
return 1;
}
for (i = 0; i < n; i++) {
expr_ty elt = asdl_seq_GET(elts, i);
if (elt->kind == Starred_kind) {
Expand Down
284 changes: 142 additions & 142 deletions Python/importlib_external.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading