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-130415: Narrowstr to"" based on boolean tests#130476

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
brandtbucher merged 9 commits intopython:mainfromfluhus:hack-night2
Mar 4, 2025

Conversation

fluhus
Copy link
Contributor

@fluhusfluhus commentedFeb 22, 2025
edited by bedevere-appbot
Loading

Assign value to string when anif evaluates to false.

@brandtbucher

@ghost
Copy link

ghost commentedFeb 22, 2025
edited by ghost
Loading

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply theskip news label instead.

@brandtbucherbrandtbucher self-assigned thisFeb 22, 2025
@brandtbucherbrandtbucher added performancePerformance or resource usage interpreter-core(Objects, Python, Grammar, and Parser dirs) topic-JIT labelsFeb 22, 2025
@bedevere-app
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phraseI have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@fluhus
Copy link
ContributorAuthor

Added requested corrections. Thanks,@brandtbucher !

Copy link
Member

@markshannonmarkshannon left a comment

Choose a reason for hiding this comment

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

Thanks for contribution.
Unfortunately, I think there is a critical flaw in this approach as it could result in mis-optimizations in the future.

This would be a useful optimization, so if you're willing to pursue this further, it would be appreciated.

dummy = "aaa"
# Hopefully the optimizer can't guess what the value is.
# empty is always "", but we can only prove that it's a string:
empty = dummy[:0]
Copy link
Member

Choose a reason for hiding this comment

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

I can easily see the optimizer turning"aaa"[:0] into"".
empty doesn't need to be a constant, we just need it to be mostly "", for profiling.
Use something likeempty = "a"[:(n % 1000) == 0]

Copy link
Member

@brandtbucherbrandtbucherMar 3, 2025
edited
Loading

Choose a reason for hiding this comment

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

Since we check the actual path taken as part of the test, we need the value toalways be"", not justmostly"". So maybe:

false=i==TIER2_THRESHOLDempty="X"[:false]

The optimizer can't provefalse isFalse, so it's good enough for our purposes.

// *can't* narrow res, since that would cause the guard to be
// removed and the narrowed value to be invalid:
if (next_opcode == _GUARD_IS_FALSE_POP) {
sym_set_const(value, Py_GetConstant(Py_CONSTANT_EMPTY_STR));
Copy link
Member

Choose a reason for hiding this comment

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

This is strictly incorrect. We don't know thatvalue is "" until after the_GUARD_IS_FALSE_POP.
The reason that matters is that when we start attaching type information to side exits, as we probably will in 3.15, then this could lead us to infer thatvalue is "" on both branches. Which would be wrong.

There are two possible fixes for this.

  • CombineTO_BOOL_STR and_GUARD_IS_FALSE_POP/_GUARD_IS_TRUE_POP into a single (super)instruction, then optimize that.
  • Annotate the bool value resulting from theTO_BOOL with its input, then in_GUARD_IS_FALSE_POP convert the input value toTO_BOOL.

I prefer the second option, although it may be more work, as it is more flexible and can be extended more easily.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah,@Fidget-Spinner and I suggested something like the latter on the issue (new symbols likeJitBoolOf(JitOptSymbol *source, bool inverted) andJitEqualTo(JitOptSymbol *lhs, JitOptSymbol *rhs, bool inverted)). That's probably the direction we're headed in longer term.

However, I don't think we should let perfect be the enemy of good here. We have nice, working optimizations in these PRs; just because wemight sink info onto side exits in the future probably shouldn't prevent us from making changes like this now for 3.14, which are perfectly correct for the current optimizer (which doesn't sink anything).

I'm inclined to land these changes and other similar ones for==/!= now, and make the symbolic representation of derived boolean values more complex later as an improvement (it will also be able to handle more uncommon cases likex = y == 42; if x: ...). I'm really worried that if we try to "future-proof" optimizations based on what wecould do six months from now, it will prevent actual improvements in the near term.

But I'll defer to you here. If havingvalue be narrowed one uop too early in the instruction stream is enough to block this PR, I can work with these new contributors on the more complex solution. But as-is, this has no bugs and works as intended. We don't sink value info onto side exits, so it's correct.

@bedevere-app
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phraseI have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@brandtbucherbrandtbucher changed the titleGH-130415: Add JIT optimization path for _TO_BOOL_STRGH-130415: Narrowstr to"" based on boolean testsMar 3, 2025
@brandtbucherbrandtbucher merged commit691354c intopython:mainMar 4, 2025
58 checks passed
@fluhusfluhus deleted the hack-night2 branchMarch 21, 2025 20:23
seehwan pushed a commit to seehwan/cpython that referenced this pull requestApr 16, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@markshannonmarkshannonmarkshannon requested changes

@brandtbucherbrandtbucherbrandtbucher approved these changes

@Fidget-SpinnerFidget-SpinnerAwaiting requested review from Fidget-SpinnerFidget-Spinner is a code owner

Assignees

@brandtbucherbrandtbucher

Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetopic-JIT
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@fluhus@markshannon@brandtbucher

[8]ページ先頭

©2009-2025 Movatter.jp