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

Simon Urbanek simon.urbanek at r-project.org
Thu Aug 29 20:12:10 CEST 2013


On Aug 29, 2013, at 12:08 PM, Hadley Wickham wrote:

>> 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).
> 

Not to be picky, but that is not the point. The alignment is due to the attrib pointer which is at offset 8 despite the fact that there is only a 4-byte element in front of it. Maybe for better illustration, this is the layout on 64-bit machines:

* 4 bytes: sxpinfo header (= 32 bits)
* 4 bytes: --- padding so next ptr is aligned ---
* 8 bytes: pointer to attributes
* 8 bytes: pointer to next node
* 8 bytes: pointer to previous node
* 4 bytes: length
* 4 bytes: true length

= 40 bytes

This is already aligned so the payload alignment doesn't any extra padding so that has no effect at all.



>> 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/
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 
> 



More information about the R-devel mailing list