Skip to content

[PERF]: Handle generic sequences more efficiently #1640

Description

@mdboom

In driver, runtime and nvrtc, most of the sequence arguments are of a type like Optional[tuple[cuuint64_t] | list[cuuint64_t]].

They are then converted to C arrays using code like:

for idx in range(elementStridesLen):
            elementStridesStatic[idx] = <cydriver.cuuint32_t>(<cuuint32_t?> elementStrides[idx])._pvt_ptr[0]

elementStrides[idx] here uses the generic PySequence_GetItem, which first must check whether the sequence is a list or tuple before getting the item. This check is performed repeatedly for each item, even though the type of the sequence remains constant. The branch predictor may or may not be able to smooth this out.

Ideally, we would use the PySequence_Fast family of functions. Unfortunately, Cython's wrappers of low-level Python/C API functions involving borrowing are broken -- we would probably need to revert to raw C snippets to make that work.

Alternatively, we could do something like:

if type(seq) is list:
    for idx in range(len(seq)):
        # use cpython.PyList_GetItemInt...
elif type(seq) is tuple:
    for idx in range(len(seq)):
        # use cpython.PyTuple_GetItemInt...

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1Medium priority - Should docuda.bindingsEverything related to the cuda.bindings moduleperformance

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions