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 23776cf
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 19 deletions.
22 changes: 22 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,27 @@ PyCell_GetRef(PyCellObject *cell)
return res;
}

static inline void
_PyCell_GetStackRef(PyCellObject *cell, _PyStackRef *value_addr)
{
PyObject *value = _Py_TryXGetRef(&cell->ob_ref);
if (value != NULL) {
if (_Py_IsImmortal(value) || _PyObject_HasDeferredRefcount(value)) {
*value_addr = (_PyStackRef){ .bits = (uintptr_t)value | Py_TAG_DEFERRED };
return;
}
*value_addr = PyStackRef_FromPyObjectSteal(value);
return;
}
// fallback
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.

7 changes: 3 additions & 4 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1647,14 +1647,13 @@ dummy_func(
value = PyStackRef_FromPyObjectSteal(value_o);
}

inst(LOAD_DEREF, ( -- value)) {
inst(LOAD_DEREF, ( -- value[1])) {
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
9 changes: 4 additions & 5 deletions Python/executor_cases.c.h

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

9 changes: 4 additions & 5 deletions Python/generated_cases.c.h

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

8 changes: 5 additions & 3 deletions Python/optimizer_cases.c.h

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

0 comments on commit 23776cf

Please sign in to comment.