From h.wickham at gmail.com  Thu Aug 29 18:08:35 2013
From: h.wickham at gmail.com (Hadley Wickham)
Date: Thu, 29 Aug 2013 11:08:35 -0500
Subject: [Rd] Why does an empty vector occupy 40 bytes?
In-Reply-To: <521F6474.40307@stats.ox.ac.uk>
References: <CABdHhvFchkfQagqO1xJbEn9MbePFLO6VR4kaNhUYgzyB6Jp8tg@mail.gmail.com>
	<521F6474.40307@stats.ox.ac.uk>
Message-ID: <CABdHhvHZ2RYd_TGrehUSUt5xw6y-pgNbxQ8Tpu0jiAj145Z5KQ@mail.gmail.com>

> You have not: the start of the vector area also needs to be aligned (since
> it might hold doubles or CHARSXPs).

Thanks, that was the key point I was missing. (Also mentioned to me
off list by Luke Tierney).

> See memory.c, which says, inter alia
>
> /* All vector objects must be a multiple of sizeof(SEXPREC_ALIGN)
>    bytes so that alignment is preserved for all objects */
>
> /* Node Classes.  Non-vector nodes are of class zero. Small vector
>    nodes are in classes 1, ..., NUM_SMALL_NODE_CLASSES, and large
>    vector nodes are in class LARGE_NODE_CLASS.  For vector nodes the
>    node header is followed in memory by the vector data, offset from
>    the header by SEXPREC_ALIGN. */
>
> And also, object.size() is only approximate, and documented to be so. In
> fact many short vectors are using larger blocks of memory incompletely, and
> of course the OS is supplying memory in pages.

Yes, the size of an object is a nebulous concept. Do you want the
marginal size or the average size? Do you want to know how big the
object is, or the impact of the object on memory allocation?

Hadley

-- 
Chief Scientist, RStudio
http://had.co.nz/



[Rd] Why does an empty vector occupy 40 bytes?
Hadley Wickham