You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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((indexforindexinrange(5))
We can extend this optimization to frozensets as well.
Heh, I'd been looking at making a similar change a few months ago at work, then forgot to follow up at home. Way more work to accomplish it than I expected. 👍 If I'm reading it correctly, it's doing it smarter than I would have, generating op codes that directly construct a frozenset from a set literal or set comprehension, correct?
Activity
gh-150027: Avoid copying during construction of `frozenset` objects (G…
MojoVampire commentedon May 19, 2026
Heh, I'd been looking at making a similar change a few months ago at work, then forgot to follow up at home. Way more work to accomplish it than I expected. 👍 If I'm reading it correctly, it's doing it smarter than I would have, generating op codes that directly construct a frozenset from a set literal or set comprehension, correct?
MojoVampire commentedon May 19, 2026
Seems like an optimization that would apply to
frozendict({dictliteral})andfrozendict({dictcomp})s as well, right?