-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
ENH: speed up putmask avoiding % in inner loop #5501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
ENH: speed up putmask avoiding % in inner loop
- Loading branch information
commit fc8db7395f51311e4856eaae25c4b3aa62bd9164
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -427,10 +427,11 @@ NPY_NO_EXPORT PyObject * | |
PyArray_PutMask(PyArrayObject *self, PyObject* values0, PyObject* mask0) | ||
{ | ||
PyArray_FastPutmaskFunc *func; | ||
PyArrayObject *mask, *values; | ||
PyArrayObject *mask, *values; | ||
PyArray_Descr *dtype; | ||
npy_intp i, chunk, ni, max_item, nv, tmp; | ||
npy_intp i, j, chunk, ni, max_item, nv; | ||
char *src, *dest; | ||
npy_bool *mask_data; | ||
int copied = 0; | ||
|
||
mask = NULL; | ||
|
@@ -469,6 +470,7 @@ PyArray_PutMask(PyArrayObject *self, PyObject* values0, PyObject* mask0) | |
"the same size"); | ||
goto fail; | ||
} | ||
mask_data = PyArray_DATA(mask); | ||
dtype = PyArray_DESCR(self); | ||
Py_INCREF(dtype); | ||
values = (PyArrayObject *)PyArray_FromAny(values0, dtype, | ||
|
@@ -483,14 +485,20 @@ PyArray_PutMask(PyArrayObject *self, PyObject* values0, PyObject* mask0) | |
Py_INCREF(Py_None); | ||
return Py_None; | ||
} | ||
src = PyArray_DATA(values); | ||
|
||
if (PyDataType_REFCHK(PyArray_DESCR(self))) { | ||
for (i = 0; i < ni; i++) { | ||
tmp = ((npy_bool *)(PyArray_DATA(mask)))[i]; | ||
if (tmp) { | ||
src = PyArray_BYTES(values) + chunk * (i % nv); | ||
PyArray_Item_INCREF(src, PyArray_DESCR(self)); | ||
PyArray_Item_XDECREF(dest+i*chunk, PyArray_DESCR(self)); | ||
memmove(dest + i * chunk, src, chunk); | ||
for (i = 0, j = 0; i < ni; i++, j++) { | ||
if (j >= nv) { | ||
j = 0; | ||
} | ||
if (mask_data[i]) { | ||
char *src_ptr = src + j*chunk; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could just increment |
||
char *dest_ptr = dest + i*chunk; | ||
|
||
PyArray_Item_INCREF(src_ptr, PyArray_DESCR(self)); | ||
PyArray_Item_XDECREF(dest_ptr, PyArray_DESCR(self)); | ||
memmove(dest_ptr, src_ptr, chunk); | ||
} | ||
} | ||
} | ||
|
@@ -499,16 +507,17 @@ PyArray_PutMask(PyArrayObject *self, PyObject* values0, PyObject* mask0) | |
NPY_BEGIN_THREADS_DESCR(PyArray_DESCR(self)); | ||
func = PyArray_DESCR(self)->f->fastputmask; | ||
if (func == NULL) { | ||
for (i = 0; i < ni; i++) { | ||
tmp = ((npy_bool *)(PyArray_DATA(mask)))[i]; | ||
if (tmp) { | ||
src = PyArray_BYTES(values) + chunk*(i % nv); | ||
memmove(dest + i*chunk, src, chunk); | ||
for (i = 0, j = 0; i < ni; i++, j++) { | ||
if (j >= nv) { | ||
j = 0; | ||
} | ||
if (mask_data[i]) { | ||
memmove(dest + i*chunk, src + j*chunk, chunk); | ||
} | ||
} | ||
} | ||
else { | ||
func(dest, PyArray_DATA(mask), ni, PyArray_DATA(values), nv); | ||
func(dest, mask_data, ni, src, nv); | ||
} | ||
NPY_END_THREADS; | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Speaking of branch prediction and code bumming,
gcc -O2
generates a loop with three conditional jumps, but if this conditional block is replaced withthere's only two of them. Might be worth a try to see if multiplication is faster than modulo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe worth a try, though a predicted near branch has among the best low latency/throughput of all instructions, multiplication is I think in the range of latency 4 throughout 1 cycle, likely the reason why the compiler does not do this transformation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the single benchmark I have been using, multiplying clocks in at 2.36 us, slightly better than current master, but noticeably slower than the single if in this PR, that takes 1.56 us.
Playing the "minimize branch misprediction" game, my best effort so far, clocking in at 1.25 us and outperforming the current code in this PR is the following:I suppose this is capitalizing on the ability to smartly predict loops. I am not fully sure that it is worth uglying the code like this, neither would I bet any money on the result being consistent across processor architectures. My timings come from a Sandy Bridge Intel i5, FWIW.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The extra performance was of course coming from leaving the mask check out... Putting it back in the following takes the same time (1.55 us) as the much more readable single if in the current PR: