Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-99761: add invalid_index macro#99762

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
eendebakpt wants to merge52 commits intopython:mainfromeendebakpt:list_tuple

Conversation

eendebakpt
Copy link
Contributor

@eendebakpteendebakpt commentedNov 24, 2022
edited
Loading

Inlistobject.c there is anoptimization 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:

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

@sweeneyde
Copy link
Member

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...'. Seehttps://devguide.python.org/developer-workflow/c-api/ andhttps://peps.python.org/pep-0007/#naming-conventions .

cc@vstinner

@eendebakpt
Copy link
ContributorAuthor

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...'. Seehttps://devguide.python.org/developer-workflow/c-api/ andhttps://peps.python.org/pep-0007/#naming-conventions .

cc@vstinner

Agreed. What would be a good location for the private version? One of theInclude/internal/python_xxxx files?

@rhettinger
Copy link
Contributor

  • Keep the code asvalid_index rather thaninvalid_index so that we don't even up with double negatives.

  • Please leave the collections.deque code alone. The author (me) aspires to keep this code not tightly coupled to the rest of the C API. The code is currently clean an doesn't need to be changed.

  • Are youcertain that inline functions always get inlined? We know that macros doalways get inlined. Thevalid_index macro currently gets used in performance critical code.

  • As Dennis says, this should not be part of the public API. It is a performance trick that happens to currently be useful. As compilers get better, this would be done automatically and we would prefer the simple inline code rather than the extra layer of abstraction.

@eendebakpt
Copy link
ContributorAuthor

  • Keep the code asvalid_index rather thaninvalid_index so that we don't even up with double negatives.

Everywhere in the code the usage is!valid_index, which is why I preferredinvalid_index. I renamed tovalid_index again.

  • Please leave the collections.deque code alone. The author (me) aspires to keep this code not tightly coupled to the rest of
    the C API. The code is currently clean an doesn't need to be changed.

Done.

  • Are youcertain that inline functions always get inlined? We know that macros doalways get inlined. Thevalid_index macro currently gets used in performance critical code.

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.

  • As Dennis says, this should not be part of the public API. It is a performance trick that happens to currently be useful. As compilers get better, this would be done automatically and we would prefer the simple inline code rather than the extra layer of abstraction.

Thevalid_index is now private (part of Includes/internal/pycore_abstract.h). If there is a more suitable location we can move it.

@rhettinger

@eendebakpteendebakpt marked this pull request as ready for reviewNovember 26, 2022 22:12
@vstinner
Copy link
Member

You should not use macro but a static inline functions, see:

@rhettinger:

Are you certain that inline functions always get inlined? We know that macros do always get inlined. The valid_index macro currently gets used in performance critical code.

See PEP 670 which answers to that and we approved by the Steering Council. A static inline function must be used.

erlend-aasland reacted with thumbs up emoji

Copy link
Member

@vstinnervstinner left a 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.

@eendebakpt
Copy link
ContributorAuthor

The overall change LGTM, but please address my two remaining change requests.

@vstinner Could you have a look at the PR again?

@arhadthedevarhadthedev added interpreter-core(Objects, Python, Grammar, and Parser dirs) extension-modulesC modules in the Modules dir skip news labelsApr 5, 2023
Copy link
Member

@arhadthedevarhadthedev left a 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.

@serhiy-storchaka
Copy link
Member

This idea was already discussed and rejected. See#72583.

eendebakpt reacted with thumbs up emoji

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@arhadthedevarhadthedevarhadthedev approved these changes

@vstinnervstinnerAwaiting requested review from vstinner

Assignees
No one assigned
Labels
awaiting core reviewextension-modulesC modules in the Modules dirinterpreter-core(Objects, Python, Grammar, and Parser dirs)skip news
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

7 participants
@eendebakpt@sweeneyde@rhettinger@vstinner@serhiy-storchaka@arhadthedev@bedevere-bot

[8]ページ先頭

©2009-2025 Movatter.jp