FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

list specialized for numbers · pythoncapi/pythoncapi@67708bc · GitHub

Commit 67708bc

Browse files
committed
list specialized for numbers
1 parent cfa9dd5 commit 67708bc

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

‎doc/optimization_ideas.rst‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,45 @@ PyPy experimented Software Transactional Memory (STM) but the project has
110110
been abandoned, `PyPy STM <http://doc.pypy.org/en/latest/stm.html>`_.
111111

112112

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+
113152
And more!
114153
=========
115154

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL