python / python/cpython

Optimize `frozenset` construction

Offen
#150,027 2 Kommentare 1 Reaktion 1 zugewiesene Person Auf GitHub ansehen

@ZeroIntensity arbeitet bereits daran.

Seit 18.5.2026.

performance sprint type-feature
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Feature or enhancement

Proposal:

Many cases of frozenset look like this:

xyz = frozenset({1, 2, 3})

Currently, this constructs a new frozenset and copies the set object created by the literal. We can optimize this by detecting that it's a set literal via PyUnstable_Object_IsUniqueReferencedTemporary, and then simply move the mutable set 's memory into the frozenset without a copy.

Additionally, we have some optimizations for generator expressions inside sets. For example, this will be optimized by the compiler into some bytecode magic:

xyz = set((index for index in range(5))

We can extend this optimization to frozensets as well.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Linked PRs
  • gh-150028
  • gh-153807

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.