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-134953: Make the True/False/None check more efficient#138931

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

Merged
ambv merged 1 commit intopython:mainfromambv:gh-134953-followup
Sep 15, 2025

Conversation

@ambv
Copy link
Contributor

@ambvambv commentedSep 15, 2025
edited by bedevere-appbot
Loading

Copy link
Member

@StanFromIrelandStanFromIreland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

LGTM

ZERO_WIDTH_BRACKET=re.compile(r"\x01.*?\x02")
ZERO_WIDTH_TRANS=str.maketrans({"\x01":"","\x02":""})
IDENTIFIERS_AFTER= {"def","class"}
KEYWORD_CONSTANTS= {"True","False","None"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
KEYWORD_CONSTANTS= {"True","False","None"}
KEYWORD_CONSTANTS=frozenset({"True","False","None"})

Is even faster.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The compiler already does this automatically:

>>> def foo():...     KEYWORD_CONSTANTS = {"True", "False", "None"}...>>> foo.__code__.co_consts(None, frozenset({'False', 'True', 'None'}))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The compiler only does that for function locals, right? A module variable could be mutated from other modules.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@auscompgeek You are right, for module level variables the optimization is not performed, soKEYWORD_CONSTANTS stays a set. For the free-threading build the performance difference is measurable:

Python 3.15.0a0 free-threading build (heads/main-dirty:2a54acf3c3d, Sep  1 2025, 14:33:50) [MSC v.1939 64 bit (AMD64)] on win32Type "help", "copyright", "credits" or "license" for more information.>>> import timeit>>> timeit.timeit('[10 in s for ii in range(100)]', setup='from _pyrepl.utils import KEYWORD_CONSTANTS as s; f = frozenset(s)')8.864965499844402>>> timeit.timeit('[10 in f for ii in range(100)]', setup='from _pyrepl.utils import KEYWORD_CONSTANTS as s; f = frozenset(s)')8.371516299899668

ambv and efimov-mikhail reacted with thumbs up emoji
Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@eendebakpt I'll accept a PR to wrap all global sets in_pyrepl.utils as frozensets. There's quite a few of them.

StanFromIreland reacted with thumbs up emoji
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I created a bit more general issue:#139003. For_pyrepl I'll make a PR.

@ambvambv merged commit811acc8 intopython:mainSep 15, 2025
53 checks passed
@ambvambv added the needs backport to 3.14bugs and security fixes labelSep 15, 2025
@miss-islington-app
Copy link

Thanks@ambv for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull requestSep 15, 2025
…nGH-138931)(cherry picked from commit811acc8)Co-authored-by: Łukasz Langa <lukasz@langa.pl>
@bedevere-app
Copy link

GH-138939 is a backport of this pull request to the3.14 branch.

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

Reviewers

@eendebakpteendebakpteendebakpt left review comments

@StanFromIrelandStanFromIrelandStanFromIreland approved these changes

@pablogsalpablogsalAwaiting requested review from pablogsalpablogsal is a code owner

@lysnikolaoulysnikolaouAwaiting requested review from lysnikolaoulysnikolaou is a code owner

+1 more reviewer

@auscompgeekauscompgeekauscompgeek left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

5 participants

@ambv@auscompgeek@eendebakpt@pablogsal@StanFromIreland

[8]ページ先頭

©2009-2025 Movatter.jp