GH-106008: Make implicit boolean conversions explicit#106003
Conversation
markshannon
left a comment
There was a problem hiding this comment.
A few minor comments, otherwise LGTM.
Sorry, something went wrong.
|
Note for possible future PR: We could, at the cost of two bits in Rather than check the version number, check the For abi4, we could add a per-object bit to handle immutable objects like ints and strings. |
Sorry, something went wrong.
|
Hey @brandtbucher, I have a question about this PR. In #106393 I had to change the code in to The reason is that the uop executor currently exits whenever it jumps, and your original code from this PR always jumps. Did you (or @markshannon) have a reason to prefer the If there's no deep reason I'll keep it the way I coded it up; but if there is (maybe it came out faster in a micro-benchmark?) then I suppose I could fix it another way in the uop interpreter (e.g. only exiting if the jump offset is nonzero). |
Sorry, something went wrong.
|
That the implementation of branches is itself branchless has a certain aesthetic appeal 🙂. TBH, that's the main reason. The multiplication form will be quicker for unpredictable branches, and slower for predictable ones in the tier 1 interpreter. |
Sorry, something went wrong.
Summary: Implements what we need from [GH-106008](python/cpython#106003). Technically the included updates to `UNARY_NOT`, and `POP_JUMP_IF_FALSE|TRUE` are optimizations and not strictly needed. However, it really makes sense to to them. Reviewed By: DinoV Differential Revision: D80682012 fbshipit-source-id: 6150d52dddd28ef5c2e401ed17df7c7cad09074b
...and specialize them!
This adds a new
TO_BOOLbytecode that prefixes allUNARY_NOT,POP_JUMP_IF_TRUE, andPOP_JUMP_IF_FALSEinstructions, which now require an exact boolean. We also use a spare bit inCOMPARE_OP's oparg to indicate whether the result should be converted to bool (this saves aTO_BOOLfor most branches, and is a no-op for allCOMPARE_OPspecializations)."0% faster". Stats show a 93.5% hit rate for the new instructions.
📚 Documentation preview 📚: https://cpython-previews--106003.org.readthedocs.build/