Skip to content

Speed up Flag bitwise operations (|, &, ^) #150817

@gaborbernat

Description

@gaborbernat

Combining Flag members with |, & and ^ always walks both operands to check that neither carries a None value, raising a clear TypeError when one does. That check only matters in the rare case where a value really is None; for every normal combination of two valid flags it adds work to each operation.

Flag arithmetic is hot wherever flags model option sets. The re module's own flags are an IntFlag, so every re.compile(pattern, re.I | re.M) combines flags; permission systems, feature toggles and styling layers do the same in tight loops.

Running flag combinations mined from the top-1000 corpus, including the re flag sets, takes 72.6 µs today and 49.5 µs when the guard runs only when a value is actually None, 47% faster. The TypeError and its message are unchanged for the invalid cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directorytopic-regex
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions