Uh oh!
There was an error while loading. Please reload this page.
gh-99761: add invalid_index macro - #99762
Conversation
sweeneyde
commented
Nov 24, 2022
I think this is generally a good idea, but I'm not sure this should go in the public API. If it does, its name should probably begin with 'Py...'. See https://devguide.python.org/developer-workflow/c-api/ and https://peps.python.org/pep-0007/#naming-conventions . cc @vstinner |
eendebakpt
commented
Nov 25, 2022
Agreed. What would be a good location for the private version? One of the |
rhettinger
commented
Nov 26, 2022
|
eendebakpt
commented
Nov 26, 2022
Everywhere in the code the usage is
Done.
With inline functions I think we can never be certain the code gets inlined for all compilers and settings. I changed the static inline to a macro.
The |
vstinner
commented
Nov 28, 2022
You should not use macro but a static inline functions, see:
See PEP 670 which answers to that and we approved by the Steering Council. A static inline function must be used. |
vstinner
left a comment
There was a problem hiding this comment.
I'm not very excited by a function only used for a micro-optimization, I'm fine with (i < 0 || i >= Py_SIZE(self)) test. But I'm not against this change neither.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
eendebakpt
commented
Mar 6, 2023
@vstinner Could you have a look at the PR again? |
arhadthedev
left a comment
There was a problem hiding this comment.
All earlier reviews seem to be addressed.
serhiy-storchaka
commented
Jun 8, 2024
This idea was already discussed and rejected. See #72583. |
In
listobject.cthere is an optimization to check whether an index is valid (e.g.0 <= index < N) using a single comparison. The cast-to-unsigned optimization is used in current main in 3 locations:tupleobject.c,_collectionsmodule.candbytesobject.c.It is a micro optimization, but the code generated is different than the plain
i < 0 || i >= Py_SIZE(a). This PR tries to:i) apply the optimization to more locations
ii) make the code more consistent
By replacing index checks with a single method
_Py_is_valid_indexthat includes the optimization we have consistency in the code and have the optimized check for all index checks.Notes:
bytecodes.cthe optimized expression is used in three places. E.g.With the
_Py_is_valid_indexmethod it looks likewhich is in my opinion not very readable. For these cases a separate PR was created: #100064