Skip to content

gh-148817: Fold long lists/sets of constant elements into constant tuples/frozensets - #149016

Merged
Eclips4 merged 6 commits into
python:mainfrom
Eclips4:remove-check-for-size
Aug 16, 2026
Merged

Eclips4 merged 6 commits into
python:mainfrom
Eclips4:remove-check-for-size

Conversation

@Eclips4

@Eclips4 Eclips4 commented Apr 26, 2026 •

Copy link
Copy Markdown
Member
@itamaro

itamaro commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

thanks for the PR @Eclips4 !
it looks good to me, and I tested it locally and confirmed it behaves as expected!
since this isn't my area of expertise, I'll leave further review and approval to relevant experts (like Irit)

Comment thread Python/flowgraph.c Outdated
LIST_APPEND 1
CALL_INTRINSIC_1 INTRINSIC_LIST_TO_TUPLE
LIST_APPEND/SET_ADD 1
[CALL_INTRINSIC_1 INTRINSIC_LIST_TO_TUPLE] <-- when intrinsic_at_i is true

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can intrinsic_at_i be renamed to something clearer? Maybe just expected_append?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done

Comment thread Python/flowgraph.c Resolved
Comment thread Python/flowgraph.c Outdated
if (oparg == INTRINSIC_LIST_TO_TUPLE) {
if (nextop == GET_ITER) {
RETURN_IF_ERROR(fold_constant_intrinsic_list_to_tuple(bb, i, consts, const_cache, consts_index));
/* If folding didn't apply, the list-to-tuple conversion

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this related to the current PR? It seems like a separate optimisation.

Also, why is the to-tuple redundant only when the folding did not apply? If it's folded it's already a tuple, right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, you're right, if it's folded it's already a tuple, was I meant to say if it was folded, the intrinsic is no longer exists in this case. And yes, this is a separate optimization, so we can apply it as a follow up; I'll remove it.

@hugovk
hugovk removed their request for review April 28, 2026 06:09
@Eclips4
Eclips4 requested a review from iritkatriel April 30, 2026 20:00
@read-the-docs-community

read-the-docs-community Bot commented Apr 30, 2026 •

Copy link
Copy Markdown
@StanFromIreland StanFromIreland added the needs backport to 3.15 bugs and security fixes label May 7, 2026
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Aug 10, 2026
@itamaro

itamaro commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

@Eclips4 @iritkatriel anything left to do here?

@Eclips4

Eclips4 commented Aug 13, 2026

Copy link
Copy Markdown
Member Author

I think this is ready, but it would be great if @iritkatriel could take another look.

Comment thread Python/flowgraph.c Outdated
considered. When expected_append is false, the instruction at `i` is the
trailing LIST_APPEND or SET_ADD itself, the matching BUILD_LIST/BUILD_SET
start is selected from its opcode, and for sets the result is wrapped in
a frozenset.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could we, instead of adding the expected_append arg, derive it from the current opcocde? (if i is a LIST_TO_TUPLE then it's true)?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, good idea.

Comment thread Python/flowgraph.c Resolved
Merge branch 'main' into remove-check-for-size
Comment thread Python/flowgraph.c
break;
case LIST_APPEND:
case SET_ADD:
if (oparg == 1 && (nextop == GET_ITER || nextop == CONTAINS_OP)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think there may be other cases where the optimisation can be applied:
LIST_EXTEND, SET_UPDATE , UNPACK_SEQUENCE.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'll do that in follow-up, but from a quick look it seems to be true

@iritkatriel iritkatriel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@Eclips4
Eclips4 merged commit 084230e into python:main Aug 16, 2026
58 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @Eclips4 for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖 I'm not a witch! I'm not a witch!

@miss-islington-app

Copy link
Copy Markdown

Sorry, @Eclips4, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 084230eb2d46a701fe059aa7cec6b2aa1be75814 3.14
@bedevere-app

bedevere-app Bot commented Aug 16, 2026

Copy link
Copy Markdown

GH-155897 is a backport of this pull request to the 3.15 branch.

@bedevere-app

bedevere-app Bot commented Aug 16, 2026

Copy link
Copy Markdown

GH-155898 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Aug 16, 2026
Eclips4 added a commit that referenced this pull request Aug 16, 2026
…tant tu… (#155898)

gh-148817: Fold long lists/sets of constant elements into constant tuples/frozensets (#149016)

Fold long lists/sets of constant elements into constant tuples/frozensets.


(cherry picked from commit 084230e)

Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Comment thread Python/flowgraph.c
LOAD_CONST to alternate. If `i` is the trailing LIST_TO_TUPLE
intrinsic, the next instruction back is an APPEND. If `i` is the
trailing APPEND itself, the next instruction back is a LOAD_CONST. */
bool expect_append = expected_append;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It might be more readable if we had expected_load instead of expected_append. Now that it can be SET_ADD, expected_append can read as "append rather than add".

hugovk pushed a commit that referenced this pull request Aug 19, 2026
…tant tuples/frozensets (GH-149016) (#155897)

Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale Stale PR or inactive for long period of time.

4 participants