8000 gh-115999: Enable BINARY_SUBSCR_GETITEM for free-threaded build by corona10 · Pull Request #127737 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-115999: Enable BINARY_SUBSCR_GETITEM for free-threaded build #127737

New issue 8000

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Dec 19, 2024
Prev Previous commit
Next Next commit
WIP
  • Loading branch information
corona10 committed Dec 16, 2024
commit 23d5d49b262fcb51edab11edb5a13b7e5f53454e
2 changes: 1 addition & 1 deletion Include/internal/pycore_typeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ typedef int (*_py_validate_type)(PyTypeObject *);
// and if the validation is passed, it will set the ``tp_version`` as valid
// tp_version_tag from the ``ty``.
extern int _PyType_Validate(PyTypeObject *ty, _py_validate_type validate, unsigned int *tp_version);
extern int _PyType_CacheGetItemForSpecialization(PyHeapTypeObject *ht, PyObject *descriptor);
extern int _PyType_CacheGetItemForSpecialization(PyHeapTypeObject *ht, PyObject *descriptor, uint32_t version);

#ifdef __cplusplus
}
Expand Down
25 changes: 11 additions & 14 deletions Objects/typeobject.c
8000
Original file line number Diff line number Diff line change
Expand Up @@ -5693,29 +5693,26 @@ _PyType_CacheInitForSpecialization(PyHeapTypeObject *type, PyObject *init,
}

int
_PyType_CacheGetItemForSpecialization(PyHeapTypeObject *ht, PyObject *descriptor)
_PyType_CacheGetItemForSpecialization(PyHeapTypeObject *ht, PyObject *descriptor, uint32_t version)
{
int ret = 0;
if (!descriptor || !version) {
return 0;
}
int can_cache;
BEGIN_TYPE_LOCK();
// This pointer is invalidated by PyType_Modified (see the comment on
// struct _specialization_cache):
PyFunctionObject *func = (PyFunctionObject *)descriptor;
can_cache = _PyFunction_GetVersionForCurrentState(func) == version;
#ifdef Py_GIL_DISABLED
if (!_PyObject_HasDeferredRefcount(descriptor)) {
ret = -1;
goto end;
}
can_cache = can_cache && _PyObject_HasDeferredRefcount(descriptor);
#endif
uint32_t version = _PyFunction_GetVersionForCurrentState(func);
if (!_PyFunction_IsVersionValid(version)) {
ret = -1;
goto end;
if (can_cache) {
FT_ATOMIC_STORE_PTR_RELEASE(ht->_spec_cache.getitem, descriptor);
FT_ATOMIC_STORE_UINT32_RELAXED(ht->_spec_cache.getitem_version, version);
}
FT_ATOMIC_STORE_PTR_RELEASE(ht->_spec_cache.getitem, descriptor);
FT_ATOMIC_STORE_UINT32_RELAXED(ht->_spec_cache.getitem_version, version);
end:
END_TYPE_LOCK();
return ret;
return can_cache;
}

static void
Expand Down
19 changes: 12 additions & 7 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -1763,36 +1763,41 @@ _Py_Specialize_BinarySubscr(
specialized_op = BINARY_SUBSCR_DICT;
goto success;
}
PyObject *descriptor = _PyType_Lookup(container_type, &_Py_ID(__getitem__));
unsigned int version;
PyObject *descriptor = _PyType_LookupRefAndVersion(container_type, &_Py_ID(__getitem__), &version);
if (descriptor && Py_TYPE(descriptor) == &PyFunction_Type) {
if (!(container_type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_SUBSCR_NOT_HEAP_TYPE);
Py_DECREF(descriptor);
goto fail;
}
PyFunctionObject *func = (PyFunctionObject *)descriptor;
PyCodeObject *fcode = (PyCodeObject *)func->func_code;
int kind = function_kind(fcode);
if (kind != SIMPLE_FUNCTION) {
SPECIALIZATION_FAIL(BINARY_SUBSCR, kind);
Py_DECREF(descriptor);
goto fail;
}
if (fcode->co_argcount != 2) {
SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_WRONG_NUMBER_ARGUMENTS);
Py_DECREF(descriptor);
goto fail;
}

PyHeapTypeObject *ht = (PyHeapTypeObject *)container_type;
if (_PyType_CacheGetItemForSpecialization(ht, descriptor) < 0) {
SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_OUT_OF_VERSIONS);
goto fail;
}
/* Don't specialize if PEP 523 is active */
if (_PyInterpreterState_GET()->eval_frame) {
SPECIALIZATION_FAIL(BINARY_SUBSCR, SPEC_FAIL_OTHER);
Py_DECREF(descriptor);
goto fail;
}
specialized_op = BINARY_SUBSCR_GETITEM;
goto success;
if (_PyType_CacheGetItemForSpecialization(ht, descriptor, (uint32_t)version)) {
specialized_op = BINARY_SUBSCR_GETITEM;
Py_DECREF(descriptor);
goto success;
}
Py_DECREF(descriptor);
}
SPECIALIZATION_FAIL(BINARY_SUBSCR,
binary_subscr_fail_kind(container_type, sub));
Expand Down
Loading
0