Commit ed516fb9 authored by Mark Florisson's avatar Mark Florisson

Avoid with gil blocks in memoryview code (for performance)

parent 8f6abe21
...@@ -709,15 +709,13 @@ cdef int slice_memviewslice({{memviewslice_name}} *src, ...@@ -709,15 +709,13 @@ cdef int slice_memviewslice({{memviewslice_name}} *src,
if start < 0: if start < 0:
start += shape start += shape
if not 0 <= start < shape: if not 0 <= start < shape:
with gil: _err_dim(IndexError, "Index out of bounds (axis %d)", dim)
raise IndexError("Index out of bounds (axis %d)")
else: else:
# index is a slice # index is a slice
negative_step = have_step != 0 and step < 0 negative_step = have_step != 0 and step < 0
if have_step and step == 0: if have_step and step == 0:
with gil: _err_dim(ValueError, "Step may not be zero (axis %d)", dim)
raise ValueError("Step may not be zero (axis %d)" % dim)
# check our bounds and set defaults # check our bounds and set defaults
if have_start: if have_start:
...@@ -778,9 +776,8 @@ cdef int slice_memviewslice({{memviewslice_name}} *src, ...@@ -778,9 +776,8 @@ cdef int slice_memviewslice({{memviewslice_name}} *src,
if new_ndim == 0: if new_ndim == 0:
dst.data = (<char **> dst.data)[0] + suboffset dst.data = (<char **> dst.data)[0] + suboffset
else: else:
with gil: _err_dim(IndexError, "All dimensions preceding dimension %d "
raise IndexError("All dimensions preceding dimension %d " "must be indexed and not sliced", dim)
"must be indexed and not sliced" % dim)
else: else:
suboffset_dim[0] = new_ndim suboffset_dim[0] = new_ndim
...@@ -837,8 +834,7 @@ cdef int transpose_memslice({{memviewslice_name}} *memslice) nogil except 0: ...@@ -837,8 +834,7 @@ cdef int transpose_memslice({{memviewslice_name}} *memslice) nogil except 0:
shape[i], shape[j] = shape[j], shape[i] shape[i], shape[j] = shape[j], shape[i]
if memslice.suboffsets[i] >= 0 or memslice.suboffsets[j] >= 0: if memslice.suboffsets[i] >= 0 or memslice.suboffsets[j] >= 0:
with gil: _err(ValueError, "Cannot transpose memoryview with indirect dimensions")
raise ValueError("Cannot transpose memoryview with indirect dimensions")
return 1 return 1
...@@ -1082,8 +1078,7 @@ cdef void *copy_data_to_temp({{memviewslice_name}} *src, ...@@ -1082,8 +1078,7 @@ cdef void *copy_data_to_temp({{memviewslice_name}} *src,
result = malloc(size) result = malloc(size)
if not result: if not result:
with gil: _err(MemoryError, NULL)
raise MemoryError
# tmpslice[0] = src # tmpslice[0] = src
tmpslice.data = <char *> result tmpslice.data = <char *> result
...@@ -1107,6 +1102,25 @@ cdef void *copy_data_to_temp({{memviewslice_name}} *src, ...@@ -1107,6 +1102,25 @@ cdef void *copy_data_to_temp({{memviewslice_name}} *src,
return result return result
# Use 'with gil' functions and avoid 'with gil' blocks, as the code within the blocks
# has temporaries that need the GIL to clean up
@cname('__pyx_memoryview_err_extents')
cdef int _err_extents(int i, Py_ssize_t extent1,
Py_ssize_t extent2) except -1 with gil:
raise ValueError("got differing extents in dimension %d (got %d and %d)" %
(i, extent1, extent2))
@cname('__pyx_memoryview_err_dim')
cdef int _err_dim(object error, char *msg, int dim) except -1 with gil:
raise error(msg % dim)
@cname('__pyx_memoryview_err')
cdef int _err(object error, char *msg) except -1 with gil:
if msg != NULL:
raise error(msg)
else:
raise error
@cname('__pyx_memoryview_copy_contents') @cname('__pyx_memoryview_copy_contents')
cdef int memoryview_copy_contents({{memviewslice_name}} src, cdef int memoryview_copy_contents({{memviewslice_name}} src,
{{memviewslice_name}} dst, {{memviewslice_name}} dst,
...@@ -1136,14 +1150,10 @@ cdef int memoryview_copy_contents({{memviewslice_name}} src, ...@@ -1136,14 +1150,10 @@ cdef int memoryview_copy_contents({{memviewslice_name}} src,
broadcasting = True broadcasting = True
src.strides[i] = 0 src.strides[i] = 0
else: else:
with gil: _err_extents(i, dst.shape[i], src.shape[i])
raise ValueError(
"got differing extents in dimension %d "
"(got %d and %d)" % (i, dst.shape[i], src.shape[i]))
if src.suboffsets[i] >= 0: if src.suboffsets[i] >= 0:
with gil: _err_dim(ValueError, "Dimension %d is not direct", i)
raise ValueError("Dimension %d is not direct" % i)
if slices_overlap(&src, &dst, ndim, itemsize): if slices_overlap(&src, &dst, ndim, itemsize):
# slices overlap, copy to temp, copy temp to dst # slices overlap, copy to temp, copy temp to dst
......
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