]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Speed-up building enums by value, e.g. http.HTTPStatus(200) (GH-11318) (GH-11324)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 26 Dec 2018 20:48:55 +0000 (12:48 -0800)
committerAndrew Svetlov <andrew.svetlov@gmail.com>
Wed, 26 Dec 2018 20:48:55 +0000 (22:48 +0200)
bpo-35585: Speed up enum by-value lookup
(cherry picked from commit 34ae04f74dcf4ac97d07c3e82eaf8f619d80cedb)

Co-authored-by: Andrew Svetlov <andrew.svetlov@gmail.com>
Lib/enum.py
Misc/NEWS.d/next/Library/2018-12-26-02-28-00.bpo-35585.Lkzd3Z.rst [new file with mode: 0644]

index e5a80cd609d498e2ed7a0d960fc012cb09081262..782d37433a6e8e8877fccbf474cf44aa8b3f3150 100644 (file)
@@ -532,8 +532,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 (file)
index 0000000..247a4ae
--- /dev/null
@@ -0,0 +1 @@
+Speed-up building enums by value, e.g. http.HTTPStatus(200).