| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -110,6 +110,45 @@ PyPy experimented Software Transactional Memory (STM) but the project has | |||
| 110 | 110 | been abandoned, `PyPy STM <http://doc.pypy.org/en/latest/stm.html>`_. | |
| 111 | 111 | ||
| 112 | 112 | ||
| 113 | + Specialized list for small integers | ||
| 114 | + =================================== | ||
| 115 | + | ||
| 116 | + If C extensions don't access structure members anymore, it becomes | ||
| 117 | + possible to modify the memory layout. | ||
| 118 | + | ||
| 119 | + For example, it's possible to design a specialized implementation of | ||
| 120 | + ``PyListObject`` for small integers:: | ||
| 121 | + | ||
| 122 | + typedef struct { | ||
| 123 | + PyVarObject ob_base; | ||
| 124 | + int use_small_int; | ||
| 125 | + PyObject **pyobject_array; | ||
| 126 | + int32_t *small_int_array; // <-- new compact C array for integers | ||
| 127 | + Py_ssize_t allocated; | ||
| 128 | + } PyListObject; | ||
| 129 | + | ||
| 130 | + PyObject* PyList_GET_ITEM(PyObject *op, Py_ssize_t index) | ||
| 131 | + { | ||
| 132 | + PyListObject *list = (PyListObject *)op; | ||
| 133 | + if (list->use_small_int) { | ||
| 134 | + int32_t item = list->small_int_array[index]; | ||
| 135 | + /* create a new object at each call */ | ||
| 136 | + return PyLong_FromLong(item); | ||
| 137 | + } | ||
| 138 | + else { | ||
| 139 | + return list->pyobject_array[index]; | ||
| 140 | + } | ||
| 141 | + } | ||
| 142 | + | ||
| 143 | + Each call to ``PyList_GET_ITEM()`` of this example creates a new temporary | ||
| 144 | + object which leads the memory leak (reference leak). This is one concrete | ||
| 145 | + example of issue with borrowed references. | ||
| 146 | + | ||
| 147 | + List specialized for numbers is just a example easy to understand to show that | ||
| 148 | + it becomes possible to modify PyObject structures. The main benefit of the | ||
| 149 | + memory footprint, but the overall on performances is unknown at this point. | ||
| 150 | + | ||
| 151 | + | ||
| 113 | 152 | And more! | |
| 114 | 153 | ========= | |
| 115 | 154 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments