Reputation: 812
From my experiments, i see that inheritable objects start with 4 additional bytes (i have a 32 cpu). From this observation, i'd like to know:
Does it depend on the architecture? (ie. 8 bytes for 64 cpu)
How are these bytes used? It is one field or several ones?
I defined an heterogeneous container which copies objects except these four bytes using copyMem (i want contigious seqs without using variants). Should i be concerned about something?
Upvotes: 2
Views: 112
Reputation: 5403
A good way to figure out this kind of question is to look at the intermediate C file. I compiled this file:
type Foo = object {.inheritable.}
x: int
var a: Foo
echo sizeof(a)
After compiling it with nim -d:release c x
a look into nimcache/x.c
reveals:
struct Foo118004 {
TNimType* m_type;
NI x;
};
So there is simply a pointer to a TNimType
object stored. The size of the pointer and the alignment of the Foo
object are system and compiler dependent, but it should be 8 bytes for x86_64 and 4 bytes for x86. TNimType
itself can be found in lib/system/hti.nim
and is defined like this:
TNimType {.codegenType.} = object
size: int
kind: TNimKind
flags: set[TNimTypeFlag]
base: ptr TNimType
node: ptr TNimNode # valid for tyRecord, tyObject, tyTuple, tyEnum
finalizer: pointer # the finalizer for the type
marker: proc (p: pointer, op: int) {.nimcall, benign.} # marker proc for GC
deepcopy: proc (p: pointer): pointer {.nimcall, benign.}
Upvotes: 3