NVIDIA / NVIDIA/cuda-python

[PERF]: Remove redundant type checks

Open
#1,639 2 comments 0 reactions 1 assignee View on GitHub

@mdboom is already working on this.

Since Feb 18, 2026.

cuda.bindings performance
Dominant language
Cython
Stars
3.4k
Forks
329
Avg merge
1d 23h
Merged PRs (30d)
116

Description

When an argument to a Cython function is declared as not None, this adds a check early on that the argument is not None. However, in almost all cases, that check is performed again implicitly when it is converted to our desired type later in the function. Since they are far apart, the branch predictor or compiler generally doesn't optimize this out, and it's just unnecessary extra work. For a concrete example:

def cuTensorMapEncodeTiled(..., tensorDataType not None : CUtensorMapDataType, ...):
    ...
    cdef CUtensorMapDataType = int(tensorDataType)

This inserts a check that it isn't None shortly after unpacking the arguments:

if (unlikely(((PyObject *)__pyx_v_tensorDataType) == Py_None)) {
    PyErr_Format(PyExc_TypeError, "Argument '%.200s' must not be None", "tensorDataType"); __PYX_ERR(0, 44522, __pyx_L1_error)
  }

However, this "None check" is implied when converting the value to an int because None is not convertible to an int.

We can remove these annotations without any change in safety or accepted types. The downside is that the error message may not be as nice or mention the name of the argument. If we put a try/except around the integer conversion, we may be able to have 100% equivalent behavior, but the performance implications of that would need to be measured.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.