Debian Codegraph @

pypy/doc/discussion/rawrefcount.rst

raw
1 ======================
2 Rawrefcount and the GC
3 ======================
4
5
6 GC Interface
7 ------------
8
9 "PyObject" is a raw structure with at least two fields, ob_refcnt and
10 ob_pypy_link. The ob_refcnt is the reference counter as used on
11 CPython. If the PyObject structure is linked to a live PyPy object,
12 its current address is stored in ob_pypy_link and ob_refcnt is bumped
13 by either the constant REFCNT_FROM_PYPY, or the constant
14 REFCNT_FROM_PYPY_LIGHT (== REFCNT_FROM_PYPY + SOME_HUGE_VALUE)
15 (to mean "light finalizer").
16
17 Most PyPy objects exist outside cpyext, and conversely in cpyext it is
18 possible that a lot of PyObjects exist without being seen by the rest
19 of PyPy. At the interface, however, we can "link" a PyPy object and a
20 PyObject. There are two kinds of link:
21
22 rawrefcount.create_link_pypy(p, ob)
23
24 Makes a link between an existing object gcref 'p' and a newly
25 allocated PyObject structure 'ob'. ob->ob_refcnt must be
26 initialized to either REFCNT_FROM_PYPY, or
27 REFCNT_FROM_PYPY_LIGHT. (The second case is an optimization:
28 when the GC finds the PyPy object and PyObject no longer
29 referenced, it can just free() the PyObject.)
30
31 rawrefcount.create_link_pyobj(p, ob)
32
33 Makes a link from an existing PyObject structure 'ob' to a newly
34 allocated W_CPyExtPlaceHolderObject 'p'. You must also add
35 REFCNT_FROM_PYPY to ob->ob_refcnt. For cases where the PyObject
36 contains all the data, and the PyPy object is just a proxy. The
37 W_CPyExtPlaceHolderObject should have only a field that contains
38 the address of the PyObject, but that's outside the scope of the
39 GC.
40
41 rawrefcount.from_obj(p)
42
43 If there is a link from object 'p' made with create_link_pypy(),
44 returns the corresponding 'ob'. Otherwise, returns NULL.
45
46 rawrefcount.to_obj(Class, ob)
47
48 Returns ob->ob_pypy_link, cast to an instance of 'Class'.
49
50
51 Collection logic
52 ----------------
53
54 Objects existing purely on the C side have ob->ob_pypy_link == 0;
55 these are purely reference counted. On the other hand, if
56 ob->ob_pypy_link != 0, then ob->ob_refcnt is at least REFCNT_FROM_PYPY
57 and the object is part of a "link".
58
59 The idea is that links whose 'p' is not reachable from other PyPy
60 objects *and* whose 'ob->ob_refcnt' is REFCNT_FROM_PYPY or
61 REFCNT_FROM_PYPY_LIGHT are the ones who die. But it is more messy
62 because PyObjects still (usually) need to have a tp_dealloc called,
63 and this cannot occur immediately (and can do random things like
64 accessing other references this object points to, or resurrecting the
65 object).
66
67 Let P = list of links created with rawrefcount.create_link_pypy()
68 and O = list of links created with rawrefcount.create_link_pyobj().
69 The PyPy objects in the list O are all W_CPyExtPlaceHolderObject: all
70 the data is in the PyObjects, and all outsite references (if any) are
71 in C, as ``PyObject *`` fields.
72
73 So, during the collection we do this about P links:
74
75 .. code-block:: python
76
77 for (p, ob) in P:
78 if ob->ob_refcnt != REFCNT_FROM_PYPY
79 and ob->ob_refcnt != REFCNT_FROM_PYPY_LIGHT:
80 mark 'p' as surviving, as well as all its dependencies
81
82 At the end of the collection, the P and O links are both handled like
83 this:
84
85 .. code-block:: python
86
87 for (p, ob) in P + O:
88 if p is not surviving: # even if 'ob' might be surviving
89 unlink p and ob
90 if ob->ob_refcnt == REFCNT_FROM_PYPY_LIGHT:
91 free(ob)
92 elif ob->ob_refcnt > REFCNT_FROM_PYPY_LIGHT:
93 ob->ob_refcnt -= REFCNT_FROM_PYPY_LIGHT
94 else:
95 ob->ob_refcnt -= REFCNT_FROM_PYPY
96 if ob->ob_refcnt == 0:
97 invoke _Py_Dealloc(ob) later, outside the GC
98
99
100 GC Implementation
101 -----------------
102
103 We need two copies of both the P list and O list, for young or old
104 objects. All four lists can be regular AddressLists of 'ob' objects.
105
106 We also need an AddressDict mapping 'p' to 'ob' for all links in the P
107 list, and update it when PyPy objects move.
108
109
110 Further notes
111 -------------
112
113 XXX
114 XXX the rest is the ideal world, but as a first step, we'll look
115 XXX for the minimal tweaks needed to adapt the existing cpyext
116 XXX
117
118 For objects that are opaque in CPython, like <dict>, we always create
119 a PyPy object, and then when needed we make an empty PyObject and
120 attach it with create_link_pypy()/REFCNT_FROM_PYPY_LIGHT.
121
122 For <int> and <float> objects, the corresponding PyObjects contain a
123 "long" or "double" field too. We link them with create_link_pypy()
124 and we can use REFCNT_FROM_PYPY_LIGHT too: 'tp_dealloc' doesn't
125 need to be called, and instead just calling free() is fine.
126
127 For <type> objects, we need both a PyPy and a PyObject side. These
128 are made with create_link_pypy()/REFCNT_FROM_PYPY.
129
130 For custom PyXxxObjects allocated from the C extension module, we
131 need create_link_pyobj().
132
133 For <str> or <unicode> objects coming from PyPy, we use
134 create_link_pypy()/REFCNT_FROM_PYPY_LIGHT with a PyObject
135 preallocated with the size of the string. We copy the string
136 lazily into that area if PyString_AS_STRING() is called.
137
138 For <str>, <unicode>, <tuple> or <list> objects in the C extension
139 module, we first allocate it as only a PyObject, which supports
140 mutation of the data from C, like CPython. When it is exported to
141 PyPy we could make a W_CPyExtPlaceHolderObject with
142 create_link_pyobj().
143
144 For <tuple> objects coming from PyPy, if they are not specialized,
145 then the PyPy side holds a regular reference to the items. Then we
146 can allocate a PyTupleObject and store in it borrowed PyObject
147 pointers to the items. Such a case is created with
148 create_link_pypy()/REFCNT_FROM_PYPY_LIGHT. If it is specialized,
149 then it doesn't work because the items are created just-in-time on the
150 PyPy side. In this case, the PyTupleObject needs to hold real
151 references to the PyObject items, and we use create_link_pypy()/
152 REFCNT_FROM_PYPY. In all cases, we have a C array of PyObjects
153 that we can directly return from PySequence_Fast_ITEMS, PyTuple_ITEMS,
154 PyTuple_GetItem, and so on.
155
156 For <list> objects coming from PyPy, we can use a cpyext list
157 strategy. The list turns into a PyListObject, as if it had been
158 allocated from C in the first place. The special strategy can hold
159 (only) a direct reference to the PyListObject, and we can use either
160 create_link_pyobj() or create_link_pypy() (to be decided).
161 PySequence_Fast_ITEMS then works for lists too, and PyList_GetItem
162 can return a borrowed reference, and so on.