Skip to content

gh-150737: Optimize bytecode for empty unpack cases such as {*()}. - #150812

Merged
JelleZijlstra merged 7 commits into
python:mainfrom
gesslerpd:codegen-ast-leading-only
Sep 21, 2026
Merged

JelleZijlstra merged 7 commits into
python:mainfrom
gesslerpd:codegen-ast-leading-only

Conversation

@gesslerpd

@gesslerpd gesslerpd commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

This is preferred implementation of the proposal in issue.

This change strives to be as self contained as possible but requires adding starunpack_helper_impl(..., start, ...) argument to support elts slice to ignore leading AST starred node.

Less intrusive alternate implementation can be viewed here (suboptimal since it operates without AST, slight behavioral differences shown in the tests): f857d88#diff-3cbf15668c31488528b7ab0f903c674a0ecf550f4f53c1be6cf8cd965246c2a0

gesslerpd added 2 commits May 30, 2026 17:09
Includes the branch-local test expectations for the leading-only behavior.
@gesslerpd gesslerpd changed the title gh-150737: Optimize bytecode for leading null unpack cases such as the ast.unparse empty set representation. gh-150737: Optimize bytecode for literals with leading null unpack, such as the ast.unparse empty set representation. Jun 18, 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 Sep 17, 2026
@gesslerpd

Copy link
Copy Markdown
Contributor Author

ping to "unstale"

cc: @JelleZijlstra

@gesslerpd gesslerpd changed the title gh-150737: Optimize bytecode for literals with leading null unpack, such as the ast.unparse empty set representation. gh-150737: Optimize bytecode for collection literals with leading null unpack, such as the ast.unparse empty set representation. Sep 17, 2026
Comment thread Lib/test/test_compile.py Outdated
('SET_UPDATE', 1),
('RETURN_VALUE', None),
],
'{1, 2, 3, *()}': [

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.

hm would it be much harder to optimize this too? I feel that would be more consistent, and probably not make the code much more complex. We would just ignore empty unpacks everywhere.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for feedback, I can try to add that if you think it's worth it. Started with this since it's really only useful idiom at the start (or end) of collection literal. Current implementation is a balance to not slow down bytecode "compile" times overall. Maybe it doesn't matter much since it only affects compile-time?

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.

Yeah in the first place it's probably not measurable, in the second place bytecode compilation time isn't that important.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added!

Comment thread Python/codegen.c Outdated
return false;
}
expr_ty value = elt->v.Starred.value;
return value->kind == Tuple_kind &&

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.

Why not also lists and dicts?

This comment was marked as outdated.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added, nvm recursive flattening since it's probably not worth added complication for contrived forms.

@gesslerpd gesslerpd changed the title gh-150737: Optimize bytecode for collection literals with leading null unpack, such as the ast.unparse empty set representation. gh-150737: Optimize bytecode for collection literals with null unpack idioms, such as the ast.unparse empty set representation. Sep 21, 2026
Comment thread Misc/NEWS.d/next/Core_and_Builtins/2026-06-02-16-21-02.gh-issue-150737.LoYUFY.rst Outdated
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
@gesslerpd gesslerpd changed the title gh-150737: Optimize bytecode for collection literals with null unpack idioms, such as the ast.unparse empty set representation. gh-150737: Optimize bytecode for empty unpack cases such as {*()}. Sep 21, 2026
@JelleZijlstra
JelleZijlstra enabled auto-merge (squash) September 21, 2026 04:53
@JelleZijlstra
JelleZijlstra merged commit f8b0e26 into python:main Sep 21, 2026
55 checks passed
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants