Skip to content

Commit 2c43ad0

Browse files
committed
pythongh-127022: Simplify PyStackRef_FromPyObjectSteal
This gets rid of the immortal check in `PyStackRef_FromPyObjectSteal()`. Overall, this improves performance about 2% in the free threading build. This also renames `PyStackRef_Is()` to `PyStackRef_IsExactly()` because the macro requires that the tag bits of the arguments match, which is only true in certain special cases.
1 parent 29cbcbd commit 2c43ad0

File tree

5 files changed

+58
-72
lines changed

5 files changed

+58
-72
lines changed

Include/internal/pycore_stackref.h

+8-4
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ _PyStackRef_FromPyObjectSteal(PyObject *obj)
9999
assert(obj != NULL);
100100
// Make sure we don't take an already tagged value.
101101
assert(((uintptr_t)obj & Py_TAG_BITS) == 0);
102-
unsigned int tag = _Py_IsImmortal(obj) ? (Py_TAG_DEFERRED) : Py_TAG_PTR;
103-
return ((_PyStackRef){.bits = ((uintptr_t)(obj)) | tag});
102+
return (_PyStackRef){ .bits = (uintptr_t)obj };
104103
}
105104
# define PyStackRef_FromPyObjectSteal(obj) _PyStackRef_FromPyObjectSteal(_PyObject_CAST(obj))
106105

@@ -190,9 +189,14 @@ static const _PyStackRef PyStackRef_NULL = { .bits = 0 };
190189

191190
#endif // Py_GIL_DISABLED
192191

193-
// Note: this is a macro because MSVC (Windows) has trouble inlining it.
192+
// Check if a stackref is exactly the same as another stackref, including
193+
// the deferred bit.
194+
#define PyStackRef_IsExactly(a, b) (assert(((a).bits & 1) == ((b).bits & 1)), ((a).bits == (b).bits))
194195

195-
#define PyStackRef_Is(a, b) ((a).bits == (b).bits)
196+
// Checks that mask out the deferred bit in the free threading build.
197+
#define PyStackRef_IsNone(ref) (PyStackRef_AsPyObjectBorrow(ref) == Py_None)
198+
#define PyStackRef_IsTrue(ref) (PyStackRef_AsPyObjectBorrow(ref) == Py_True)
199+
#define PyStackRef_IsFalse(ref) (PyStackRef_AsPyObjectBorrow(ref) == Py_False)
196200

197201
// Converts a PyStackRef back to a PyObject *, converting the
198202
// stackref to a new reference.

Python/bytecodes.c

