8000 WIP · python/cpython@23d5d49 · GitHub
[go: up one dir, main page]

Skip to content

Commit 23d5d49

Browse files
committed
WIP
1 parent 58c2289 commit 23d5d49

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

Include/internal/pycore_typeobject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ typedef int (*_py_validate_type)(PyTypeObject *);
278278
// and if the validation is passed, it will set the ``tp_version`` as valid
279279
// tp_version_tag from the ``ty``.
280280
extern int _PyType_Validate(PyTypeObject *ty, _py_validate_type validate, unsigned int *tp_version);
281-
extern int _PyType_CacheGetItemForSpecialization(PyHeapTypeObject *ht, PyObject *descriptor);
281+
extern int _PyType_CacheGetItemForSpecialization(PyHeapTypeObject *ht, PyObject *descriptor, uint32_t version);
282282

283283
#ifdef __cplusplus
284284
}

Objects/typeobject.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5693,29 +5693,26 @@ _PyType_CacheInitForSpecialization(PyHeapTypeObject *type, PyObject *init,
56935693
}
56945694

56955695
int
5696-
_PyType_CacheGetItemForSpecialization(PyHeapTypeObject *ht, PyObject *descriptor)
5696+
_PyType_CacheGetItemForSpecialization(PyHeapTypeObject *ht, PyObject *descriptor, uint32_t version)
56975697
{
5698-
int ret = 0;
5698+
if (!de 8000 scriptor || !version) {
5699+
return 0;
5700+
}
5701+
int can_cache;
56995702
BEGIN_TYPE_LOCK();
57005703
// This pointer is invalidated by PyType_Modified (see the comment on
57015704
// struct _specialization_cache):
57025705
PyFunctionObject *func = (PyFunctionObject *)descriptor;
5706+
can_cache = _PyFunction_GetVersionForCurrentState(func) == version;
57035707
#ifdef Py_GIL_DISABLED
5704-
if (!_PyObject_HasDeferredRefcount(descriptor)) {
5705-
ret = -1;
5706-
goto end;
5707-
}
5708+
can_cache = can_cache && _PyObject_HasDeferredRefcount(descriptor);
57085709
#endif
5709-
uint32_t version = _PyFunction_GetVersionForCurrentState(func);
5710-
if (!_PyFunction_IsVersionValid(version)) {
5711-
ret = -1;
5712-
goto end;
5710+
if (can_cache) {
5711+
FT_ATOMIC_STORE_PTR_RELEASE(ht->_spec_cache.getitem, descriptor);
5712+
FT_ATOMIC_STORE_UINT32_RELAXED(ht->_spec_cache.getitem_version, version);
57135713
}
5714-
FT_ATOMIC_STORE_PTR_RELEASE(ht->_spec_cache.getitem, descriptor);
5715-
FT_ATOMIC_STORE_UINT32_RELAXED(ht->_spec_cache.getitem_version, version);
5716-
end:
57175714
END_TYPE_LOCK();
5718-
return ret;
5715+
return can_cache;
57195716
}
57205717

57215718
static void

Python/specialize.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,36 +1763,41 @@ _Py_Specialize_BinarySubscr(
17631763
specialized_op = BINARY_SUBSCR_DICT;
17641764
goto success;
17651765
}
1766-
PyObject *descriptor = _PyType_Lookup(container_type, &_Py_ID(__getitem__));
1766+
unsigned int version;
1767+
PyObject *descriptor = _PyType_LookupRefAndVersion(container_type, &_Py_ID(__getitem__), &version);
17671768
if (descri 8000 ptor && Py_TYPE(descriptor) == &PyFunction_Type) {
17681769
if (!(container_type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
17691770
SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_SUBSCR_NOT_HEAP_TYPE);
1771+
Py_DECREF(descriptor);
17701772
goto fail;
17711773
}
17721774
PyFunctionObject *func = (PyFunctionObject *)descriptor;
17731775
PyCodeObject *fcode = (PyCodeObject *)func->func_code;
17741776
int kind = function_kind(fcode);
17751777
if (kind != SIMPLE_FUNCTION) {
17761778
SPECIALIZATION_FAIL(BINARY_SUBSCR, kind);
1779+
Py_DECREF(descriptor);
17771780
goto fail;
17781781
}
17791782
if (fcode->co_argcount != 2) {
17801783
SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_WRONG_NUMBER_ARGUMENTS);
1784+
Py_DECREF(descriptor);
17811785
goto fail;
17821786
}
17831787

17841788
PyHeapTypeObject *ht = (PyHeapTypeObject *)container_type;
1785-
if (_PyType_CacheGetItemForSpecialization(ht, descriptor) < 0) {
1786-
SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_OUT_OF_VERSIONS);
1787-
goto fail;
1788-
}
17891789
/* Don't specialize if PEP 523 is active */
17901790
if (_PyInterpreterState_GET()->eval_frame) {
17911791
SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_OTHER);
1792+
Py_DECREF(descriptor);
17921793
goto fail;
17931794
}
1794-
specialized_op = BINARY_SUBSCR_GETITEM;
1795-
goto success;
1795+
if (_PyType_CacheGetItemForSpecialization(ht, descriptor, (uint32_t)version)) {
1796+
specialized_op = BINARY_SUBSCR_GETITEM;
1797+
Py_DECREF(descriptor);
1798+
goto success;
1799+
}
1800+
Py_DECREF(descriptor);
17961801
}
17971802
SPECIALIZATION_FAIL(BINARY_SUBSCR,
17981803
binary_subscr_fail_kind(container_type, sub));

0 commit comments

Comments
 (0)
0