python / python/cpython

Optimize `frozenset` construction

Aperta
#150,027 2 commenti 1 reazione 1 assegnatario Vedi su GitHub

@ZeroIntensity ci sta già lavorando.

Dal 18/5/2026.

performance sprint type-feature
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.