From 34ae04f74dcf4ac97d07c3e82eaf8f619d80cedb Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Wed, 26 Dec 2018 20:45:33 +0200 Subject: Speed-up building enums by value, e.g. http.HTTPStatus(200) (#11318) bpo-35585: Speed up enum by-value lookup --- Lib/enum.py | 6 ++++-- Misc/NEWS.d/next/Library/2018-12-26-02-28-00.bpo-35585.Lkzd3Z.rst | 1 + 2 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2018-12-26-02-28-00.bpo-35585.Lkzd3Z.rst diff --git a/Lib/enum.py b/Lib/enum.py index fec1aed..f7452f0 100644 --- a/Lib/enum.py +++ b/Lib/enum.py @@ -563,8 +563,10 @@ class Enum(metaclass=EnumMeta): # by-value search for a matching enum member # see if it's in the reverse mapping (for hashable values) try: - if value in cls._value2member_map_: - return cls._value2member_map_[value] + return cls._value2member_map_[value] + except KeyError: + # Not found, no need to do long O(n) search + pass except TypeError: # not there, now do long search -- O(n) behavior for member in cls._member_map_.values(): diff --git a/Misc/NEWS.d/next/Library/2018-12-26-02-28-00.bpo-35585.Lkzd3Z.rst b/Misc/NEWS.d/next/Library/2018-12-26-02-28-00.bpo-35585.Lkzd3Z.rst new file mode 100644 index 0000000..247a4ae --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-12-26-02-28-00.bpo-35585.Lkzd3Z.rst @@ -0,0 +1 @@ +Speed-up building enums by value, e.g. http.HTTPStatus(200). -- cgit v0.12