Skip to content

Optimize frozenset construction #150027

@ZeroIntensity

Description

@ZeroIntensity

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

Metadata

Metadata

Assignees

Labels

performancePerformance or resource usagesprinttype-featureA feature request or enhancement

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions