Skip to content

[PERF]: Remove redundant type checks #1639

Open

Description

@mdboom

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.

Activity

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

    @mdboom
    ContributorAuthor

    This would be best done after the cython-gen/cybind merge.

  3. mdboom commented on Jun 23, 2026

    @mdboom
    ContributorAuthor

    Moving to P2 -- there's not much evidence this would have a major impact, so it's just "nice to have".

  4. removed
    P2Low priority - Nice to have
    on Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions