Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-39102: Increase Enum performance up to 10x times (3x average) #17669

Open
wants to merge 15 commits into
base: master
from

Conversation

@MrMrRobat
Copy link

MrMrRobat commented Dec 20, 2019

Increase Enum performance

https://bugs.python.org/issue39102

Partial benchmark result, to see more, check full benchmark result and source code

ATTR_ACCESS:
Testing with 1000000 repeats:

    >>> OldEnum['foo']  # 0.663708519 seconds
    <OldEnum.foo: 1>

    >>> NewEnum['foo']  # 0.32571259499999994 seconds
    <NewEnum.foo: 1>

    >>> OldEnum.foo  # 0.16809049799999998 seconds
    <OldEnum.foo: 1>

    >>> NewEnum.foo  # 0.06393614900000011 seconds
    <NewEnum.foo: 1>

    >>> OldEnum.foo.value  # 0.6732232629999999 seconds
    1

    >>> NewEnum.foo.value  # 0.1533165799999998 seconds
    1

    >>> OldEnum.value.value  # 1.9447311319999998 seconds
    3

    >>> NewEnum.value.value  # 0.1025070509999999 seconds
    3

    >>> try:     OldEnum.value.value = 'new' except: pass  # 2.048566492 seconds
    AttributeError("can't set attribute")

    >>> try:     NewEnum.value.value = 'new' except: pass  # 0.596396082 seconds
    AttributeError("Can't set attribute")

NewEnum: total: 1.2418685, average: 0.1811723 (Fastest)
OldEnum: total: 5.4983199, average: 0.7855935, ~ x4.34 times slower than NewEnum

Full benchmark result and source code

ToDo:

  • Add tests for changes

Also, does compatibility with python <3.6 need to be maintained?
If not, we could use type annotations for name and values attrs. (However I wasn't able to import typing.Any in enum module due to circular import)
Ok, I see that there are no type annotations in stdlib at all, but the question is still valid, because if compatibility doesn't need to be maintained, we could use the fact that python 3.8 dicts and sets which are preserve order to speed things up even more.

https://bugs.python.org/issue39102

@MrMrRobat MrMrRobat requested a review from ethanfurman as a code owner Dec 20, 2019
@the-knights-who-say-ni

This comment has been minimized.

Copy link

the-knights-who-say-ni commented Dec 20, 2019

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

CLA Missing

Our records indicate the following people have not signed the CLA:

@MrMrRobat

For legal reasons we need all the people listed to sign the CLA before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

If you have recently signed the CLA, please wait at least one business day
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

MrMrRobat and others added 2 commits Dec 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.