From 06ef25e8f4e88adea8053e843c40cd976cc05212 Mon Sep 17 00:00:00 2001 From: John Kirkham Date: Fri, 23 Nov 2018 18:22:42 -0500 Subject: [PATCH] Skip creating a `memoryview` from `obj` At this stage, there is no real value to creating a `memoryview` from our data to create an object vs. just using the buffer protocol to fill out our buffer. Under the hood, the `PyMemoryView_FromObject` call is doing exactly what we are doing and providing little more. Filling out the buffer directly avoids creating an intermediate object between our buffer and the data. Not to mention this should make it easier for us to get access to the underlying object used to create the buffer. Also should cutdown on the overhead of generating a `cybuffer` object. --- src/cybuffer.pyx | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/cybuffer.pyx b/src/cybuffer.pyx index 2315469..785cff4 100644 --- a/src/cybuffer.pyx +++ b/src/cybuffer.pyx @@ -35,8 +35,6 @@ include "version.pxi" cdef extern from "Python.h": size_t Py_UNICODE_SIZE - object PyMemoryView_FromObject(object obj) - cdef extern from *: """ @@ -145,8 +143,7 @@ cdef class cybuffer(object): else: raise TypeError("Unable to get buffer protocol API for `data`.") - # Create a buffer based on memoryview - data_buf = PyMemoryView_FromObject(data_buf) + # Fill out our buffer based on the data cpython.buffer.PyObject_GetBuffer(data_buf, &self._buf, PyBUF_FULL_RO) # Allocate and/or initialize metadata for casting @@ -155,7 +152,7 @@ cdef class cybuffer(object): self._shape = self._buf.shape self._strides = self._buf.strides - # Figure out whether the memoryview is contiguous + # Figure out whether the data is contiguous self.c_contiguous = cpython.buffer.PyBuffer_IsContiguous( &self._buf, b'C' )