Skip to content

Commit 57526e8

Browse files
corona10colesbury
authored andcommitted
gh-123358: Update LOAD_DEREF to use stackref and atomic incref
1 parent e094420 commit 57526e8

File tree

6 files changed

+42
-12
lines changed

6 files changed

+42
-12
lines changed

Include/internal/pycore_cell.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define Py_INTERNAL_CELL_H
33

44
#include "pycore_critical_section.h"
5+
#include "pycore_object.h"
56

67
#ifdef __cplusplus
78
extern "C" {
@@ -19,7 +20,7 @@ PyCell_SwapTakeRef(PyCellObject *cell, PyObject *value)
1920
PyObject *old_value;
2021
Py_BEGIN_CRITICAL_SECTION(cell);
2122
old_value = cell->ob_ref;
22-
cell->ob_ref = value;
23+
FT_ATOMIC_STORE_PTR_RELEASE(cell->ob_ref, value);
2324
Py_END_CRITICAL_SECTION();
2425
return old_value;
2526
}
@@ -42,6 +43,28 @@ PyCell_GetRef(PyCellObject *cell)
4243
return res;
4344
}
4445

46+
static inline
47+
_PyStackRef _PyCell_GetStackRef(PyCellObject *cell)
48+
{
49+
PyObject *value;
50+
#ifdef Py_GIL_DISABLED
51+
value = _Py_atomic_load_ptr(&cell->ob_ref);
52+
if (value != NULL) {
53+
if (_Py_IsImmortal(value) || _PyObject_HasDeferredRefcount(value)) {
54+
return (_PyStackRef){ .bits = (uintptr_t)value | Py_TAG_DEFERRED };
55+
}
56+
if (_Py_TryIncrefFast(value)) {
57+
return _PyStackRef_FromPyObjectSteal(value);
58+
}
59+
}
60+
#endif
61+
value = PyCell_GetRef(cell);
62+
if (value == NULL) {
63+
return PyStackRef_NULL;
64+
}
65+
return PyStackRef_FromPyObjectSteal(value);
66+
}
67+
4568
#ifdef __cplusplus
4669
}
4770
#endif

Include/internal/pycore_opcode_metadata.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_uop_metadata.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/bytecodes.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,12 +1811,11 @@ dummy_func(
18111811

18121812
inst(LOAD_DEREF, ( -- value)) {
18131813
PyCellObject *cell = (PyCellObject *)PyStackRef_AsPyObjectBorrow(GETLOCAL(oparg));
1814-
PyObject *value_o = PyCell_GetRef(cell);
1815-
if (value_o == NULL) {
1814+
value = _PyCell_GetStackRef(cell);
1815+
if (PyStackRef_IsNull(value)) {
18161816
_PyEval_FormatExcUnbound(tstate, _PyFrame_GetCode(frame), oparg);
18171817
ERROR_IF(true, error);
18181818
}
1819-
value = PyStackRef_FromPyObjectSteal(value_o);
18201819
}
18211820

18221821
inst(STORE_DEREF, (v --)) {

Python/executor_cases.c.h

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

Lines changed: 7 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)