Commit f744dd2e authored by Stefan Behnel's avatar Stefan Behnel

Use potentially faster PyObject_Malloc() instead of C malloc() for strides and...

Use potentially faster PyObject_Malloc() instead of C malloc() for strides and format fields in ndarray buffer support.
parent 270b3136
...@@ -18,9 +18,9 @@ DEF _buffer_format_string_len = 255 ...@@ -18,9 +18,9 @@ DEF _buffer_format_string_len = 255
cimport cpython.buffer as pybuf cimport cpython.buffer as pybuf
from cpython.ref cimport Py_INCREF, Py_XDECREF from cpython.ref cimport Py_INCREF, Py_XDECREF
from cpython.mem cimport PyObject_Malloc, PyObject_Free
from cpython.object cimport PyObject from cpython.object cimport PyObject
from cpython.type cimport type from cpython.type cimport type
cimport libc.stdlib as stdlib
cimport libc.stdio as stdio cimport libc.stdio as stdio
cdef extern from "Python.h": cdef extern from "Python.h":
...@@ -243,7 +243,7 @@ cdef extern from "numpy/arrayobject.h": ...@@ -243,7 +243,7 @@ cdef extern from "numpy/arrayobject.h":
if copy_shape: if copy_shape:
# Allocate new buffer for strides and shape info. # Allocate new buffer for strides and shape info.
# This is allocated as one block, strides first. # This is allocated as one block, strides first.
info.strides = <Py_ssize_t*>stdlib.malloc(sizeof(Py_ssize_t) * <size_t>ndim * 2) info.strides = <Py_ssize_t*>PyObject_Malloc(sizeof(Py_ssize_t) * 2 * <size_t>ndim)
info.shape = info.strides + ndim info.shape = info.strides + ndim
for i in range(ndim): for i in range(ndim):
info.strides[i] = PyArray_STRIDES(self)[i] info.strides[i] = PyArray_STRIDES(self)[i]
...@@ -296,7 +296,7 @@ cdef extern from "numpy/arrayobject.h": ...@@ -296,7 +296,7 @@ cdef extern from "numpy/arrayobject.h":
info.format = f info.format = f
return return
else: else:
info.format = <char*>stdlib.malloc(_buffer_format_string_len) info.format = <char*>PyObject_Malloc(_buffer_format_string_len)
info.format[0] = c'^' # Native data types, manual alignment info.format[0] = c'^' # Native data types, manual alignment
offset = 0 offset = 0
f = _util_dtypestring(descr, info.format + 1, f = _util_dtypestring(descr, info.format + 1,
...@@ -306,9 +306,9 @@ cdef extern from "numpy/arrayobject.h": ...@@ -306,9 +306,9 @@ cdef extern from "numpy/arrayobject.h":
def __releasebuffer__(ndarray self, Py_buffer* info): def __releasebuffer__(ndarray self, Py_buffer* info):
if PyArray_HASFIELDS(self): if PyArray_HASFIELDS(self):
stdlib.free(info.format) PyObject_Free(info.format)
if sizeof(npy_intp) != sizeof(Py_ssize_t): if sizeof(npy_intp) != sizeof(Py_ssize_t):
stdlib.free(info.strides) PyObject_Free(info.strides)
# info.shape was stored after info.strides in the same block # info.shape was stored after info.strides in the same block
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment