3 ms·
TXR Lisp, 64 bit: 1> (defstruct blank ()) #<struct-type blank> 2> (pprof (new blank)) malloc bytes: 16 gc heap bytes: 32 total
by kazinator 7y ago
TXR Lisp, 64 bit:
1> (defstruct blank ())
#<struct-type blank>
2> (pprof (new blank))
malloc bytes: 16
gc heap bytes: 32
total: 48
milliseconds: 0
#S(blank)
32 bit:
2> (pprof (new blank))
malloc bytes: 8
gc heap bytes: 16
total: 24
milliseconds: 0
#S(blank)
The structure instance has a pointer to its type, followed by a numeric ID (which is also in the type, but is forwarded to the instance for faster access). The ID is combined with a slot symbol to perform a cache lookup to get the offset of a slot. The numeric ID is a fixnum, which leaves a few spare bits for a couple of flags:
struct struct_inst {
struct struct_type *type;
cnum id : sizeof (cnum) * CHAR_BIT - TAG_SHIFT;
unsigned lazy : 1;
unsigned dirty : 1;
val slot[1];
};
If someone wanted to shrink this, they could patch the code so that the dirty flag support, and lazy instantiation of structs is made optional (as in compiled out), and so is the forwarding of the inst->type->id to inst->id. This struct type is not known outside of struct.c, which is only some 1700 lines long. If you take out the id member from struct_inst, the C compiler will find all the places that have to be fixed up; literally a 15 minute job.
I can also think of a more substantial refactoring that would eliminate the type pointer also. There is no room in the heap object handle to store it directly: heap handles have four words in them; the COBJ ones used for structs have the type field, a class symbol, a pointer to an operations structure, and a pointer to some associated object (in this case struct_inst). All structures share the same operations structure. However, if we dynamically allocate that operations structure for each struct type, we could stick the type pointer in there, taking it out of the instance. Thus an instance size could literally just be sizeof(pointer) * no-of-instance-slots.