This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Increase Enum performance
Type: performance Stage: needs patch
Components: Library (Lib) Versions: Python 3.10
Status: open Resolution:
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: MrMrRobat, barry, eli.bendersky, ethan.furman, jack1142, levkivskyi, methane
Priority: normal Keywords: patch

Created on 2019-12-20 02:48 by MrMrRobat, last changed 2022-04-11 14:59 by admin.

File name Uploaded Description Edit
benchmark_result.txt MrMrRobat, 2019-12-20 02:48 Benchmark-comparison
Pull Requests
URL Status Linked Edit
PR 17669 open MrMrRobat, 2019-12-20 03:01
Messages (2)
msg358694 - (view) Author: Arseny Boykov (MrMrRobat) * Date: 2019-12-20 02:48
Now enum has very poor speed on trying values and attributes access (especially when it comes to accessing members name/value attrs)

There are two major reasons why attrs access is slow:
  - All values/names access going through DynamicClassAttribute (x10 slower than accessing attr directly)
  - EnumMeta has __getattr__ which is slowing down even direct class attributes access (up to x6 slower)

However, there are no need to use it, as we could just set value and name to newly created enum members without affecting its class.

The main issue with values check is the slow _missing_ hook handling when it raising exception, which happens pretty much always, if value is not valid enum and we talking about vanilla Enum class.

Also I found Flag performance issue being fixed already:
It's also related, because new Flag creation involves many lookups

My proposal:
  - I think we should completely get rid of __getattr__ on Enum (~6x speed boost)
  - Rework DynamicClassAttribute so it could work without __getattr__ or perhaps completely get rid of it
  - Don't use DynamicClassAttribute for and .value (~10x speed boost)
  - Think of faster handling of _missing_ hook  (~2x speed boost)
  - Make other improvements to the code

Proposed changes doesn't require changing public API or behaviour.

By far I were able to implement almost things proposed here and will be happy to make a PR.
msg358742 - (view) Author: Arseny Boykov (MrMrRobat) * Date: 2019-12-20 22:59
Also, do we need to leave compatibility with python <3.8? 
If not, we could use the fact that python 3.8 dicts and sets which are preserve order to speed things up even more. Also I'd replace % string formatting with f-strings, as they also faster.

And another thing to think about: maybe we can calculate values returned by __str__, __repr__ and __invert__ once on member creation, since they not supposed to change during its life?

For example __invert__ on Flag does a lot of work on every call:
    def __invert__(self):
        cls = self.__class__
        members, uncovered = _decompose(cls, self._value_)
        inverted = cls(0)
        for m in cls:
            if m not in members and not (m._value_ & self._value_):
                inverted = inverted | m
        return cls(inverted)
Date User Action Args
2022-04-11 14:59:24adminsetgithub: 83283
2021-04-11 15:41:25ethan.furmansetstage: patch review -> needs patch
versions: + Python 3.10, - Python 3.9
2020-08-30 15:33:53jack1142setnosy: + jack1142
2020-08-09 12:52:36methanesetnosy: + methane
2020-08-08 19:52:43pablogsalsetnosy: - pablogsal
2019-12-26 21:55:44pablogsalsetnosy: + pablogsal
2019-12-20 22:59:56MrMrRobatsetmessages: + msg358742
2019-12-20 21:46:21terry.reedysetversions: - Python 3.6, Python 3.7, Python 3.8
2019-12-20 18:56:37levkivskyisetnosy: + levkivskyi
2019-12-20 06:18:03ethan.furmansetassignee: ethan.furman
2019-12-20 04:07:29xtreaksetnosy: + barry, eli.bendersky, ethan.furman
2019-12-20 03:01:57MrMrRobatsetkeywords: + patch
stage: patch review
pull_requests: + pull_request17133
2019-12-20 02:48:28MrMrRobatcreate