Remove unnecessary calls to PyNumber_Index, PyLong_Checks #3242
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.
This simplifies code and improves performance. It makes
rect[x]
~7% faster andrect[x] = y
~9% faster. Although these functions are really cheap, and therefore I would be surprised if there was any measurable macro impact from this change. You can run them 10 million times in less than a second. I didn't test performance impact in pixelarray.And in case anyone is curious
rect.y
is ~8% faster thanrect[1]
(after this PR). So for those micro-optimizers out there, that's the strategy to use.PyNumber_AsSsize_t doesn't need the value to be converted to an "index int" first.
If you look at the CPython source, in Objects/abstract.c:
The first thing it does is use PyNumber_Index anyway!
Plus it is documented as "Returns o converted to a Py_ssize_t value if o can be interpreted as an integer". Having an
__index__
method counts as being interpretable as an integer.PyIndex_Check(op) || PyLong_Check(op)
, the PyLong_Check is unnecessary.Python ints already pass PyIndex_Check, as you can tell because Python ints work in the Rect functions.