[Python-checkins] cpython: Close issue23486: performance boost for enum member lookup

ethan.furman python-checkins at python.org
Wed Mar 11 16:43:59 CET 2015


https://hg.python.org/cpython/rev/2545bfe0d273
changeset:   94947:2545bfe0d273
parent:      94945:6e736a57a482
user:        Ethan Furman <ethan at stoneleaf.us>
date:        Wed Mar 11 08:43:12 2015 -0700
summary:
  Close issue23486: performance boost for enum member lookup

files:
  Lib/enum.py |  11 ++++++++++-
  1 files changed, 10 insertions(+), 1 deletions(-)


diff --git a/Lib/enum.py b/Lib/enum.py
--- a/Lib/enum.py
+++ b/Lib/enum.py
@@ -112,6 +112,10 @@
         enum_class._member_map_ = OrderedDict()      # name->value map
         enum_class._member_type_ = member_type
 
+        # save attributes from super classes so we know if we can take
+        # the shortcut of storing members in the class dict
+        base_attributes = {a for b in bases for a in b.__dict__}
+
         # Reverse value->name map for hashable values.
         enum_class._value2member_map_ = {}
 
@@ -165,6 +169,11 @@
             else:
                 # Aliases don't appear in member names (only in __members__).
                 enum_class._member_names_.append(member_name)
+            # performance boost for any member that would not shadow
+            # a DynamicClassAttribute
+            if member_name not in base_attributes:
+                setattr(enum_class, member_name, enum_member)
+            # now add to _member_map_
             enum_class._member_map_[member_name] = enum_member
             try:
                 # This may fail if value is not hashable. We can't add the value
@@ -468,7 +477,7 @@
                 m
                 for cls in self.__class__.mro()
                 for m in cls.__dict__
-                if m[0] != '_'
+                if m[0] != '_' and m not in self._member_map_
                 ]
         return (['__class__', '__doc__', '__module__'] + added_behavior)
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list