File tree Expand file tree Collapse file tree 1 file changed +6
-8
lines changed
cuda_core/cuda/core/experimental Expand file tree Collapse file tree 1 file changed +6
-8
lines changed Original file line number Diff line number Diff line change @@ -45,17 +45,15 @@ cdef class Context:
4545 True if other is a Context wrapping the same handle, False if not equal,
4646 NotImplemented if other is not a Context.
4747 """
48- # Use cast with exception handling instead of isinstance(other, Context)
48+ # Use type identity check + cast with exception handling instead of isinstance(other, Context)
4949 # for performance: isinstance with cdef classes must traverse inheritance
5050 # hierarchies via Python's type checking mechanism, even when other is
51- # already a Context. In contrast, a direct cast succeeds immediately in
52- # the common case (other is a Context), and exception handling has very
53- # low overhead when no exception occurs.
54- cdef Context _other
55- try :
56- _other = < Context> other
57- except TypeError :
51+ # already a Context. A type identity check (type(other) is Context) is very fast
52+ # and catches non-Context types before attempting the cast, preventing potential
53+ # segfaults from unsafe casts.
54+ if type (other) is not Context:
5855 return NotImplemented
56+ cdef Context _other = < Context> other
5957 return int (self ._handle) == int (_other._handle)
6058
6159 def __hash__ (self ) -> int:
You can’t perform that action at this time.
0 commit comments