Skip to content

Commit 07995d1

Browse files
naschemeebonnal
authored andcommitted
pythongh-115999: Add free-threaded specialization for SEND (pythongh-127426)
No additional thread safety changes are required. Note that sending to a generator that is shared between threads is currently not safe in the free-threaded build.
1 parent 27fdbb9 commit 07995d1

File tree

4 files changed

+50
-15
lines changed

4 files changed

+50
-15
lines changed

Lib/test/test_opcache.py

+42
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,48 @@ def contains_op_set():
13111311
self.assert_specialized(contains_op_set, "CONTAINS_OP_SET")
13121312
self.assert_no_opcode(contains_op_set, "CONTAINS_OP")
13131313

1314+
@cpython_only
1315+
@requires_specialization_ft
1316+
def test_send_with(self):
1317+
def run_async(coro):
1318+
while True:
1319+
try:
1320+
coro.send(None)
1321+
except StopIteration:
1322+
break
1323+
1324+
class CM:
1325+
async def __aenter__(self):
1326+
return self
1327+
1328+
async def __aexit__(self, *exc):
1329+
pass
1330+
1331+
async def send_with():
1332+
for i in range(100):
1333+
async with CM():
1334+
x = 1
1335+
1336+
run_async(send_with())
1337+
# Note there are still unspecialized "SEND" opcodes in the
1338+
# cleanup paths of the 'with' statement.
1339+
self.assert_specialized(send_with, "SEND_GEN")
1340+
1341+
@cpython_only
1342+
@requires_specialization_ft
1343+
def test_send_yield_from(self):
1344+
def g():
1345+
yield None
1346+
1347+
def send_yield_from():
1348+
yield from g()
1349+
1350+
for i in range(100):
1351+
list(send_yield_from())
1352+
1353+
self.assert_specialized(send_yield_from, "SEND_GEN")
1354+
self.assert_no_opcode(send_yield_from, "SEND")
1355+
13141356
@cpython_only
13151357
@requires_specialization_ft
13161358
def test_to_bool(self):

Python/bytecodes.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1117,15 +1117,15 @@ dummy_func(
11171117
};
11181118

11191119
specializing op(_SPECIALIZE_SEND, (counter/1, receiver, unused -- receiver, unused)) {
1120-
#if ENABLE_SPECIALIZATION
1120+
#if ENABLE_SPECIALIZATION_FT
11211121
if (ADAPTIVE_COUNTER_TRIGGERS(counter)) {
11221122
next_instr = this_instr;
11231123
_Py_Specialize_Send(receiver, next_instr);
11241124
DISPATCH_SAME_OPARG();
11251125
}
11261126
OPCODE_DEFERRED_INC(SEND);
11271127
ADVANCE_ADAPTIVE_COUNTER(this_instr[1].counter);
1128-
#endif /* ENABLE_SPECIALIZATION */
1128+
#endif /* ENABLE_SPECIALIZATION_FT */
11291129
}
11301130

11311131
op(_SEND, (receiver, v -- receiver, retval)) {

Python/generated_cases.c.h

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

Python/specialize.c

+4-11
Original file line numberDiff line numberDiff line change
@@ -2627,28 +2627,21 @@ _Py_Specialize_Send(_PyStackRef receiver_st, _Py_CODEUNIT *instr)
26272627
{
26282628
PyObject *receiver = PyStackRef_AsPyObjectBorrow(receiver_st);
26292629

2630-
assert(ENABLE_SPECIALIZATION);
2630+
assert(ENABLE_SPECIALIZATION_FT);
26312631
assert(_PyOpcode_Caches[SEND] == INLINE_CACHE_ENTRIES_SEND);
2632-
_PySendCache *cache = (_PySendCache *)(instr + 1);
26332632
PyTypeObject *tp = Py_TYPE(receiver);
26342633
if (tp == &PyGen_Type || tp == &PyCoro_Type) {
26352634
if (_PyInterpreterState_GET()->eval_frame) {
26362635
SPECIALIZATION_FAIL(SEND, SPEC_FAIL_OTHER);
26372636
goto failure;
26382637
}
2639-
instr->op.code = SEND_GEN;
2640-
goto success;
2638+
specialize(instr, SEND_GEN);
2639+
return;
26412640
}
26422641
SPECIALIZATION_FAIL(SEND,
26432642
_PySpecialization_ClassifyIterator(receiver));
26442643
failure:
2645-
STAT_INC(SEND, failure);
2646-
instr->op.code = SEND;
2647-
cache->counter = adaptive_counter_backoff(cache->counter);
2648-
return;
2649-
success:
2650-
STAT_INC(SEND, success);
2651-
cache->counter = adaptive_counter_cooldown();
2644+
unspecialize(instr);
26522645
}
26532646

26542647
#ifdef Py_STATS

0 commit comments

Comments
 (0)