-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
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
gh-99761: add invalid_index macro #99762
Conversation
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 |
Agreed. What would be a good location for the private version? One of the |
37a83e2
to
d85a883
Compare
d85a883
to
1d58ef0
Compare
|
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 |
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. |
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.
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.
@vstinner Could you have a look at the PR again? |
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.
All earlier reviews seem to be addressed.
This idea was already discussed and rejected. See #72583. |
In
listobject.c
there 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.c
andbytesobject.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_index
that includes the optimization we have consistency in the code and have the optimized check for all index checks.Notes:
bytecodes.c
the optimized expression is used in three places. E.g.With the
_Py_is_valid_index
method it looks likewhich is in my opinion not very readable. For these cases a separate PR was created: #100064