Skip to content

[PERF]: Faster void * conversion#1616

Open
mdboom wants to merge 4 commits intoNVIDIA:mainfrom
mdboom:faster-conversion
Open

[PERF]: Faster void * conversion#1616
mdboom wants to merge 4 commits intoNVIDIA:mainfrom
mdboom:faster-conversion

Conversation

@mdboom
Copy link
Contributor

@mdboom mdboom commented Feb 12, 2026

We currently accept an int, CUdeviceptr, or a buffer-providing object as convertible to a void *. This is currently handled with a class _HelperInputVoidPtr, which mainly exists to manage the lifetime when the input exposes a buffer.

This object (like all PyObjects) is allocated on the heap and gets free'd implicitly by Cython at the end of the function. Since it only exists to manage lifetimes when the object exposes a buffer, we pay this heap allocation penalty even in the common case where the input is a simple integer.

This changes the code to statically allocate the Py_buffer on the stack, and so is faster for similar reasons to #1545. This means we are trading some stack space (88 bytes) for speed. But given that CUDA Python API calls can't recursively call themselves, I'm not concerned.

This improves the overhead time in the benchmark in #659 from 2.97us/call to 2.67us/call.

The old _HelperInputVoidPtr class stays around here because it is still useful when the input is a list of void *-convertible things and we can't statically determine how much space to allocate.

@copy-pr-bot
Copy link
Contributor

copy-pr-bot bot commented Feb 12, 2026

Auto-sync is disabled for ready for review pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@mdboom
Copy link
Contributor Author

mdboom commented Feb 12, 2026

/ok to test

@github-actions
Copy link

Comment on lines -137 to -138
elif isinstance(ptr, (_driver["CUdeviceptr"])):
self._cptr = <void*><void_ptr>int(ptr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: This path seems to be gone?

cdef void * _helper_input_void_ptr(ptr, _HelperInputVoidPtrStruct *buffer)

cdef inline void * _helper_input_void_ptr_free(_HelperInputVoidPtrStruct *helper):
if helper[0]._pybuffer.buf != NULL:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Should we check first if helper is NULL?

self._cptr = NULL
elif isinstance(ptr, (int)):
# Easy run, user gave us an already configured void** address
try:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we can avoid code duplication by replacing the try-except block with a call to the new helper like this?

self._cptr = _helper_input_void_ptr(ptr, <_HelperInputVoidPtrStruct*><PyObject*>self)

(I'm not so sure about the self casting, I think it's correct because they share the same layout.)

@leofang leofang added this to the cuda.bindings 13.1.2 & 12.9.6 milestone Feb 14, 2026
@leofang leofang added enhancement Any code-related improvements cuda.bindings Everything related to the cuda.bindings module P1 Medium priority - Should do labels Feb 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cuda.bindings Everything related to the cuda.bindings module enhancement Any code-related improvements P1 Medium priority - Should do

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants