[New-bugs-announce] [issue39102] Increase Enum performance

Arseny Boykov report at bugs.python.org
Thu Dec 19 21:48:28 EST 2019

New submission from Arseny Boykov <appkiller16 at gmail.com>:

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 member.name 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 member.name 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.

components: Library (Lib)
files: benchmark_result.txt
messages: 358694
nosy: MrMrRobat
priority: normal
severity: normal
status: open
title: Increase Enum performance
type: performance
versions: Python 3.6, Python 3.7, Python 3.8, Python 3.9
Added file: https://bugs.python.org/file48793/benchmark_result.txt

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list