Skip to content

Commit

Permalink
pythongh-123358: Update LOAD_DEREF to use stackref and atomic incref
Browse files Browse the repository at this point in the history
  • Loading branch information
corona10 committed Sep 25, 2024
1 parent 54dd77f commit d9e018c
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 11 deletions.
26 changes: 26 additions & 0 deletions Include/internal/pycore_cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define Py_INTERNAL_CELL_H

#include "pycore_critical_section.h"
#include "pycore_object.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -42,6 +43,31 @@ PyCell_GetRef(PyCellObject *cell)
return res;
}

static inline void
_PyCell_GetStackRef(PyCellObject *cell, _PyStackRef *value_addr)
{
PyObject *value;
#ifdef Py_GIL_DISABLED
value = _Py_atomic_load_ptr(&cell->ob_ref);
if (value != NULL) {
if (_Py_IsImmortal(value) || _PyObject_HasDeferredRefcount(value)) {
*value_addr = (_PyStackRef){ .bits = (uintptr_t)value | Py_TAG_DEFERRED };
return;
}
if (_Py_TryIncrefCompare(&cell->ob_ref, value)) {
*value_addr = _PyStackRef_FromPyObjectSteal(value);
return;
}
}
#endif
value = PyCell_GetRef(cell);
if (value == NULL) {
*value_addr = PyStackRef_NULL;
return;
}
*value_addr = PyStackRef_FromPyObjectSteal(value);
}

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion Include/internal/pycore_opcode_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Include/internal/pycore_uop_metadata.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1649,12 +1649,11 @@ dummy_func(

inst(LOAD_DEREF, ( -- value)) {
PyCellObject *cell = (PyCellObject *)PyStackRef_AsPyObjectBorrow(GETLOCAL(oparg));
PyObject *value_o = PyCell_GetRef(cell);
if (value_o == NULL) {
_PyCell_GetStackRef(cell, &value);
if (PyStackRef_IsNull(value)) {
_PyEval_FormatExcUnbound(tstate, _PyFrame_GetCode(frame), oparg);
ERROR_IF(true, error);
}
value = PyStackRef_FromPyObjectSteal(value_o);
}

inst(STORE_DEREF, (v --)) {
Expand Down
5 changes: 2 additions & 3 deletions Python/executor_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Python/generated_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d9e018c

Please sign in to comment.