Title: gh-99761: add invalid_index macro by eendebakpt · Pull Request #99762 · python/cpython · GitHub
Open Graph Title: gh-99761: add invalid_index macro by eendebakpt · Pull Request #99762 · python/cpython
X Title: gh-99761: add invalid_index macro by eendebakpt · Pull Request #99762 · python/cpython
Description: 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 and bytesobject.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. Issue: gh-99761 Notes: The new method is private and is static inline (https://peps.python.org/pep-0670/) In bytecodes.c the optimized expression is used in three places. E.g. DEOPT_IF(((size_t)signed_magnitude) > 1, BINARY_SUBSCR); With the _Py_is_valid_index method it looks like DEOPT_IF(!_Py_is_valid_index(signed_magnitude, 2), BINARY_SUBSCR); which is in my opinion not very readable. For these cases a separate PR was created: #100064
Open Graph Description: 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 loc...
X Description: 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 ...
Opengraph URL: https://github.com/python/cpython/pull/99762
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/checks(.:format) |
| route-controller | pull_requests |
| route-action | checks |
| fetch-nonce | v2:cea7823d-2842-f34f-01ee-b3667f6f79df |
| current-catalog-service-hash | 87dc3bc62d9b466312751bfd5f889726f4f1337bdff4e8be7da7c93d6c00a25a |
| request-id | 8E9A:2265AE:2446236:3138E36:696AD323 |
| html-safe-nonce | 898436426954ac0cd23f1851c8d52405da831dee44d6631ccdf76ddc7449a88b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RTlBOjIyNjVBRToyNDQ2MjM2OjMxMzhFMzY6Njk2QUQzMjMiLCJ2aXNpdG9yX2lkIjoiNDQwNTEwMjY3NTk4ODUwMTI4MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 653e19abe28256de6ecbfd2fbbb77c9526113c059ae8e457f6bd494b7382a4f7 |
| hovercard-subject-tag | pull_request:1135366320 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,checks,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/python/cpython/pull/99762/checks |
| twitter:image | https://avatars.githubusercontent.com/u/883786?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/883786?s=400&v=4 |
| og:image:alt | 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 loc... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | c785f4ce187e9e7331257791b36ddee01625bb8e292a9b4fe2c16d4c006abf5d |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive full-width full-width-p-0 |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | c718a376fcf780eb22089171adb84a543f660bf7 |
| ui-target | canary-1 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width