Conversation
`_hashable_values_` is a list, so checking it for each new member made class creation quadratic. Skip the check when the value was just added to `_value2member_map_`, since it can't be in the list yet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Every time a member is added,
_proto_member.__set_name__runsvalue not in enum_class._hashable_values_. That's a list, so each new member scans every member before it, and building the class is O(n²).The scan is only needed when the value was already in
_value2member_map_, which happens for aliases and Flag pseudo-members. Ifsetdefaultjust added the key, the value can't be in the list yet, because the list only ever gets values that were added to the map, and nothing removes from the map. So I compare the map's size before and after and only scan when it didn't grow:It's the same symptom as gh-89580, which was fixed back then; this scan was added later.
Evidence
Enum('Generated', {f'M{i:08x}': i for i in range(n)}), 3.16 dev build, macOS arm64, median of 5, two alternating runs:IntEnum(6 members, 1 alias)New test
test_hashable_values_creation_workcounts__eq__calls while building a 200-member enum:AssertionError: 19900 not less than 800test_hashable_values_with_aliaseschecks that aliases still don't add duplicates to_hashable_values_._hashable_values_is identical before and after for all 70 enum classes created when importingssl,signal,socket,re,http,inspect,uuid,astand a few more.test_enum: 1,093 run, all pass.test_signal,test_http_cookies,test_re,test_ssl,test_inspect,test_pydocandtest_socketshow the same 6 failures with and without the patch; they're local-environment failures in the CLI and online-docs tests.Note:
Enum('X', ['A', 'B', ...])(a list of names, so auto values) is still quadratic, because_create_copieslast_valuesand_generate_next_value_sorts it for every member. That's a public hook that people override, so I've left it alone here.Merge Danger
Door: two-way
Three lines in one private code path.
_hashable_values_ends up with the same contents in the same order.Blast Radius: stdlib
Every Enum class goes through this code. The only way behaviour could change is a value whose
__eq__and__hash__disagree, which already breaks_value2member_map_lookups.AI disclosure: the profiling, the patch and the equivalence check were done with AI tools (Codex, Claude).
🤖 Generated with Claude Code