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.

Author Antony.Lee
Recipients Antony.Lee
Date 2019-09-06.12:18:00
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1567772280.73.0.782923595007.issue38045@roundup.psfhosted.org>
In-reply-to
Content
Consider the following example

    from enum import Flag
    F = Flag("F", list("abcdefghijklm"))
    for idx in range(2**len(F) - 1):
        F(idx)

creating all possible combos of 13 flags, so 8192 instances (yes, I know the instances are cached later, but the initial cost is still significant).  Locally, this takes over 4.5s to execute; profiling shows that ~30% of that time is spent executing enum._is_power_of_two(!):

    def _power_of_two(value):
        if value < 1:
            return False
        return value == 2 ** _high_bit(value)

Indeed, replacing the implementation of _power_of_two by

    import math
    _powers_of_two = {
        2 ** i for i in range(math.ceil(math.log2(sys.maxsize)) + 1)}
    def _power_of_two(value):
        return (value in _powers_of_two if value <= sys.maxsize
                else value == 2 ** _high_bit(value))

shaves off ~30% of the runtime (obviously this won't help for huge (>sys.maxsize) flag values, but these are rarer I would think).

An even better fix, I think, would be for Flag to cache `flags_to_check` (updating it at the same time as `_value2member_map_`) instead of recomputing it each time in _decompose

    flags_to_check = [
            (m, v)
            for v, m in list(flag._value2member_map_.items())
            if m.name is not None or _power_of_two(v)
            ]

as this actually needlessly introduces quadratic complexity when iterating over all possible combos (because the size of _value2member_map_ is growing).  (Also, it's not actually clear to me how one can end up with unnamed power-of-two instances in _value2member_map?)
History
Date User Action Args
2019-09-06 12:18:00Antony.Leesetrecipients: + Antony.Lee
2019-09-06 12:18:00Antony.Leesetmessageid: <1567772280.73.0.782923595007.issue38045@roundup.psfhosted.org>
2019-09-06 12:18:00Antony.Leelinkissue38045 messages
2019-09-06 12:18:00Antony.Leecreate