From 0ab543fd7524ff6ed81432eea8f6bc7849af192d Mon Sep 17 00:00:00 2001 From: Jonas Kulla Date: Sat, 9 Aug 2014 10:38:42 +0200 Subject: [PATCH] MRuby-Binding: Reduce number of object allocations This is the same change as f067e0eff8777f78a9f6ab8ad0af1309d907e6ba, applied to the mruby backend. --- binding-mruby/binding-util.cpp | 5 +-- binding-mruby/binding-util.h | 68 ++++++++---------------------- binding-mruby/bitmap-binding.cpp | 2 +- binding-mruby/etc-binding.cpp | 2 +- binding-mruby/font-binding.cpp | 2 +- binding-mruby/plane-binding.cpp | 2 +- binding-mruby/sprite-binding.cpp | 2 +- binding-mruby/table-binding.cpp | 2 +- binding-mruby/tilemap-binding.cpp | 2 +- binding-mruby/viewport-binding.cpp | 2 +- binding-mruby/window-binding.cpp | 2 +- 11 files changed, 28 insertions(+), 63 deletions(-) diff --git a/binding-mruby/binding-util.cpp b/binding-mruby/binding-util.cpp index 2466609..70e1d46 100644 --- a/binding-mruby/binding-util.cpp +++ b/binding-mruby/binding-util.cpp @@ -31,7 +31,6 @@ struct const char *str; } static const symData[] = { - SYMD(priv_iv), SYMD(font), SYMD(viewport), SYMD(bitmap), @@ -143,10 +142,8 @@ void raiseMrbExc(mrb_state *mrb, const Exception &exc) MRB_METHOD_PUB(inspectObject) { - mrb_value priv = mrb_obj_iv_get(mrb, mrb_obj_ptr(self), getSym(mrb, CSpriv_iv)); - static char buffer[64]; - snprintf(buffer, sizeof(buffer), "#<%s:%p>", DATA_TYPE(priv)->struct_name, DATA_PTR(priv)); + snprintf(buffer, sizeof(buffer), "#<%s:%p>", DATA_TYPE(self)->struct_name, DATA_PTR(self)); return mrb_str_new_cstr(mrb, buffer); } diff --git a/binding-mruby/binding-util.h b/binding-mruby/binding-util.h index 170e77b..38c5e56 100644 --- a/binding-mruby/binding-util.h +++ b/binding-mruby/binding-util.h @@ -32,11 +32,7 @@ enum CommonSymbol { - CSpriv_iv = 0, /* private data */ - - /* From here on out all symbols - * have implicit '@' prefix */ - CSfont, + CSfont = 0, CSviewport, CSbitmap, CScolor, @@ -115,7 +111,10 @@ getMrbData(mrb_state *mrb) inline RClass* defineClass(mrb_state *mrb, const char *name) { - return mrb_define_class(mrb, name, mrb->object_class); + RClass *klass = mrb_define_class(mrb, name, mrb->object_class); + MRB_SET_INSTANCE_TT(klass, MRB_TT_DATA); + + return klass; } #define GUARD_EXC(exp) \ @@ -222,7 +221,7 @@ defineClass(mrb_state *mrb, const char *name) Klass *k = getPrivateData(mrb, self); \ mrb_value dupObj = mrb_obj_clone(mrb, self); \ Klass *dupK = new Klass(*k); \ - setPrivateData(mrb, dupObj, dupK, Klass##Type); \ + setPrivateData(dupObj, dupK, Klass##Type); \ return dupObj; \ } @@ -257,62 +256,33 @@ template inline T * getPrivateData(mrb_state *mrb, mrb_value self) { - mrb_value priv = mrb_obj_iv_get(mrb, - mrb_obj_ptr(self), - getSym(mrb, CSpriv_iv)); - - return static_cast(DATA_PTR(priv)); + (void) mrb; + return static_cast(DATA_PTR(self)); } template inline T * getPrivateDataCheck(mrb_state *mrb, mrb_value obj, const mrb_data_type &type) { - static const char mesg[] = "wrong argument type %S (expected %S)"; - - if (mrb_type(obj) != MRB_TT_OBJECT) - mrb_raisef(mrb, E_TYPE_ERROR, mesg, - mrb_str_new_cstr(mrb, (mrb_class_name(mrb, mrb_class(mrb, obj)))), - mrb_str_new_cstr(mrb, type.struct_name)); - - RObject *objP = mrb_obj_ptr(obj); - - if (!mrb_obj_iv_defined(mrb, objP, getSym(mrb, CSpriv_iv))) - mrb_raisef(mrb, E_TYPE_ERROR, mesg, - mrb_str_new_cstr(mrb, (mrb_class_name(mrb, mrb_class(mrb, obj)))), - mrb_str_new_cstr(mrb, type.struct_name)); - - mrb_value priv = mrb_obj_iv_get(mrb, objP, getSym(mrb, CSpriv_iv)); - - void *p = mrb_check_datatype(mrb, priv, &type); - - return static_cast(p); + return static_cast(mrb_check_datatype(mrb, obj, &type)); } inline void -setPrivateData(mrb_state *mrb, mrb_value self, void *p, const mrb_data_type &type) +setPrivateData(mrb_value self, void *p, const mrb_data_type &type) { - RData *data = - mrb_data_object_alloc(mrb, - mrb_obj_class(mrb, self), - p, - &type); - - mrb_obj_iv_set(mrb, - mrb_obj_ptr(self), - getSym(mrb, CSpriv_iv), - mrb_obj_value(data)); + DATA_PTR(self) = p; + DATA_TYPE(self) = &type; } inline mrb_value wrapObject(mrb_state *mrb, void *p, const mrb_data_type &type) { - RClass *c = mrb_class_get(mrb, type.struct_name); - RObject *o = (RObject*) mrb_obj_alloc(mrb, MRB_TT_OBJECT, c); - mrb_value obj = mrb_obj_value(o); + RClass *klass = mrb_class_get(mrb, type.struct_name); + RData *data = mrb_data_object_alloc(mrb, klass, p, &type); + mrb_value obj = mrb_obj_value(data); - setPrivateData(mrb, obj, p, type); + setPrivateData(obj, p, type); return obj; } @@ -384,12 +354,10 @@ objectLoad(mrb_state *mrb, mrb_value self, const mrb_data_type &type) int data_len; mrb_get_args(mrb, "s", &data, &data_len); - RObject *obj = (RObject*) mrb_obj_alloc(mrb, MRB_TT_OBJECT, klass); - mrb_value obj_value = mrb_obj_value(obj); - C *c = C::deserialize(data, data_len); - setPrivateData(mrb, obj_value, c, type); + RData *obj = mrb_data_object_alloc(mrb, klass, c, &type); + mrb_value obj_value = mrb_obj_value(obj); return obj_value; } diff --git a/binding-mruby/bitmap-binding.cpp b/binding-mruby/bitmap-binding.cpp index 301ea29..cfb93e5 100644 --- a/binding-mruby/bitmap-binding.cpp +++ b/binding-mruby/bitmap-binding.cpp @@ -49,7 +49,7 @@ MRB_METHOD(bitmapInitialize) GUARD_EXC( b = new Bitmap(width, height); ) } - setPrivateData(mrb, self, b, BitmapType); + setPrivateData(self, b, BitmapType); /* Wrap properties */ Font *font = new Font(); diff --git a/binding-mruby/etc-binding.cpp b/binding-mruby/etc-binding.cpp index f65ed19..bf571e8 100644 --- a/binding-mruby/etc-binding.cpp +++ b/binding-mruby/etc-binding.cpp @@ -95,7 +95,7 @@ DEF_TYPE(Rect); param_type p1, p2, p3, p4 = last_param_def; \ mrb_get_args(mrb, param_t_s, &p1, &p2, &p3, &p4); \ Klass *k = new Klass(p1, p2, p3, p4); \ - setPrivateData(mrb, self, k, Klass##Type); \ + setPrivateData(self, k, Klass##Type); \ return self; \ } diff --git a/binding-mruby/font-binding.cpp b/binding-mruby/font-binding.cpp index c3c5afc..7ffad01 100644 --- a/binding-mruby/font-binding.cpp +++ b/binding-mruby/font-binding.cpp @@ -45,7 +45,7 @@ MRB_METHOD(fontInitialize) Font *f = new Font(name, size); - setPrivateData(mrb, self, f, FontType); + setPrivateData(self, f, FontType); /* Wrap property objects */ f->setColor(new Color(*f->getColor())); diff --git a/binding-mruby/plane-binding.cpp b/binding-mruby/plane-binding.cpp index 1edc735..0903364 100644 --- a/binding-mruby/plane-binding.cpp +++ b/binding-mruby/plane-binding.cpp @@ -31,7 +31,7 @@ MRB_METHOD(planeInitialize) { Plane *p = viewportElementInitialize(mrb, self); - setPrivateData(mrb, self, p, PlaneType); + setPrivateData(self, p, PlaneType); p->setColor(new Color); p->setTone(new Tone); diff --git a/binding-mruby/sprite-binding.cpp b/binding-mruby/sprite-binding.cpp index 0e99d4e..8542247 100644 --- a/binding-mruby/sprite-binding.cpp +++ b/binding-mruby/sprite-binding.cpp @@ -32,7 +32,7 @@ MRB_METHOD(spriteInitialize) { Sprite *s = viewportElementInitialize(mrb, self); - setPrivateData(mrb, self, s, SpriteType); + setPrivateData(self, s, SpriteType); /* Wrap property objects */ s->setSrcRect(new Rect); diff --git a/binding-mruby/table-binding.cpp b/binding-mruby/table-binding.cpp index 0ccc038..4dd4e6a 100644 --- a/binding-mruby/table-binding.cpp +++ b/binding-mruby/table-binding.cpp @@ -35,7 +35,7 @@ MRB_METHOD(tableInitialize) Table *t = new Table(x, y, z); - setPrivateData(mrb, self, t, TableType); + setPrivateData(self, t, TableType); return self; } diff --git a/binding-mruby/tilemap-binding.cpp b/binding-mruby/tilemap-binding.cpp index a117533..78f3e6d 100644 --- a/binding-mruby/tilemap-binding.cpp +++ b/binding-mruby/tilemap-binding.cpp @@ -87,7 +87,7 @@ MRB_METHOD(tilemapInitialize) /* Construct object */ t = new Tilemap(viewport); - setPrivateData(mrb, self, t, TilemapType); + setPrivateData(self, t, TilemapType); setProperty(mrb, self, CSviewport, viewportObj); diff --git a/binding-mruby/viewport-binding.cpp b/binding-mruby/viewport-binding.cpp index 2a52884..728bf6f 100644 --- a/binding-mruby/viewport-binding.cpp +++ b/binding-mruby/viewport-binding.cpp @@ -54,7 +54,7 @@ MRB_METHOD(viewportInitialize) v = new Viewport(x, y, width, height); } - setPrivateData(mrb, self, v, ViewportType); + setPrivateData(self, v, ViewportType); /* Wrap property objects */ v->setRect(new Rect(*v->getRect())); diff --git a/binding-mruby/window-binding.cpp b/binding-mruby/window-binding.cpp index 9c2410a..49a1414 100644 --- a/binding-mruby/window-binding.cpp +++ b/binding-mruby/window-binding.cpp @@ -30,7 +30,7 @@ MRB_METHOD(windowInitialize) { Window *w = viewportElementInitialize(mrb, self); - setPrivateData(mrb, self, w, WindowType); + setPrivateData(self, w, WindowType); w->setCursorRect(new Rect); wrapNilProperty(mrb, self, CSwindowskin);