Skip to content

bpo-38328: Speed up the creation time of constant set literals. #16878

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

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sped up the creation time of constant :class:`set` literals. Patch by Brandt Bucher.
24 changes: 22 additions & 2 deletions Python/peephole.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,12 @@ copy_op_arg(_Py_CODEUNIT *codestr, Py_ssize_t i, unsigned char op,
The consts table must still be in list form so that the
new constant (c1, c2, ... cn) can be appended.
Called with codestr pointing to the first LOAD_CONST.
If frozenset is nonzero, do the same thing with BUILD_SET and a frozenset.
*/
static Py_ssize_t
fold_tuple_on_constants(_Py_CODEUNIT *codestr, Py_ssize_t codelen,
Py_ssize_t c_start, Py_ssize_t opcode_end,
PyObject *consts, int n)
PyObject *consts, int n, int frozenset)
{
/* Pre-conditions */
assert(PyList_CheckExact(consts));
Expand Down Expand Up @@ -161,6 +162,15 @@ fold_tuple_on_constants(_Py_CODEUNIT *codestr, Py_ssize_t codelen,
}
#endif

if (frozenset) {
PyObject *tmp = newconst;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm pretty sure Py_SETREF() should be used here instead of this temporary variable.

Copy link
Member Author

Choose a reason for hiding this comment

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

Is Py_SETREF(newconst, PyFrozenSet_New(newconst)) really clearer and safer? I can add it if you think so, but I personally don’t...

newconst = PyFrozenSet_New(tmp);
Py_DECREF(tmp);
if (!newconst) {
return -1;
}
}

/* Append folded constant onto consts */
if (PyList_Append(consts, newconst)) {
Py_DECREF(newconst);
Expand Down Expand Up @@ -321,16 +331,26 @@ PyCode_Optimize(PyObject *code, PyObject* consts, PyObject *names,
break;

/* Try to fold tuples of constants.
Sets of constants are loaded as frozensets and unpacked.
Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
case BUILD_TUPLE:
case BUILD_SET:
j = get_arg(codestr, i);
int is_tuple = opcode == BUILD_TUPLE;
if (!is_tuple && j <= 1) {
break;
}
if (j > 0 && lastlc >= j) {
h = lastn_const_start(codestr, op_start, j);
if (ISBASICBLOCK(blocks, h, op_start)) {
h = fold_tuple_on_constants(codestr, codelen,
h, i+1, consts, j);
h, i+is_tuple, consts, j,
!is_tuple);
if (!is_tuple && h >= 0) {
codestr[i] = PACKOPARG(BUILD_SET_UNPACK, 1);
}
break;
}
}
Expand Down