summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorBrandt Bucher <brandtbucher@microsoft.com>2022-02-28 11:54:14 (GMT)
committerGitHub <noreply@github.com>2022-02-28 11:54:14 (GMT)
commit424ecab494d538650ba34937cdd710094ccb2275 (patch)
treefe50c89dca09df2c3f2ff49cc937af75a9450c75 /Python
parentc32aef48533769161e1247927a5b418322e0860c (diff)
downloadcpython-424ecab494d538650ba34937cdd710094ccb2275.zip
cpython-424ecab494d538650ba34937cdd710094ccb2275.tar.gz
cpython-424ecab494d538650ba34937cdd710094ccb2275.tar.bz2
bpo-46841: Use inline caching for `UNPACK_SEQUENCE` (GH-31591)
Diffstat (limited to 'Python')
-rw-r--r--Python/ceval.c29
-rw-r--r--Python/specialize.c23
2 files changed, 26 insertions, 26 deletions
diff --git a/Python/ceval.c b/Python/ceval.c
index 4c0a71b..fe75782 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -2758,22 +2758,22 @@ handle_eval_breaker:
}
STACK_GROW(oparg);
Py_DECREF(seq);
+ JUMPBY(INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE);
DISPATCH();
}
TARGET(UNPACK_SEQUENCE_ADAPTIVE) {
assert(cframe.use_tracing == 0);
- SpecializedCacheEntry *cache = GET_CACHE();
- if (cache->adaptive.counter == 0) {
+ _PyUnpackSequenceCache *cache = (_PyUnpackSequenceCache *)next_instr;
+ if (cache->counter == 0) {
PyObject *seq = TOP();
next_instr--;
- _Py_Specialize_UnpackSequence(seq, next_instr, cache);
+ _Py_Specialize_UnpackSequence(seq, next_instr, oparg);
DISPATCH();
}
else {
STAT_INC(UNPACK_SEQUENCE, deferred);
- cache->adaptive.counter--;
- oparg = cache->adaptive.original_oparg;
+ cache->counter--;
JUMP_TO_INSTRUCTION(UNPACK_SEQUENCE);
}
}
@@ -2786,36 +2786,37 @@ handle_eval_breaker:
SET_TOP(Py_NewRef(PyTuple_GET_ITEM(seq, 1)));
PUSH(Py_NewRef(PyTuple_GET_ITEM(seq, 0)));
Py_DECREF(seq);
+ JUMPBY(INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE);
NOTRACE_DISPATCH();
}
TARGET(UNPACK_SEQUENCE_TUPLE) {
PyObject *seq = TOP();
- int len = GET_CACHE()->adaptive.original_oparg;
DEOPT_IF(!PyTuple_CheckExact(seq), UNPACK_SEQUENCE);
- DEOPT_IF(PyTuple_GET_SIZE(seq) != len, UNPACK_SEQUENCE);
+ DEOPT_IF(PyTuple_GET_SIZE(seq) != oparg, UNPACK_SEQUENCE);
STAT_INC(UNPACK_SEQUENCE, hit);
STACK_SHRINK(1);
PyObject **items = _PyTuple_ITEMS(seq);
- while (len--) {
- PUSH(Py_NewRef(items[len]));
+ while (oparg--) {
+ PUSH(Py_NewRef(items[oparg]));
}
Py_DECREF(seq);
+ JUMPBY(INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE);
NOTRACE_DISPATCH();
}
TARGET(UNPACK_SEQUENCE_LIST) {
PyObject *seq = TOP();
- int len = GET_CACHE()->adaptive.original_oparg;
DEOPT_IF(!PyList_CheckExact(seq), UNPACK_SEQUENCE);
- DEOPT_IF(PyList_GET_SIZE(seq) != len, UNPACK_SEQUENCE);
+ DEOPT_IF(PyList_GET_SIZE(seq) != oparg, UNPACK_SEQUENCE);
STAT_INC(UNPACK_SEQUENCE, hit);
STACK_SHRINK(1);
PyObject **items = _PyList_ITEMS(seq);
- while (len--) {
- PUSH(Py_NewRef(items[len]));
+ while (oparg--) {
+ PUSH(Py_NewRef(items[oparg]));
}
Py_DECREF(seq);
+ JUMPBY(INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE);
NOTRACE_DISPATCH();
}
@@ -5600,7 +5601,7 @@ MISS_WITH_CACHE(CALL)
MISS_WITH_INLINE_CACHE(BINARY_OP)
MISS_WITH_CACHE(COMPARE_OP)
MISS_WITH_CACHE(BINARY_SUBSCR)
-MISS_WITH_CACHE(UNPACK_SEQUENCE)
+MISS_WITH_INLINE_CACHE(UNPACK_SEQUENCE)
MISS_WITH_OPARG_COUNTER(STORE_SUBSCR)
LOAD_ATTR_INSTANCE_VALUE_miss:
diff --git a/Python/specialize.c b/Python/specialize.c
index 1624f19..b88c5d5 100644
--- a/Python/specialize.c
+++ b/Python/specialize.c
@@ -67,7 +67,6 @@ static uint8_t cache_requirements[256] = {
[PRECALL] = 2, /* _PyAdaptiveEntry and _PyObjectCache/_PyCallCache */
[STORE_ATTR] = 1, // _PyAdaptiveEntry
[COMPARE_OP] = 1, /* _PyAdaptiveEntry */
- [UNPACK_SEQUENCE] = 1, // _PyAdaptiveEntry
};
Py_ssize_t _Py_QuickenedCount = 0;
@@ -2133,39 +2132,39 @@ unpack_sequence_fail_kind(PyObject *seq)
#endif
void
-_Py_Specialize_UnpackSequence(PyObject *seq, _Py_CODEUNIT *instr,
- SpecializedCacheEntry *cache)
+_Py_Specialize_UnpackSequence(PyObject *seq, _Py_CODEUNIT *instr, int oparg)
{
- _PyAdaptiveEntry *adaptive = &cache->adaptive;
+ assert(_PyOpcode_InlineCacheEntries[UNPACK_SEQUENCE] ==
+ INLINE_CACHE_ENTRIES_UNPACK_SEQUENCE);
+ _PyUnpackSequenceCache *cache = (_PyUnpackSequenceCache *)(instr + 1);
if (PyTuple_CheckExact(seq)) {
- if (PyTuple_GET_SIZE(seq) != adaptive->original_oparg) {
+ if (PyTuple_GET_SIZE(seq) != oparg) {
SPECIALIZATION_FAIL(UNPACK_SEQUENCE, SPEC_FAIL_EXPECTED_ERROR);
goto failure;
}
if (PyTuple_GET_SIZE(seq) == 2) {
- *instr = _Py_MAKECODEUNIT(UNPACK_SEQUENCE_TWO_TUPLE,
- _Py_OPARG(*instr));
+ *instr = _Py_MAKECODEUNIT(UNPACK_SEQUENCE_TWO_TUPLE, oparg);
goto success;
}
- *instr = _Py_MAKECODEUNIT(UNPACK_SEQUENCE_TUPLE, _Py_OPARG(*instr));
+ *instr = _Py_MAKECODEUNIT(UNPACK_SEQUENCE_TUPLE, oparg);
goto success;
}
if (PyList_CheckExact(seq)) {
- if (PyList_GET_SIZE(seq) != adaptive->original_oparg) {
+ if (PyList_GET_SIZE(seq) != oparg) {
SPECIALIZATION_FAIL(UNPACK_SEQUENCE, SPEC_FAIL_EXPECTED_ERROR);
goto failure;
}
- *instr = _Py_MAKECODEUNIT(UNPACK_SEQUENCE_LIST, _Py_OPARG(*instr));
+ *instr = _Py_MAKECODEUNIT(UNPACK_SEQUENCE_LIST, oparg);
goto success;
}
SPECIALIZATION_FAIL(UNPACK_SEQUENCE, unpack_sequence_fail_kind(seq));
failure:
STAT_INC(UNPACK_SEQUENCE, failure);
- cache_backoff(adaptive);
+ cache->counter = ADAPTIVE_CACHE_BACKOFF;
return;
success:
STAT_INC(UNPACK_SEQUENCE, success);
- adaptive->counter = initial_counter_value();
+ cache->counter = initial_counter_value();
}
#ifdef Py_STATS