]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
bpo-45417: [Enum] fix quadratic behavior during creation (GH-28907)
authorCarl Friedrich Bolz-Tereick <cfbolz@gmx.de>
Thu, 14 Oct 2021 20:59:51 +0000 (22:59 +0200)
committerGitHub <noreply@github.com>
Thu, 14 Oct 2021 20:59:51 +0000 (13:59 -0700)
commitb2af211e229df941d9b404f69547a264115156b5
tree7419794a48171aa351ce7ebb1510bcf6408b2cde
parent0bbea0723ee07f9d7ad9745f0e1875718ef38715
bpo-45417: [Enum] fix quadratic behavior during creation (GH-28907)

Creating an Enum exhibited quadratic behavior based on the number of members in three places:
- `EnumDict._member_names`: a list searched with each new member's name
- member creation: a `for` loop checking each existing member to see if new member was a duplicate
- `auto()` values: a list of all previous values in enum was copied before being sent to `_generate_next_value()`

Two of those issues have been resolved:
- `_EnumDict._member_names` is now a dictionary so lookups are fast
- member creation tries a fast value lookup before falling back to the slower `for` loop lookup

The third issue still remains, as `_generate_next_value_()` can be user-overridden and could corrupt the last values list if it were not copied.
Lib/enum.py
Misc/NEWS.d/next/Library/2021-10-12-20-35-06.bpo-45417.gQM-O7.rst [new file with mode: 0644]