views:

38

answers:

1

Hi, the new object structure in 1.9 embeds some ivars into objects for faster access:

#define ROBJECT_EMBED_LEN_MAX 3
struct RObject {
    struct RBasic basic;
    union {
        struct {
            long numiv;
            VALUE *ivptr;
            struct st_table *iv_index_tbl; 
        } heap;
        VALUE ary[ROBJECT_EMBED_LEN_MAX];
    } as;
}; 

My question is are the first 3 ivars always embedded? or are they only embedded if the number of ivars is <=3 ?

I've tried reading the source but find it next to incomprehensible.

Thanks

+1  A: 

The instance variable heap (called heap) and the embedded instance variables (called ary) are contained in a union. You'll also find some macros defined below the snippit you quoted that all look like:

#define ROBJECT_IVPTR(o) \
    ((RBASIC(o)->flags & ROBJECT_EMBED) ? \
     ROBJECT(o)->as.ary : \
     ROBJECT(o)->as.heap.ivptr)

Key in all these is RBASIC(o)->flags & ROBJECT_EMBED. The ROBJECT_EMBED flag indicates whether embedded instance variables are in use, or the heap is in use.

So embedded variables are only used when the number of instance variables is <= 3.

Shtééf
this is what i suspected but i needed confirmation! thanks a lot!
banister