GeezerVison is showing github.com — gh-143732: Add specialization for TO_BOOL by eendebakpt · Pull Request #148113 · python/cpython
Conversation
eendebakpt
commented
Apr 4, 2026
•
edited by bedevere-app
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
- 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]>
|
|
self.assert_specialized(to_bool_str, "TO_BOOL_STR")
|
|
|
self.assert_no_opcode(to_bool_str, "TO_BOOL")
|
|
|
|
|
|
def to_bool_generic_dict():
|
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.
|
|
// 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
|
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.
eendebakpt
changed the title
Draft: gh-143732: Add specialization for TO_BOOL
gh-143732: Add specialization for TO_BOOL
Apr 5, 2026
|
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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@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).
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
The issue with _SPECIALIZE_TO_BOOL preventing recording has been fixed, if you want to pick this up again
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
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.
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.
|