gh-146640: Optimize int operations by mutating uniquely-referenced operands in place (JIT only)#146641
Draft
eendebakpt wants to merge 12 commits intopython:mainfrom
Draft
gh-146640: Optimize int operations by mutating uniquely-referenced operands in place (JIT only)#146641eendebakpt wants to merge 12 commits intopython:mainfrom
eendebakpt wants to merge 12 commits intopython:mainfrom
Conversation
Python/bytecodes.c
Outdated
| // On mutation success, DUP the target so POP_TOP_INT can safely | ||
| // decref the original. Tier 2 only. | ||
| tier2 op(_BINARY_OP_ADD_INT_INPLACE, (left, right -- res, l, r)) { | ||
| INT_INPLACE_OP(left, right, left, +); |
Member
There was a problem hiding this comment.
I think you can factor out all the common code here into a function that takes a function pointer (the operation to do).
For each op, pass the function pointer e.g. _PyCompactLong_Add or whatever, and then the CPU/inliner can fix it up/constant-fold it away in the compiler.
eendebakpt
commented
Mar 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
In this PR we add inplace binary operations for int type, similar to #146397. A complication compared to the float case is that the int operations can return small ints which should not be modified in place. In this PR we:
PyJitRef_MakeUnique(sym_new_compact_int(ctx));to mark that a symbol is an int that is either unique or one of the small ints._BINARY_OP_ADD_INT_INPLACE(with variations for the other operations) that handles both small ints as input and overflows. An alternative to handling these cases in the opcode would be do deopt. Here we decided not to deopts because this could lead to many deopts.Micro benchmark results:
total += a*b + c(non-small)a*bresulttotal += a + b(non-small)a+bresult is unique, inplace on+=t = a + b(plain assign)+=, not affectedtotal += a*b + c*d(chain)+total += a*b + 1(small int)All values are non-small integers (> 1024) unless noted.
Selected pyperformance benchmarks: