Skip to content

Commit 3e2bba7

Browse files
authored
Remove context dependency from __hash__ and __eq__ (#1502)
Simplify Stream and Event hash/equality to use pointer comparison only, avoiding expensive context lookups. This follows the cccl-rt principle and fixes issues with wrapping foreign streams where context is unknown. Closes #1480
1 parent 0f2329b commit 3e2bba7

2 files changed

Lines changed: 3 additions & 13 deletions

File tree

cuda_core/cuda/core/_event.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ cdef class Event:
172172
raise RuntimeError(explanation)
173173

174174
def __hash__(self) -> int:
175-
return hash((type(self), as_intptr(self._h_context), as_intptr(self._h_event)))
175+
return hash((type(self), as_intptr(self._h_event)))
176176

177177
def __eq__(self, other) -> bool:
178178
# Note: using isinstance because `Event` can be subclassed.

cuda_core/cuda/core/_stream.pyx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -217,22 +217,12 @@ cdef class Stream:
217217
return (0, as_intptr(self._h_stream))
218218

219219
def __hash__(self) -> int:
220-
# Ensure context is initialized for hash consistency
221-
Stream_ensure_ctx(self)
222-
return hash((as_intptr(self._h_context), as_intptr(self._h_stream)))
220+
return hash(as_intptr(self._h_stream))
223221

224222
def __eq__(self, other) -> bool:
225223
if not isinstance(other, Stream):
226224
return NotImplemented
227-
cdef Stream _other = <Stream>other
228-
# Fast path: compare handles first
229-
if as_intptr(self._h_stream) != as_intptr(_other._h_stream):
230-
return False
231-
# Ensure contexts are initialized for both streams
232-
Stream_ensure_ctx(self)
233-
Stream_ensure_ctx(_other)
234-
# Compare contexts as well
235-
return as_intptr(self._h_context) == as_intptr(_other._h_context)
225+
return as_intptr(self._h_stream) == as_intptr((<Stream>other)._h_stream)
236226

237227
@property
238228
def handle(self) -> cuda.bindings.driver.CUstream:

0 commit comments

Comments
 (0)