gh-150204: Optimize literal null unpack idiom#150205
Closed
gesslerpd wants to merge 8 commits into
Closed
Conversation
…work as checking all elements
…mout of work as checking all elements" This reverts commit c0374cc.
Member
|
This comes at a cost of checking for every literal container constructions. In addition, there was no concrete support from core devs for this feature so please wait for that first. |
Contributor
Author
|
Thanks for feedback, yes definitely thought about the potential cost so reduced the scope to only checking 1 element containers. Also have now since pushed a commit to reduce the scope to only modifying set only AFAIK repeated runtime performance gains of cached resulting bytecode may outweigh the small performance hit AST preprocessor takes for doing this optimization. Will wait and see the outcome of PEP 802, this can serve as a reference implementation of what can be done if the language never has a true empty set literal form. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposal
When parsing a set container literal whose only element is a direct
*(), simplify that element out of the AST during preprocessing.Proposed examples:
Motivation is full
ast.unparse()symmetry for the empty-set idiom.Today,
ast.unparse(ast.Set(elts=[]))renders as{*()}, because Python has no dedicated empty-set literal. Without AST canonicalization on parse, reparsing that output produces aSetcontainingStarred(Tuple([]))instead of an emptySet, so the AST does not round-trip cleanly.With this change:
would produce the same
ast.Set(elts=[])structure again.Scope
This proposal is intentionally narrow for performance reasons.
Only simplify the direct lone
{*()}case:{*()}x = {*(),}Do not simplify larger literals such as:
{*(), 2}{1, *(), 2}That keeps the rule cheap and focused on empty-set idiom rather than introducing a broader AST rewrite.
Compatibility and behavior
Runtime semantics do not change.
The visible AST change is that tools would no longer see
Starred(Tuple([]))for the lone forms above; they would instead see emptySetnode.That is an externally visible AST change, but it is narrow and maps a degenerate form to the canonical empty set node.
Notable consequences:
ast.literal_eval('{*()}')can evaluate toset()once the parsed AST is canonicalizedast.unparsesymmetry #150204