Commit e24d2904 authored by Stefan Behnel's avatar Stefan Behnel

Sprinkle "unlikely()" branch hints in all places where the memory view C code raises exceptions.

parent 441187e9
...@@ -181,13 +181,13 @@ __pyx_check_strides(Py_buffer *buf, int dim, int ndim, int spec) ...@@ -181,13 +181,13 @@ __pyx_check_strides(Py_buffer *buf, int dim, int ndim, int spec)
if (buf->strides) { if (buf->strides) {
if (spec & __Pyx_MEMVIEW_CONTIG) { if (spec & __Pyx_MEMVIEW_CONTIG) {
if (spec & (__Pyx_MEMVIEW_PTR|__Pyx_MEMVIEW_FULL)) { if (spec & (__Pyx_MEMVIEW_PTR|__Pyx_MEMVIEW_FULL)) {
if (buf->strides[dim] != sizeof(void *)) { if (unlikely(buf->strides[dim] != sizeof(void *))) {
PyErr_Format(PyExc_ValueError, PyErr_Format(PyExc_ValueError,
"Buffer is not indirectly contiguous " "Buffer is not indirectly contiguous "
"in dimension %d.", dim); "in dimension %d.", dim);
goto fail; goto fail;
} }
} else if (buf->strides[dim] != buf->itemsize) { } else if (unlikely(buf->strides[dim] != buf->itemsize)) {
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"Buffer and memoryview are not contiguous " "Buffer and memoryview are not contiguous "
"in the same dimension."); "in the same dimension.");
...@@ -199,7 +199,7 @@ __pyx_check_strides(Py_buffer *buf, int dim, int ndim, int spec) ...@@ -199,7 +199,7 @@ __pyx_check_strides(Py_buffer *buf, int dim, int ndim, int spec)
Py_ssize_t stride = buf->strides[dim]; Py_ssize_t stride = buf->strides[dim];
if (stride < 0) if (stride < 0)
stride = -stride; stride = -stride;
if (stride < buf->itemsize) { if (unlikely(stride < buf->itemsize)) {
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"Buffer and memoryview are not contiguous " "Buffer and memoryview are not contiguous "
"in the same dimension."); "in the same dimension.");
...@@ -207,17 +207,17 @@ __pyx_check_strides(Py_buffer *buf, int dim, int ndim, int spec) ...@@ -207,17 +207,17 @@ __pyx_check_strides(Py_buffer *buf, int dim, int ndim, int spec)
} }
} }
} else { } else {
if (spec & __Pyx_MEMVIEW_CONTIG && dim != ndim - 1) { if (unlikely(spec & __Pyx_MEMVIEW_CONTIG && dim != ndim - 1)) {
PyErr_Format(PyExc_ValueError, PyErr_Format(PyExc_ValueError,
"C-contiguous buffer is not contiguous in " "C-contiguous buffer is not contiguous in "
"dimension %d", dim); "dimension %d", dim);
goto fail; goto fail;
} else if (spec & (__Pyx_MEMVIEW_PTR)) { } else if (unlikely(spec & (__Pyx_MEMVIEW_PTR))) {
PyErr_Format(PyExc_ValueError, PyErr_Format(PyExc_ValueError,
"C-contiguous buffer is not indirect in " "C-contiguous buffer is not indirect in "
"dimension %d", dim); "dimension %d", dim);
goto fail; goto fail;
} else if (buf->suboffsets) { } else if (unlikely(buf->suboffsets)) {
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"Buffer exposes suboffsets but no strides"); "Buffer exposes suboffsets but no strides");
goto fail; goto fail;
...@@ -235,7 +235,7 @@ __pyx_check_suboffsets(Py_buffer *buf, int dim, CYTHON_UNUSED int ndim, int spec ...@@ -235,7 +235,7 @@ __pyx_check_suboffsets(Py_buffer *buf, int dim, CYTHON_UNUSED int ndim, int spec
// Todo: without PyBUF_INDIRECT we may not have suboffset information, i.e., the // Todo: without PyBUF_INDIRECT we may not have suboffset information, i.e., the
// ptr may not be set to NULL but may be uninitialized? // ptr may not be set to NULL but may be uninitialized?
if (spec & __Pyx_MEMVIEW_DIRECT) { if (spec & __Pyx_MEMVIEW_DIRECT) {
if (buf->suboffsets && buf->suboffsets[dim] >= 0) { if (unlikely(buf->suboffsets && buf->suboffsets[dim] >= 0)) {
PyErr_Format(PyExc_ValueError, PyErr_Format(PyExc_ValueError,
"Buffer not compatible with direct access " "Buffer not compatible with direct access "
"in dimension %d.", dim); "in dimension %d.", dim);
...@@ -244,7 +244,7 @@ __pyx_check_suboffsets(Py_buffer *buf, int dim, CYTHON_UNUSED int ndim, int spec ...@@ -244,7 +244,7 @@ __pyx_check_suboffsets(Py_buffer *buf, int dim, CYTHON_UNUSED int ndim, int spec
} }
if (spec & __Pyx_MEMVIEW_PTR) { if (spec & __Pyx_MEMVIEW_PTR) {
if (!buf->suboffsets || (buf->suboffsets[dim] < 0)) { if (unlikely(!buf->suboffsets || (buf->suboffsets[dim] < 0))) {
PyErr_Format(PyExc_ValueError, PyErr_Format(PyExc_ValueError,
"Buffer is not indirectly accessible " "Buffer is not indirectly accessible "
"in dimension %d.", dim); "in dimension %d.", dim);
...@@ -265,9 +265,7 @@ __pyx_verify_contig(Py_buffer *buf, int ndim, int c_or_f_flag) ...@@ -265,9 +265,7 @@ __pyx_verify_contig(Py_buffer *buf, int ndim, int c_or_f_flag)
if (c_or_f_flag & __Pyx_IS_F_CONTIG) { if (c_or_f_flag & __Pyx_IS_F_CONTIG) {
Py_ssize_t stride = 1; Py_ssize_t stride = 1;
for (i = 0; i < ndim; i++) { for (i = 0; i < ndim; i++) {
if (stride * buf->itemsize != buf->strides[i] && if (unlikely(stride * buf->itemsize != buf->strides[i] && buf->shape[i] > 1)) {
buf->shape[i] > 1)
{
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"Buffer not fortran contiguous."); "Buffer not fortran contiguous.");
goto fail; goto fail;
...@@ -277,8 +275,7 @@ __pyx_verify_contig(Py_buffer *buf, int ndim, int c_or_f_flag) ...@@ -277,8 +275,7 @@ __pyx_verify_contig(Py_buffer *buf, int ndim, int c_or_f_flag)
} else if (c_or_f_flag & __Pyx_IS_C_CONTIG) { } else if (c_or_f_flag & __Pyx_IS_C_CONTIG) {
Py_ssize_t stride = 1; Py_ssize_t stride = 1;
for (i = ndim - 1; i >- 1; i--) { for (i = ndim - 1; i >- 1; i--) {
if (stride * buf->itemsize != buf->strides[i] && if (unlikely(stride * buf->itemsize != buf->strides[i] && buf->shape[i] > 1)) {
buf->shape[i] > 1) {
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"Buffer not C contiguous."); "Buffer not C contiguous.");
goto fail; goto fail;
...@@ -325,7 +322,7 @@ static int __Pyx_ValidateAndInit_memviewslice( ...@@ -325,7 +322,7 @@ static int __Pyx_ValidateAndInit_memviewslice(
} }
buf = &memview->view; buf = &memview->view;
if (buf->ndim != ndim) { if (unlikely(buf->ndim != ndim)) {
PyErr_Format(PyExc_ValueError, PyErr_Format(PyExc_ValueError,
"Buffer has wrong number of dimensions (expected %d, got %d)", "Buffer has wrong number of dimensions (expected %d, got %d)",
ndim, buf->ndim); ndim, buf->ndim);
...@@ -334,10 +331,10 @@ static int __Pyx_ValidateAndInit_memviewslice( ...@@ -334,10 +331,10 @@ static int __Pyx_ValidateAndInit_memviewslice(
if (new_memview) { if (new_memview) {
__Pyx_BufFmt_Init(&ctx, stack, dtype); __Pyx_BufFmt_Init(&ctx, stack, dtype);
if (!__Pyx_BufFmt_CheckString(&ctx, buf->format)) goto fail; if (unlikely(!__Pyx_BufFmt_CheckString(&ctx, buf->format))) goto fail;
} }
if ((unsigned) buf->itemsize != dtype->size) { if (unlikely((unsigned) buf->itemsize != dtype->size)) {
PyErr_Format(PyExc_ValueError, PyErr_Format(PyExc_ValueError,
"Item size of buffer (%" CYTHON_FORMAT_SSIZE_T "u byte%s) " "Item size of buffer (%" CYTHON_FORMAT_SSIZE_T "u byte%s) "
"does not match size of '%s' (%" CYTHON_FORMAT_SSIZE_T "u byte%s)", "does not match size of '%s' (%" CYTHON_FORMAT_SSIZE_T "u byte%s)",
...@@ -352,14 +349,14 @@ static int __Pyx_ValidateAndInit_memviewslice( ...@@ -352,14 +349,14 @@ static int __Pyx_ValidateAndInit_memviewslice(
/* Check axes */ /* Check axes */
for (i = 0; i < ndim; i++) { for (i = 0; i < ndim; i++) {
spec = axes_specs[i]; spec = axes_specs[i];
if (!__pyx_check_strides(buf, i, ndim, spec)) if (unlikely(!__pyx_check_strides(buf, i, ndim, spec)))
goto fail; goto fail;
if (!__pyx_check_suboffsets(buf, i, ndim, spec)) if (unlikely(!__pyx_check_suboffsets(buf, i, ndim, spec)))
goto fail; goto fail;
} }
/* Check contiguity */ /* Check contiguity */
if (buf->strides && !__pyx_verify_contig(buf, ndim, c_or_f_flag)) if (unlikely(buf->strides && !__pyx_verify_contig(buf, ndim, c_or_f_flag)))
goto fail; goto fail;
/* Initialize */ /* Initialize */
...@@ -394,7 +391,7 @@ __Pyx_init_memviewslice(struct __pyx_memoryview_obj *memview, ...@@ -394,7 +391,7 @@ __Pyx_init_memviewslice(struct __pyx_memoryview_obj *memview,
Py_buffer *buf = &memview->view; Py_buffer *buf = &memview->view;
__Pyx_RefNannySetupContext("init_memviewslice", 0); __Pyx_RefNannySetupContext("init_memviewslice", 0);
if (memviewslice->memview || memviewslice->data) { if (unlikely(memviewslice->memview || memviewslice->data)) {
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"memviewslice is already initialized!"); "memviewslice is already initialized!");
goto fail; goto fail;
...@@ -491,7 +488,7 @@ __Pyx_INC_MEMVIEW({{memviewslice_name}} *memslice, int have_gil, int lineno) ...@@ -491,7 +488,7 @@ __Pyx_INC_MEMVIEW({{memviewslice_name}} *memslice, int have_gil, int lineno)
if (!memview || (PyObject *) memview == Py_None) if (!memview || (PyObject *) memview == Py_None)
return; /* allow uninitialized memoryview assignment */ return; /* allow uninitialized memoryview assignment */
if (__pyx_get_slice_count(memview) < 0) if (unlikely(__pyx_get_slice_count(memview) < 0))
__pyx_fatalerror("Acquisition count is %d (line %d)", __pyx_fatalerror("Acquisition count is %d (line %d)",
__pyx_get_slice_count(memview), lineno); __pyx_get_slice_count(memview), lineno);
...@@ -570,7 +567,7 @@ __pyx_memoryview_copy_new_contig(const __Pyx_memviewslice *from_mvs, ...@@ -570,7 +567,7 @@ __pyx_memoryview_copy_new_contig(const __Pyx_memviewslice *from_mvs,
__Pyx_RefNannySetupContext("__pyx_memoryview_copy_new_contig", 0); __Pyx_RefNannySetupContext("__pyx_memoryview_copy_new_contig", 0);
for (i = 0; i < ndim; i++) { for (i = 0; i < ndim; i++) {
if (from_mvs->suboffsets[i] >= 0) { if (unlikely(from_mvs->suboffsets[i] >= 0)) {
PyErr_Format(PyExc_ValueError, "Cannot copy memoryview slice with " PyErr_Format(PyExc_ValueError, "Cannot copy memoryview slice with "
"indirect dimensions (axis %d)", i); "indirect dimensions (axis %d)", i);
goto fail; goto fail;
...@@ -860,7 +857,7 @@ if (unlikely(__pyx_memoryview_slice_memviewslice( ...@@ -860,7 +857,7 @@ if (unlikely(__pyx_memoryview_slice_memviewslice(
{{endif}} {{endif}}
{{if boundscheck}} {{if boundscheck}}
if (!__Pyx_is_valid_index(__pyx_tmp_idx, __pyx_tmp_shape)) { if (unlikely(!__Pyx_is_valid_index(__pyx_tmp_idx, __pyx_tmp_shape))) {
{{if not have_gil}} {{if not have_gil}}
#ifdef WITH_THREAD #ifdef WITH_THREAD
PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure(); PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure();
......
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