Commit 036637f1 authored by Stefan Behnel's avatar Stefan Behnel

Remove a tiny bit of time overhead in mockbuffer test support.

parent ddc9a18c
...@@ -31,11 +31,12 @@ cdef class MockBuffer: ...@@ -31,11 +31,12 @@ cdef class MockBuffer:
def __init__(self, label, data, shape=None, strides=None, format=None, writable=True, offset=0): def __init__(self, label, data, shape=None, strides=None, format=None, writable=True, offset=0):
# It is important not to store references to data after the constructor # It is important not to store references to data after the constructor
# as refcounting is checked on object buffers. # as refcounting is checked on object buffers.
cdef Py_ssize_t x, s, cumprod, itemsize
self.label = label self.label = label
self.release_ok = True self.release_ok = True
self.log = "" self.log = ""
self.offset = offset self.offset = offset
self.itemsize = self.get_itemsize() self.itemsize = itemsize = self.get_itemsize()
self.writable = writable self.writable = writable
if format is None: format = self.get_default_format() if format is None: format = self.get_default_format()
if shape is None: shape = (len(data),) if shape is None: shape = (len(data),)
...@@ -48,7 +49,7 @@ cdef class MockBuffer: ...@@ -48,7 +49,7 @@ cdef class MockBuffer:
strides.append(cumprod) strides.append(cumprod)
cumprod *= s cumprod *= s
strides.reverse() strides.reverse()
strides = [x * self.itemsize for x in strides] strides = [x * itemsize for x in strides]
suboffsets = [-1] * len(shape) suboffsets = [-1] * len(shape)
datashape = [len(data)] datashape = [len(data)]
p = data p = data
...@@ -61,8 +62,10 @@ cdef class MockBuffer: ...@@ -61,8 +62,10 @@ cdef class MockBuffer:
self.ndim = <int>len(datashape) self.ndim = <int>len(datashape)
shape = datashape shape = datashape
self.buffer = self.create_indirect_buffer(data, shape) self.buffer = self.create_indirect_buffer(data, shape)
suboffsets = [0] * (self.ndim-1) + [-1] suboffsets = [0] * self.ndim
strides = [sizeof(void*)] * (self.ndim-1) + [self.itemsize] suboffsets[-1] = -1
strides = [sizeof(void*)] * self.ndim
strides[-1] = itemsize
self.suboffsets = self.list_to_sizebuf(suboffsets) self.suboffsets = self.list_to_sizebuf(suboffsets)
else: else:
# strided and/or simple access # strided and/or simple access
...@@ -75,7 +78,7 @@ cdef class MockBuffer: ...@@ -75,7 +78,7 @@ cdef class MockBuffer:
except AttributeError: except AttributeError:
pass pass
self.format = format self.format = format
self.len = len(data) * self.itemsize self.len = len(data) * itemsize
self.strides = self.list_to_sizebuf(strides) self.strides = self.list_to_sizebuf(strides)
self.shape = self.list_to_sizebuf(shape) self.shape = self.list_to_sizebuf(shape)
...@@ -119,6 +122,7 @@ cdef class MockBuffer: ...@@ -119,6 +122,7 @@ cdef class MockBuffer:
return buf return buf
cdef Py_ssize_t* list_to_sizebuf(self, l): cdef Py_ssize_t* list_to_sizebuf(self, l):
cdef Py_ssize_t i, x
cdef size_t n = <size_t>len(l) * sizeof(Py_ssize_t) cdef size_t n = <size_t>len(l) * sizeof(Py_ssize_t)
cdef Py_ssize_t* buf = <Py_ssize_t*>stdlib.malloc(n) cdef Py_ssize_t* buf = <Py_ssize_t*>stdlib.malloc(n)
for i, x in enumerate(l): for i, x in enumerate(l):
......
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