10000 gh-121459: Deferred LOAD_ATTR (methods) by Fidget-Spinner · Pull Request #124101 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-121459: Deferred LOAD_ATTR (methods) #124101

New issue

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Partially address review
Co-Authored-By: Sam Gross <655866+colesbury@users.noreply.github.com>
  • Loading branch information
Fidget-Spinner and colesbury committed Nov 14, 2024
commit ba19770f28c210109c9b6113d53e728f0848a888
2 changes: 1 addition & 1 deletion Include/internal/pycore_dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ PyAPI_FUNC(int) _PyDict_SetItem_KnownHash_LockHeld(PyDictObject *mp, PyObject *k
PyAPI_FUNC(int) _PyDict_GetItemRef_KnownHash_LockHeld(PyDictObject *op, PyObject *key, Py_hash_t hash, PyObject **result);
extern int _PyDict_GetItemRef_KnownHash(PyDictObject *op, PyObject *key, Py_hash_t hash, PyObject **result);
extern int _PyDict_GetItemRef_Unicode_LockHeld(PyDictObject *op, PyObject *key, PyObject **result);
extern int _PyDict_GetItem_KnownHash_StackRef(PyDictObject *op, PyObject *key, Py_hash_t hash, _PyStackRef *result);
extern int _PyDict_GetItemStackRef_KnownHash(PyDictObject *op, PyObject *key, Py_hash_t hash, _PyStackRef *result);
extern int _PyObjectDict_SetItem(PyTypeObject *tp, PyObject *obj, PyObject **dictptr, PyObject *name, PyObject *value);
extern int _PyDict_GetItemStackRef(PyObject *op, PyObject *key, _PyStackRef *result);

Expand Down
18 changes: 18 additions & 0 deletions Include/internal/pycore_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,24 @@ _Py_TryXGetRef(PyObject **ptr)
return NULL;
}

// This belongs here rather than pycore_stackref.h because including pycore_object.h
// there causes a circular include.
static inline _PyStackRef
_Py_TryXGetStackRef(PyObject **ptr)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add clear comments as to what this does.
What is it "trying" to do? What happens if it fails? etc.

{
PyObject *value = _Py_atomic_load_ptr(ptr);
if (value == NULL) {
return PyStackRef_NULL;
}
if (_Py_IsImmortal(value) || _PyObject_HasDeferredRefcount(value)) {
return (_PyStackRef){ .bits = (uintptr_t)value | Py_TAG_DEFERRED };
}
if (_Py_TryIncrefCompare(ptr, value)) {
return PyStackRef_FromPyObjectSteal(value);
}
return PyStackRef_NULL;
}

/* Like Py_NewRef but also optimistically sets _Py_REF_MAYBE_WEAKREF
on objects owned by a different thread. */
static inline PyObject *
Expand Down
25 changes: 6 additions & 19 deletions Objects/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2337,7 +2337,7 @@ _PyDict_GetItemRef_KnownHash(PyDictObject *op, PyObject *key, Py_hash_t hash, Py
}

int
_PyDict_GetItem_KnownHash_StackRef(PyDictObject *op, PyObject *key, Py_hash_t hash, _PyStackRef *result)
_PyDict_GetItemStackRef_KnownHash(PyDictObject *op, PyObject *key, Py_hash_t hash, _PyStackRef *result)
{
Py_ssize_t ix = _Py_dict_lookup_threadsafe_stackref(op, key, hash, result);
assert(ix >= 0 || PyStackRef_IsNull(*result));
Expand Down Expand Up @@ -2384,7 +2384,7 @@ _PyDict_GetItemStackRef(PyObject *op, PyObject *key, _PyStackRef *result)
return -1;
}

return _PyDict_GetItem_KnownHash_StackRef((PyDictObject *)op, key, hash, result);
return _PyDict_GetItemStackRef_KnownHash((PyDictObject *)op, key, hash, result);
}

int
Expand Down Expand Up @@ -7148,14 +7148,8 @@ _PyObject_TryGetInstanceAttributeStackRef(PyObject *obj, PyObject *name, _PyStac
}

#ifdef Py_GIL_DISABLED
PyObject *value = _Py_atomic_load_ptr_acquire(&values->values[ix]);
if (value == NULL) {
*attr = PyStackRef_NULL;
}
else {
*attr = PyStackRef_FromPyObjectNew(value);
}
if (value == _Py_atomic_load_ptr_acquire(&values->values[ix])) {
*attr = _Py_TryXGetStackRef(&values->values[ix]);
if (PyStackRef_AsPyObjectBorrow(*attr) == _Py_atomic_load_ptr_acquire(&values->values[ix])) {
return true;
}

Expand All @@ -7170,13 +7164,7 @@ _PyObject_TryGetInstanceAttributeStackRef(PyObject *obj, PyObject *name, _PyStac
if (dict == NULL) {
// Still no dict, we can read from the values
assert(values->valid);
value = values->values[ix];
if (value != NULL) {
*attr = PyStackRef_FromPyObjectNew(value);
}
else {
*attr = PyStackRef_NULL;
}
*attr = _Py_TryXGetStackRef(&values->values[ix]);
success = true;
}

Expand All @@ -7195,8 +7183,7 @@ _PyObject_TryGetInstanceAttributeStackRef(PyObject *obj, PyObject *name, _PyStac
Py_BEGIN_CRITICAL_SECTION(dict);

if (dict->ma_values == values && FT_ATOMIC_LOAD_UINT8(values->valid)) {
value = _Py_atomic_load_ptr_relaxed(&values->values[ix]);
*attr = PyStackRef_FromPyObjectNew(value);
*attr = _Py_TryXGetStackRef(&values->values[ix]);
success = true;
} else {
// Caller needs to lookup from the dictionary
Expand Down
8 changes: 7 additions & 1 deletion Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -1636,7 +1636,13 @@ _PyObject_GetMethodStackRef(PyObject *obj, PyObject *name, _PyStackRef *method)
}

if (tp->tp_getattro != PyObject_GenericGetAttr || !PyUnicode_CheckExact(name)) {
*method = PyStackRef_FromPyObjectSteal(PyObject_GetAttr(obj, name));
PyObject *attr_o = PyObject_GetAttr(obj, name);
if (attr_o != NULL) {
*method = PyStackRef_FromPyObjectSteal(attr_o);
}
else {
*method = PyStackRef_NULL;
}
return 0;
}

Expand Down
5 changes: 3 additions & 2 deletions Objects/typeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -5489,11 +5489,12 @@ find_name_in_mro_stackref(PyTypeObject *type, PyObject *name, int *error, _PySta
PyObject *base = PyTuple_GET_ITEM(mro, i);
PyObject *dict = lookup_tp_dict(_PyType_CAST(base));
assert(dict && PyDict_Check(dict));
if (_PyDict_GetItem_KnownHash_StackRef((PyDictObject *)dict, name, hash, result) < 0) {
int code = _PyDict_GetItemStackRef_KnownHash((PyDictObject *)dict, name, hash, result);
if (code < 0) {
*error = -1;
goto done;
}
if (!PyStackRef_IsNull(*result)) {
if (code == 1) {
break;
}
}
Expand Down
0