GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-143732: Add specialization for TO_BOOL by eendebakpt · Pull Request #148113 · python/cpython

Conversation

@eendebakpt

@eendebakpt eendebakpt commented Apr 4, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

We add TO_BOOL_GENERIC as suggested in #143732 (comment). This adds type information for several builtin classes. We also add _TO_BOOL_DICT (tier 2). In the jit conversion of a dict to bool is about 30% faster.

  • Issue: Broader specialization in the Specializing Adaptive Interpreter for better JIT performance #143732

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
eendebakpt and others added 5 commits March 25, 2026 22:53
@eendebakpt @claude
Add TO_BOOL_GENERIC specialization and _TO_BOOL_DICT JIT optimization
395bdfc
- Add TO_BOOL_GENERIC: a catch-all specialization for types not covered
  by existing TO_BOOL variants (dict, tuple, float, set, bytes, frozenset,
  etc. and heap types with __bool__/__len__). Records type info for the JIT.

- Add _TO_BOOL_DICT: a tier2-only uop that checks dict.ma_used directly
  instead of calling PyObject_IsTrue(). The JIT optimizer replaces _TO_BOOL
  with _TO_BOOL_DICT when the type is known to be dict or frozendict.

- Fix _GUARD_TYPE_VERSION optimizer handler to resolve types from recorded
  type info even when the type version cache has a collision. This enables
  the optimizer to eliminate redundant type guards (e.g. _GUARD_NOS_LIST)
  in more cases.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@eendebakpt
Merge branch 'main' into to_bool_generic
8e62e9f
@eendebakpt
refactor
d52937f
@eendebakpt
refactor
abe868f
@eendebakpt
refactor
c896087
@eendebakpt
eendebakpt requested review from Fidget-Spinner, markshannon, savannahostrowski and tomasr8 as code owners April 4, 2026 21:51
@bedevere-app bedevere-app Bot added the awaiting review label Apr 4, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Apr 4, 2026
Broader specialization in the Specializing Adaptive Interpreter for better JIT performance #143732
Open
eendebakpt
eendebakpt commented Apr 5, 2026
View reviewed changes
Comment thread Lib/test/test_opcache.py
self.assert_specialized(to_bool_str, "TO_BOOL_STR")
self.assert_no_opcode(to_bool_str, "TO_BOOL")

def to_bool_generic_dict():

@eendebakpt eendebakpt Apr 5, 2026

Copy link
Copy Markdown
Contributor Author

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.

There is quite some redundancy in the added tests (and also the existing tests). We could refactor this, but it is a bit tricky as re-using the same method (with different arguments) makes it more difficult to reason about the specialization that takes place.

If we want this, I suggest we open a separate PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
eendebakpt
eendebakpt commented Apr 5, 2026
View reviewed changes
Comment thread Python/optimizer_bytecodes.c
// already added one earlier.
if (sym_set_type_version(owner, type_version)) {
// sym_set_type_version can resolve the type from recorded type info
// even when the version cache has a collision

@eendebakpt eendebakpt Apr 5, 2026

Copy link
Copy Markdown
Contributor Author

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.

This change has as side effect the list.append needs less checks.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@eendebakpt eendebakpt changed the title Draft: gh-143732: Add specialization for TO_BOOL gh-143732: Add specialization for TO_BOOL Apr 5, 2026
eendebakpt and others added 4 commits April 5, 2026 21:20
@eendebakpt
Merge branch 'main' into to_bool_generic
f616e12
@blurb-it
📜🤖 Added by blurb_it.
0d00d54
@eendebakpt
format
2c645ee
@eendebakpt
Merge branch 'main' into to_bool_generic
39f9e66
@markshannon

markshannon commented Apr 7, 2026

Copy link
Copy Markdown
Member

I don't think we want TO_BOOL_GENERIC as it is basically duplicating TO_BOOL. The "generic" form is only useful if we also add a _PY form that inlines calls to __bool__() for classes where __bool__() is implemented in Python.
In that case we need the "generic" form to guard against __bool__() being implemented in Python, so we always inline the call.
I'm not sure if this is worth it for __bool__ as much as for other __dunder__ functions.

What you can do is add the _RECORD_TOS_TYPE uop to TO_BOOL and use that to implement the _TO_BOOL_DICT optimization.

You might also want to add _TO_BOOL_SIZED and apply it to any class where __bool__() is equivalent to Py_SIZE(obj) != 0. We could then specialize TO_BOOL for tuples, bytes, bytearrays and others, in the JIT.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@eendebakpt eendebakpt mentioned this pull request Apr 8, 2026
gh-143732: Add tier2 specialization for TO_BOOL #148271
Open
@eendebakpt

eendebakpt commented Apr 8, 2026

Copy link
Copy Markdown
Contributor Author

@markshannon It seems I misinterpreted the comments at the issue. I though the TO_BOOL_GENERIC/TO_BOOL_PY pair would cover most (all types) and allow tier2 specialization.

I am having some trouble adding the _RECORD_TOS_TYPE to TO_BOOL as that has _SPECIALIZE_TO_BOOL and I cannot combine the two. I created a separate PR with just the tier2 opcodes. (they work in the jit, but could benefit from more type information from either a _RECORD_TOS_TYPE or tier1 opcodes).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@markshannon

markshannon commented Apr 9, 2026

Copy link
Copy Markdown
Member

#148285

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@markshannon

markshannon commented Apr 30, 2026

Copy link
Copy Markdown
Member

The issue with _SPECIALIZE_TO_BOOL preventing recording has been fixed, if you want to pick this up again

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@eendebakpt

eendebakpt commented Apr 30, 2026

Copy link
Copy Markdown
Contributor Author

@markshannon I am closing this in favor of #148271. That PR only currently adds the tier2 opcodes (not yet the recording uop). I will add the recording uop on top of the PR or a followup PR depending how hard it is.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@eendebakpt eendebakpt closed this Apr 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@markshannon markshannon Awaiting requested review from markshannon markshannon is a code owner
@tomasr8 tomasr8 Awaiting requested review from tomasr8 tomasr8 is a code owner
@Fidget-Spinner Fidget-Spinner Awaiting requested review from Fidget-Spinner Fidget-Spinner is a code owner
@savannahostrowski savannahostrowski Awaiting requested review from savannahostrowski

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting review

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@eendebakpt @markshannon
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.