Skip to content

[PERF]: Use a list for lookup in contiguous FastEnums #1637

Description

@mdboom

Our FastEnum implementation (and our usage of stdlib.enum.IntEnum before that) uses a dictionary from integers to a singleton enum value to return an enum from C (which is a simple int) to Python (which is a value/name pair of a specific type).

For enums where all of the values are contiguous starting at zero, (which we could determine at code-generation time) we could instead use a list for this mapping which would provide O(1) lookup.

Activity

  1. added this to the cuda.bindings 13.2 milestone on Feb 18, 2026
  2. leofang commented on Feb 18, 2026

    @leofang
    Member

    For enums where all of the values are contiguous starting at zero

    We'll need to ensure the codegen checks for this. In many cases library teams like to leave holes lol

  3. mdboom commented on Feb 19, 2026

    @mdboom
    ContributorAuthor

    For enums where all of the values are contiguous starting at zero

    We'll need to ensure the codegen checks for this. In many cases library teams like to leave holes lol

    Oh, yes, indeed. Many are contiguous, but a lot of them aren't. And a lot are "bit flags", too.

    No measurable improvement with this change, so closing.

    enum from int: Mean +- std dev: [baseline] 167 ns +- 7 ns -> [contiguous] 178 ns +- 9 ns: 1.06x slower
    enum value: Mean +- std dev: [baseline] 80.1 ns +- 3.8 ns -> [contiguous] 81.7 ns +- 2.7 ns: 1.02x slower
    cuda.bindings.runtime import: Mean +- std dev: [baseline] 63.6 ms +- 1.3 ms -> [contiguous] 62.4 ms +- 1.2 ms: 1.02x faster
    cuda.bindings.nvrtc import: Mean +- std dev: [baseline] 56.3 ms +- 1.7 ms -> [contiguous] 55.1 ms +- 1.3 ms: 1.02x faster
    
    Benchmark hidden because not significant (3): int from enum, isinstance(enum, IntEnum), cuda.bindings.driver import
    
    Geometric mean: 1.01x slower
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions