[issue4445] String allocations waste 3 bytes of memory on average.

Mark Dickinson report at bugs.python.org
Thu Nov 27 13:15:01 CET 2008


New submission from Mark Dickinson <dickinsm at gmail.com>:

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.

----------
components: Interpreter Core
files: string_alloc.patch
keywords: patch
messages: 76495
nosy: marketdickinson
severity: normal
status: open
title: String allocations waste 3 bytes of memory on average.
type: performance
versions: Python 2.6, Python 2.7
Added file: http://bugs.python.org/file12140/string_alloc.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue4445>
_______________________________________


More information about the Python-bugs-list mailing list