Skip to content

Commit c44a7d9

Browse files
authored
Merge pull request #371 from thisisthepy/feat/tp-new-fixed-direct
TypedPython: fill a fresh New's slots by direct release stores (#125)
2 parents 9394936 + 8ccfda4 commit c44a7d9

4 files changed

Lines changed: 64 additions & 3 deletions

File tree

‎docs/design/typedpython.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,34 @@ GC 추적), refcount 증감, 해제(`subtype_dealloc`)를 그대로 치르기
221221
더 가려면 객체를 아예 만들지 않거나(가상 객체), 증감을 빼거나(refcount 생략), 한꺼번에 할당·해제해야
222222
한다(영역 할당). 셋 다 `id()`, `__del__` 시점, `gc` 관측과 부딪칠 수 있다.
223223

224+
**실제 객체(N-11)의 생성 비용 (#125, 2026-11-05).** 프로파일(`benchmarks/typedpython/results/2026-10-25-binary-trees-alloc-profile.md`)
225+
가운데 관측을 바꾸지 않고 줄일 수 있는 것은 다음과 같이 처리했다.
226+
- 호출마다 스레드 상태를 한 번만 읽는다(#273).
227+
- `tp_new_fixed` 는 새 객체의 슬롯을 `PyMember_SetOne` 대신 release store 로 직접 채운다. `SetOne` 이
228+
`Py_T_OBJECT_EX` 슬롯에 하는 일(객체 critical section 안에서 `Py_XNewRef(v)` 의 release store, 그 뒤 옛 값
229+
`Py_XDECREF`)에서 critical section 과 옛 값 해제만 뺀 것이다. 옛 값은 `tp_alloc` 이 0 으로 채웠으므로 NULL 이다.
230+
critical section 을 빼도 되는 근거: `tp_alloc` 부터 마지막 슬롯 저장까지 스레드 상태를 놓는 호출도, eval
231+
breaker 검사도 없어서 safepoint 가 없다. 그래서 stop-the-world 가 필요한 `gc.get_objects()` /
232+
`gc.get_referrers()` 는 채우기가 끝나기 전에 이 객체를 다른 스레드에 줄 수 없다. 오히려 기존 critical
233+
section 은 경합 시 스레드 상태를 놓을 수 있어서, 반쯤 채운 객체가 보이는 실행이 가능했다(그것도 인터프리터의
234+
`__init__` 실행과 같은 상태라 유효했다). 슬롯의 종류·쓰기 가능·오프셋 범위는 `tp_class_capture` 가 init 에서
235+
확인한다. 참조 수: 슬롯은 `Py_NewRef` 로 자기 참조를 갖는다(이것을 빼는 변이는 `test_cgen_classes.py` 에서
236+
segfault 로 잡힌다). 측정(참고용, load 24~27): binary-trees `check(make(18))` 의 CPU 시간 중앙값이 develop
237+
대비 1.08배 빠르다. 같은 부하에서 벽시계 시간은 한 회차 안에서도 2~22초로 흔들려 쓸 수 없었다.
238+
239+
하지 않은 것과 이유:
240+
- **타입별 freelist.** 해제된 객체의 메모리를 붙잡으려면 클래스의 `tp_dealloc` 를 바꿔야 한다. 이것은
241+
2026-10-25 결정(#125 댓글)에서 미뤘다: 클래스가 뒤에 `__del__`, `__weakref__`, 서브클래스를 얻으면 바뀐 dealloc 이
242+
관측된다. free-threaded 빌드에서는 해제된 GC 객체의 메모리를 다른 스레드가 lock-free 로 읽을 수 있어서
243+
(`_Py_TryIncref` 계열) CPython 은 mimalloc 의 지연 해제(QSBR)에 기댄다. 확장이 따로 붙잡는 freelist 는 이 보장
244+
밖에 있으므로, 이 결정을 다시 열 때 그 근거도 함께 써야 한다.
245+
- **GC 추적을 늦추기/생략.** 아직 사이클을 만들 수 없는 노드(자식이 `None`)를 추적하지 않는 방식은
246+
`gc.is_tracked`, `gc.get_objects()` 로 관측된다. 그리고 인터프리터가 나중에 슬롯에 자기 자신을 넣으면
247+
(`PyMember_SetOne` 은 추적을 다시 켜지 않는다) 사이클이 수거되지 않는다. 컬렉션 횟수를 줄이는 방식(할당 계수
248+
묶기, 임계값 조정)은 컬렉션 시점을 바꾼다. 둘 다 SPEC N-12 의 "GC 시점" 판단에 속하므로 오너 결정 사항이다.
249+
- binary-trees 의 `main` 은 정책(#150)으로 컴파일되지 않고 순수하지도 않아서(`print`, `sys.argv`), 실제 객체를
250+
아예 없애는 N-12b 영역이 이 벤치마크에는 적용되지 않는다.
251+
224252
**동일성의 정의.** 컴파일된 코드의 모든 실행은 **어떤 유효한 CPython 실행과 구별할 수 없어야 한다.**
225253
구별은 관측으로만 할 수 있으므로, 설계는 "관측 지점" 목록과 "관측 지점에 닿기 전에 실제 객체로 되돌린다"는
226254
규칙으로 이루어진다. CPython 자체가 정하지 않는 것(스레드 전환 시점)은 CPython 이 고를 수 있는 실행 중

‎python-multiplatform-ksp/src/main/python/typedpython/runtime/API.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,8 @@ Slot access and the slow paths:
389389
int tp_field_set(PyObject *obj, PyMemberDef *m, PyObject *v); /* PyMember_SetOne: 0 / -1 */
390390
int tp_setattr(PyObject *obj, PyObject *name, PyObject *v); /* STORE_ATTR (PyObject_SetAttr): 0 / -1 */
391391
PyObject *tp_new_fixed(PyTypeObject *cls, PyMemberDef *const *slots, PyObject *const *values, Py_ssize_t n);
392-
/* cls->tp_alloc(cls, 0) then set each slot (new references); NULL + MemoryError */
392+
/* cls->tp_alloc(cls, 0) then a release store of a new reference into each slot
393+
(the fresh object is unreachable until it returns, #125); NULL + MemoryError */
393394
int tp_is_exact(PyObject *obj, PyTypeObject *cls); /* Py_IS_TYPE */
394395
FieldGet's slow path is `tp_getattr`, New's is `tp_global` + `tp_call` (the binding loaded before
395396
the arguments), IsExact's compares with `tp_global`; in a pure function every slow path is a deopt.

‎python-multiplatform-ksp/src/main/python/typedpython/runtime/tp_runtime.h‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2756,7 +2756,22 @@ static inline int tp_setattr(PyObject *obj, PyObject *name, PyObject *v)
27562756

27572757
/* The observable result of a trivial __init__: allocate with cls's tp_alloc and set slots[i] to
27582758
* values[i] (new references; the caller keeps its own). NULL with the error set on failure; the
2759-
* half-built object is released (its unset slots are NULL, which dealloc handles). */
2759+
* half-built object is released (its unset slots are NULL, which dealloc handles).
2760+
*
2761+
* Issue #125: each slot is filled by a release store of a new reference, which is what
2762+
* PyMember_SetOne does for a Py_T_OBJECT_EX slot (FT_ATOMIC_STORE_PTR_RELEASE of Py_XNewRef(v)
2763+
* under the object's critical section, then Py_XDECREF of the old value), minus the critical section
2764+
* and the old value. Why that is the same result:
2765+
* - every slot was checked at capture (tp_class_capture): Py_T_OBJECT_EX, writable, offset inside
2766+
* tp_basicsize; so SetOne's READONLY and type dispatch are decided already;
2767+
* - tp_alloc zeroed the object, so the old value is NULL and there is nothing to release;
2768+
* - nothing between tp_alloc and the last store can reach a safepoint (no call that detaches the
2769+
* thread state, no eval-breaker check), so no stop-the-world (gc.get_objects, gc.get_referrers)
2770+
* can hand the object to another thread before it is filled. The critical section, which could
2771+
* detach when contended, protected nothing here. A reader that did find it (none can) would see
2772+
* NULL or the stored value, as with SetOne's release store.
2773+
* A NULL value (the generator never passes one) keeps SetOne's result: AttributeError for deleting
2774+
* an unset Py_T_OBJECT_EX slot. */
27602775
static inline PyObject *tp_new_fixed(PyTypeObject *cls, PyMemberDef *const *slots,
27612776
PyObject *const *values, Py_ssize_t n)
27622777
{
@@ -2767,10 +2782,13 @@ static inline PyObject *tp_new_fixed(PyTypeObject *cls, PyMemberDef *const *slot
27672782
return NULL;
27682783
}
27692784
for (i = 0; i < n; i++) {
2770-
if (PyMember_SetOne((char *)obj, slots[i], values[i]) < 0) {
2785+
if (values[i] == NULL) {
2786+
PyErr_SetString(PyExc_AttributeError, slots[i]->name);
27712787
Py_DECREF(obj);
27722788
return NULL;
27732789
}
2790+
__atomic_store_n((PyObject **)((char *)obj + slots[i]->offset), Py_NewRef(values[i]),
2791+
__ATOMIC_RELEASE);
27742792
}
27752793
return obj;
27762794
}

‎python-multiplatform-ksp/src/test/python/test_cgen_classes.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,20 @@ def test_gen_uses_only_the_runtime_helpers_for_slots():
428428
assert "PyMember_" not in text and "tp_alloc" not in text and "->tp_dictoffset" not in text
429429

430430

431+
def test_new_fixed_fills_its_fresh_object_with_direct_release_stores():
432+
"""#125: a New allocates its object and fills every slot before control can reach a safepoint, so
433+
no other thread holds the object yet. tp_new_fixed stores each new reference straight into the
434+
slot (release store, as PyMember_SetOne's free-threaded store) instead of taking the member
435+
protocol's per-object critical section for every slot."""
436+
from typedpython import cbuild
437+
h = (cbuild.RUNTIME_DIR / "tp_runtime.h").read_text()
438+
start = h.index("static inline PyObject *tp_new_fixed(")
439+
body = h[start:h.index("\n}\n", start)]
440+
assert "PyMember_SetOne" not in body and "CRITICAL_SECTION" not in body, body
441+
assert "__atomic_store_n(" in body and "__ATOMIC_RELEASE" in body, body
442+
assert "Py_NewRef(" in body, body # the slot owns its own reference
443+
444+
431445
# --- refcounts ---------------------------------------------------------------------------------------------
432446

433447
def test_reference_counts_are_unchanged_over_many_calls(m):

0 commit comments

Comments
 (0)