python / python/cpython

Optimize `frozenset` construction

Đang mở
#150,027 2 bình luận 1 reaction 1 người được giao Xem trên GitHub

@ZeroIntensity đang làm issue này rồi.

Từ ngày 18/5/2026.

performance sprint type-feature
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.