+18-25
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ dummy_func(
376376

377377
pure inst(UNARY_NOT, (value -- res)) {
378378
assert(PyStackRef_BoolCheck(value));
379-
res = PyStackRef_Is(value, PyStackRef_False)
379+
res = PyStackRef_IsExactly(value, PyStackRef_False)
380380
? PyStackRef_True : PyStackRef_False;
381381
DEAD(value);
382382
}
@@ -441,7 +441,7 @@ dummy_func(
441441

442442
inst(TO_BOOL_NONE, (unused/1, unused/2, value -- res)) {
443443
// This one is a bit weird, because we expect *some* failures:
444-
EXIT_IF(!PyStackRef_Is(value, PyStackRef_None));
444+
EXIT_IF(!PyStackRef_IsNone(value));
445445
DEAD(value);
446446
STAT_INC(TO_BOOL, hit);
447447
res = PyStackRef_False;
@@ -651,9 +651,7 @@ dummy_func(
651651
// specializations, but there is no output.
652652
// At the end we just skip over the STORE_FAST.
653653
op(_BINARY_OP_INPLACE_ADD_UNICODE, (left, right --)) {
654-
#ifndef NDEBUG
655654
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
656-
#endif
657655
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
658656

659657
int next_oparg;
@@ -664,7 +662,7 @@ dummy_func(
664662
next_oparg = CURRENT_OPERAND0();
665663
#endif
666664
_PyStackRef *target_local = &GETLOCAL(next_oparg);
667-
DEOPT_IF(!PyStackRef_Is(*target_local, left));
665+
DEOPT_IF(PyStackRef_AsPyObjectBorrow(*target_local) != left_o);
668666
STAT_INC(BINARY_OP, hit);
669667
/* Handle `left = left + right` or `left += right` for str.
670668
*
@@ -1141,7 +1139,7 @@ dummy_func(
11411139
gen_frame->previous = frame;
11421140
DISPATCH_INLINED(gen_frame);
11431141
}
1144-
if (PyStackRef_Is(v, PyStackRef_None) && PyIter_Check(receiver_o)) {
1142+
if (PyStackRef_IsNone(v) && PyIter_Check(receiver_o)) {
11451143
retval_o = Py_TYPE(receiver_o)->tp_iternext(receiver_o);
11461144
}
11471145
else {
@@ -1249,7 +1247,7 @@ dummy_func(
12491247
inst(POP_EXCEPT, (exc_value -- )) {
12501248
_PyErr_StackItem *exc_info = tstate->exc_info;
12511249
Py_XSETREF(exc_info->exc_value,
1252-
PyStackRef_Is(exc_value, PyStackRef_None)
1250+
PyStackRef_IsNone(exc_value)
12531251
? NULL : PyStackRef_AsPyObjectSteal(exc_value));
12541252
}
12551253

@@ -2481,13 +2479,7 @@ dummy_func(
24812479
}
24822480

24832481
inst(IS_OP, (left, right -- b)) {
2484-
#ifdef Py_GIL_DISABLED
2485-
// On free-threaded builds, objects are conditionally immortalized.
2486-
// So their bits don't always compare equally.
24872482
int res = Py_Is(PyStackRef_AsPyObjectBorrow(left), PyStackRef_AsPyObjectBorrow(right)) ^ oparg;
2488-
#else
2489-
int res = PyStackRef_Is(left, right) ^ oparg;
2490-
#endif
24912483
DECREF_INPUTS();
24922484
b = res ? PyStackRef_True : PyStackRef_False;
24932485
}
@@ -2693,22 +2685,23 @@ dummy_func(
26932685

26942686
replaced op(_POP_JUMP_IF_FALSE, (cond -- )) {
26952687
assert(PyStackRef_BoolCheck(cond));
2696-
int flag = PyStackRef_Is(cond, PyStackRef_False);
2688+
// int flag = PyStackRef_IsExactly(cond, PyStackRef_False);
2689+
int flag = PyStackRef_IsFalse(cond);
26972690
DEAD(cond);
26982691
RECORD_BRANCH_TAKEN(this_instr[1].cache, flag);
26992692
JUMPBY(oparg * flag);
27002693
}
27012694

27022695
replaced op(_POP_JUMP_IF_TRUE, (cond -- )) {
27032696
assert(PyStackRef_BoolCheck(cond));
2704-
int flag = PyStackRef_Is(cond, PyStackRef_True);
2697+
int flag = PyStackRef_IsExactly(cond, PyStackRef_True);
27052698
DEAD(cond);
27062699
RECORD_BRANCH_TAKEN(this_instr[1].cache, flag);
27072700
JUMPBY(oparg * flag);
27082701
}
27092702

27102703
op(_IS_NONE, (value -- b)) {
2711-
if (PyStackRef_Is(value, PyStackRef_None)) {
2704+
if (PyStackRef_IsNone(value)) {
27122705
b = PyStackRef_True;
27132706
DEAD(value);
27142707
}
@@ -3752,7 +3745,7 @@ dummy_func(
37523745

37533746
inst(EXIT_INIT_CHECK, (should_be_none -- )) {
37543747
assert(STACK_LEVEL() == 2);
3755-
if (!PyStackRef_Is(should_be_none, PyStackRef_None)) {
3748+
if (!PyStackRef_IsNone(should_be_none)) {
37563749
PyErr_Format(PyExc_TypeError,
37573750
"__init__() should return None, not '%.200s'",
37583751
Py_TYPE(PyStackRef_AsPyObjectBorrow(should_be_none))->tp_name);
@@ -4712,7 +4705,7 @@ dummy_func(
47124705
inst(INSTRUMENTED_POP_JUMP_IF_TRUE, (unused/1 -- )) {
47134706
_PyStackRef cond = POP();
47144707
assert(PyStackRef_BoolCheck(cond));
4715-
int flag = PyStackRef_Is(cond, PyStackRef_True);
4708+
int flag = PyStackRef_IsExactly(cond, PyStackRef_True);
47164709
int offset = flag * oparg;
47174710
RECORD_BRANCH_TAKEN(this_instr[1].cache, flag);
47184711
INSTRUMENTED_JUMP(this_instr, next_instr + offset, PY_MONITORING_EVENT_BRANCH);
@@ -4721,15 +4714,15 @@ dummy_func(
47214714
inst(INSTRUMENTED_POP_JUMP_IF_FALSE, (unused/1 -- )) {
47224715
_PyStackRef cond = POP();
47234716
assert(PyStackRef_BoolCheck(cond));
4724-
int flag = PyStackRef_Is(cond, PyStackRef_False);
4717+
int flag = PyStackRef_IsFalse(cond);
47254718
int offset = flag * oparg;
47264719
RECORD_BRANCH_TAKEN(this_instr[1].cache, flag);
47274720
INSTRUMENTED_JUMP(this_instr, next_instr + offset, PY_MONITORING_EVENT_BRANCH);
47284721
}
47294722

47304723
inst(INSTRUMENTED_POP_JUMP_IF_NONE, (unused/1 -- )) {
47314724
_PyStackRef value_stackref = POP();
4732-
int flag = PyStackRef_Is(value_stackref, PyStackRef_None);
4725+
int flag = PyStackRef_IsNone(value_stackref);
47334726
int offset;
47344727
if (flag) {
47354728
offset = oparg;
@@ -4745,7 +4738,7 @@ dummy_func(
47454738
inst(INSTRUMENTED_POP_JUMP_IF_NOT_NONE, (unused/1 -- )) {
47464739
_PyStackRef value_stackref = POP();
47474740
int offset;
4748-
int nflag = PyStackRef_Is(value_stackref, PyStackRef_None);
4741+
int nflag = PyStackRef_IsNone(value_stackref);
47494742
if (nflag) {
47504743
offset = 0;
47514744
}
@@ -4780,21 +4773,21 @@ dummy_func(
47804773
///////// Tier-2 only opcodes /////////
47814774

47824775
op (_GUARD_IS_TRUE_POP, (flag -- )) {
4783-
int is_true = PyStackRef_Is(flag, PyStackRef_True);
4776+
int is_true = PyStackRef_IsTrue(flag);
47844777
DEAD(flag);
47854778
SYNC_SP();
47864779
EXIT_IF(!is_true);
47874780
}
47884781

47894782
op (_GUARD_IS_FALSE_POP, (flag -- )) {
4790-
int is_false = PyStackRef_Is(flag, PyStackRef_False);
4783+
int is_false = PyStackRef_IsFalse(flag);
47914784
DEAD(flag);
47924785
SYNC_SP();
47934786
EXIT_IF(!is_false);
47944787
}
47954788

47964789
op (_GUARD_IS_NONE_POP, (val -- )) {
4797-
int is_none = PyStackRef_Is(val, PyStackRef_None);
4790+
int is_none = PyStackRef_IsNone(val);
47984791
if (!is_none) {
47994792
PyStackRef_CLOSE(val);
48004793
SYNC_SP();
@@ -4804,7 +4797,7 @@ dummy_func(
48044797
}
48054798

48064799
op (_GUARD_IS_NOT_NONE_POP, (val -- )) {
4807-
int is_none = PyStackRef_Is(val, PyStackRef_None);
4800+
int is_none = PyStackRef_IsNone(val);
48084801
PyStackRef_CLOSE(val);
48094802
SYNC_SP();
48104803
EXIT_IF(is_none);

Python/executor_cases.c.h

+10-18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)