From c8e9bc7a4683c9b4cd1b82f9bbd21235596ed09a Mon Sep 17 00:00:00 2001 From: Andy Jost Date: Thu, 15 Jan 2026 10:32:27 -0800 Subject: [PATCH] Remove context dependency from __hash__ and __eq__ 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 --- cuda_core/cuda/core/_event.pyx | 2 +- cuda_core/cuda/core/_stream.pyx | 14 ++------------ 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/cuda_core/cuda/core/_event.pyx b/cuda_core/cuda/core/_event.pyx index 0d25959a22..ec9f41c004 100644 --- a/cuda_core/cuda/core/_event.pyx +++ b/cuda_core/cuda/core/_event.pyx @@ -172,7 +172,7 @@ cdef class Event: raise RuntimeError(explanation) def __hash__(self) -> int: - return hash((type(self), as_intptr(self._h_context), as_intptr(self._h_event))) + return hash((type(self), as_intptr(self._h_event))) def __eq__(self, other) -> bool: # Note: using isinstance because `Event` can be subclassed. diff --git a/cuda_core/cuda/core/_stream.pyx b/cuda_core/cuda/core/_stream.pyx index 05cbcce76a..2b5cdfabd2 100644 --- a/cuda_core/cuda/core/_stream.pyx +++ b/cuda_core/cuda/core/_stream.pyx @@ -217,22 +217,12 @@ cdef class Stream: return (0, as_intptr(self._h_stream)) def __hash__(self) -> int: - # Ensure context is initialized for hash consistency - Stream_ensure_ctx(self) - return hash((as_intptr(self._h_context), as_intptr(self._h_stream))) + return hash(as_intptr(self._h_stream)) def __eq__(self, other) -> bool: if not isinstance(other, Stream): return NotImplemented - cdef Stream _other = other - # Fast path: compare handles first - if as_intptr(self._h_stream) != as_intptr(_other._h_stream): - return False - # Ensure contexts are initialized for both streams - Stream_ensure_ctx(self) - Stream_ensure_ctx(_other) - # Compare contexts as well - return as_intptr(self._h_context) == as_intptr(_other._h_context) + return as_intptr(self._h_stream) == as_intptr((other)._h_stream) @property def handle(self) -> cuda.bindings.driver.CUstream: