NVIDIA / NVIDIA/cuda-python

[PERF]: Remove redundant type checks

Aperta
#1,639 2 commenti 0 reazioni 1 assegnatario Vedi su GitHub

@mdboom ci sta già lavorando.

Dal 18/2/2026.

cuda.bindings performance
Lingua principale
Cython
Stelle
3.4k
Fork
329
Merge medio
1g 23h
PR unite (30g)
116

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.