Based on python/cpython#17669 and python/cpython#16483
Patch for stdlib enum that makes it fast.
- ~10x faster
name/valueaccess - ~6x faster access to enum members
- ~2x faster values positive check
- ~3x faster values negative check
- ~3x faster iteration
- ~100x faster new
FlagsandIntFlagscreation for Python 3.8 and below
First, install it from PyPi using pip
pip install f-enumor using poetry
poetry add f-enumThen enable the patch just by calling fastenum.enable() once at the start of your programm:
import fastenum
fastenum.enable()You don't need to re-apply patch across different modules: once it's enabled, it'll work everywhere.
fastenum is designed to give effortless boost for all enums from stdlib. That means that none of optimizations should break existing code, thus requiring no changes other than installing and activating the library.
Here are summary of internal changes:
- Optimized
Enum.__new__ - Remove
EnumMeta.__getattr__ - Store
Enum.nameand.valuein members__dict__for faster access - Replace
Enum._member_names_with._unique_member_map_for faster lookups and iteration (old arg still remains) - Replace
_EmumMeta._member_namesand._last_valueswith.membersmapping (old args still remain) - Add support for direct setting and getting class attrs on
DynamicClassAttributewithout need to use slow__getattr__ - Various minor improvements