Visitar URL original
Constant folding of unpacking in sets · Issue #98701 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20250529012016/https://github.com/python/cpython/issues/98701
Skip to content

Constant folding of unpacking in sets #98701

Closed as not planned
Closed as not planned
@thatbirdguythatuknownot

Description

Feature or enhancement

Optimize out an empty/one-element constant unpack into a set, which currently output these bytecodes:

>>> from dis import dis
>>> dis("{*()}")
              0 RESUME                   0

  1           2 BUILD_SET                0
              4 LOAD_CONST               0 (())
              6 SET_UPDATE               1
              8 RETURN_VALUE
>>> dis("{*(1,)}")
              0 RESUME                   0

  1           2 BUILD_SET                0
              4 LOAD_CONST               0 ((1,))
              6 SET_UPDATE               1
              8 RETURN_VALUE

Pitch

Add a check for constants in the set case in Python/ast_opt.c and apply optimizations if the constant is a sequence and the size is 0 or 1.

Previous discussion

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance or resource usagetype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions