[Python-3000] Immutable bytes type and dbm modules

Guido van Rossum guido at python.org
Tue Aug 7 06:03:45 CEST 2007


On 8/6/07, Talin <talin at acm.org> wrote:
> Martin v. Löwis wrote:
> >> The most efficient representation of immutable bytes is quite different
> >> from the most efficient representation of mutable bytes.
> >
> > In what way?
>
> Well, in some runtime environments (I'm not sure about Python), for
> immutables you can combine the object header and the bytes array into a
> single allocation. Further, the header need not contain an explicit
> pointer to the bytes themselves, instead the bytes are obtained by doing
> pointer arithmetic on the header address.
>
> For a mutable bytes object, you'll need to allocate the actual bytes
> separately from the header. Typically you'll also need a second 'length'
> field to represent the current physical capacity of the allocated memory
> block, in addition to the logical length of the byte array.
>
> So in other words, the in-memory layout of the two structs is different
> enough that attempting to combine them into a single struct is kind of
> awkward.

Right. You've described exactly the difference between str8 and bytes
(PyString and PyBytes) in the struni branch (or in the future in
Python 2.6 for that matter).

There are two savings here: (1) the string object uses less memory
(only a single instance of the malloc header and round-off waste); (2)
the string object uses less time to allocate and free (only a single
call to malloc() or free()).

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list