[Python-checkins] Speed-up building enums by value, e.g. http.HTTPStatus(200) (GH-11318) (GH-11324)

Andrew Svetlov webhook-mailer at python.org
Wed Dec 26 15:48:58 EST 2018


https://github.com/python/cpython/commit/705b5998035739b1794a862123d3dc6e339a14d0
commit: 705b5998035739b1794a862123d3dc6e339a14d0
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Andrew Svetlov <andrew.svetlov at gmail.com>
date: 2018-12-26T22:48:55+02:00
summary:

Speed-up building enums by value, e.g. http.HTTPStatus(200) (GH-11318) (GH-11324)

bpo-35585: Speed up enum by-value lookup
(cherry picked from commit 34ae04f74dcf4ac97d07c3e82eaf8f619d80cedb)

Co-authored-by: Andrew Svetlov <andrew.svetlov at gmail.com>

files:
A Misc/NEWS.d/next/Library/2018-12-26-02-28-00.bpo-35585.Lkzd3Z.rst
M Lib/enum.py

diff --git a/Lib/enum.py b/Lib/enum.py
index e5a80cd609d4..782d37433a6e 100644
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -532,8 +532,10 @@ def __new__(cls, value):
         # 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 000000000000..247a4ae6800f
--- /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).



More information about the Python-checkins mailing list