pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/python/cpython/pull/148113

sets/global-0bd78641c0a1f3e0.css" /> gh-143732: Add specialization for TO_BOOL by eendebakpt · Pull Request #148113 · python/cpython · GitHub
Skip to content

gh-143732: Add specialization for TO_BOOL#148113

Open
eendebakpt wants to merge 6 commits intopython:mainfrom
eendebakpt:to_bool_generic
Open

gh-143732: Add specialization for TO_BOOL#148113
eendebakpt wants to merge 6 commits intopython:mainfrom
eendebakpt:to_bool_generic

Conversation

@eendebakpt
Copy link
Copy Markdown
Contributor

@eendebakpt eendebakpt commented Apr 4, 2026

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.

eendebakpt and others added 5 commits March 25, 2026 22:53
- 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) <noreply@anthropic.com>
self.assert_specialized(to_bool_str, "TO_BOOL_STR")
self.assert_no_opcode(to_bool_str, "TO_BOOL")

def to_bool_generic_dict():
Copy link
Copy Markdown
Contributor Author

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.

// 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
Copy link
Copy Markdown
Contributor Author

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.

@eendebakpt eendebakpt changed the title Draft: gh-143732: Add specialization for TO_BOOL gh-143732: Add specialization for TO_BOOL Apr 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy