There are a number of places in Objects/stringobject.c where memory is
allocated for a string of length n using:
PyObject_MALLOC(sizeof(PyStringObject) + n)
On my computer (OS X 10.5.5/Intel), and, I suspect, on most common
platforms, the PyStringObject struct is going to contain some number of
bytes (probably 3) of trailing padding; the result is that the
PyObject_MALLOC call above asks for 3 more bytes than are necessary, and
on average the Python interpreter will waste 3 bytes of memory per string
allocation. Is there any reason not to replace these calls with:
PyObject_MALLOC(offsetof(PyStringObject, ob_sval) + n + 1)
instead?
Patch attached. |