Improve Array structure
MODIFY
include/mruby/array.h
include/mruby/array.h
@@ -41,13 +41,12 @@ struct RArray {
#define mrb_ary_value(p) mrb_obj_value((void*)(p))
#define RARRAY(v) ((struct RArray*)(mrb_ptr(v)))
-#define MRB_ARY_EMBED 4
-#define MRB_ARY_EMBED_MASK 3
-#define ARY_EMBED_P(a) ((a)->flags & MRB_ARY_EMBED)
-#define ARY_SET_EMBED_FLAG(a) ((a)->flags |= MRB_ARY_EMBED)
-#define ARY_UNSET_EMBED_FLAG(a) ((a)->flags &= ~(MRB_ARY_EMBED|MRB_ARY_EMBED_MASK))
-#define ARY_EMBED_LEN(a) ((mrb_int)((a)->flags & MRB_ARY_EMBED_MASK))
-#define ARY_SET_EMBED_LEN(a,len) (a)->flags = ((a)->flags&~MRB_ARY_EMBED_MASK) | ((len)&MRB_ARY_EMBED_MASK);
+#define MRB_ARY_EMBED_MASK 7
+#define ARY_EMBED_P(a) ((a)->flags & MRB_ARY_EMBED_MASK)
+//#define ARY_SET_EMBED_FLAG(a) ((a)->flags |= MRB_ARY_EMBED)
+#define ARY_UNSET_EMBED_FLAG(a) ((a)->flags &= ~(MRB_ARY_EMBED_MASK))
+#define ARY_EMBED_LEN(a) (((a)->flags & MRB_ARY_EMBED_MASK) - 1)
+#define ARY_SET_EMBED_LEN(a,len) ((a)->flags = ((a)->flags&~MRB_ARY_EMBED_MASK) | (len + 1))
#define ARY_EMBED_PTR(a) (&((a)->as.embed[0]))
#define ARY_LEN(a) (ARY_EMBED_P(a)?ARY_EMBED_LEN(a):(a)->as.heap.len)

MODIFY
src/array.c
src/array.c
@@ -29,8 +29,7 @@ ary_new_capa(mrb_state *mrb, mrb_int capa)
a = (struct RArray*)mrb_obj_alloc(mrb, MRB_TT_ARRAY, mrb->array_class);
if (capa <= MRB_ARY_EMBED_LEN_MAX) {
- ARY_SET_EMBED_FLAG(a);
- /* ARY_SET_EMBED_LEN(a, 0); */
+ ARY_SET_EMBED_LEN(a, 0);
}
else {
a->as.heap.ptr = (mrb_value *)mrb_malloc(mrb, blen);
@@ -1048,7 +1047,6 @@ mrb_ary_clear(mrb_state *mrb, mrb_value self)
else if (!ARY_EMBED_P(a)){
mrb_free(mrb, a->as.heap.ptr);
}
- ARY_SET_EMBED_FLAG(a);
ARY_SET_EMBED_LEN(a, 0);
return self;

Improve Array structure