Fix `memsize_of` to count method table size;
Also avoid `mrb_funcall` to minimize VM recursion.
@@ -209,23 +209,6 @@ os_memsize_of_method(mrb_state* mrb, mrb_value method_obj)
return size;
}
-static mrb_int
-os_memsize_of_methods(mrb_state* mrb, mrb_value obj)
-{
- mrb_value method_list;
- mrb_int size = 0;
- mrb_int i;
-
- if(!mrb_respond_to(mrb, obj, mrb_intern_lit(mrb, "instance_methods"))) return 0;
- method_list = mrb_funcall(mrb, obj, "instance_methods", 1, mrb_false_value());
- for(i = 0; i < RARRAY_LEN(method_list); i++) {
- mrb_value method = mrb_funcall(mrb, obj, "instance_method", 1,
- mrb_ary_ref(mrb, method_list, i));
- size += os_memsize_of_method(mrb, method);
- }
- return size;
-}
-
static mrb_int
os_memsize_of_object(mrb_state* mrb, mrb_value obj)
{
@@ -240,18 +223,18 @@ os_memsize_of_object(mrb_state* mrb, mrb_value obj)
break;
case MRB_TT_CLASS:
case MRB_TT_MODULE:
- case MRB_TT_EXCEPTION:
case MRB_TT_SCLASS:
case MRB_TT_ICLASS:
+ size += mrb_gc_mark_mt_size(mrb, mrb_class_ptr(obj)) * sizeof(mrb_method_t);
+ /* fall through */
+ case MRB_TT_EXCEPTION:
case MRB_TT_OBJECT: {
size += mrb_objspace_page_slot_size();
size += os_memsize_of_ivars(mrb, obj);
- if(mrb_obj_is_kind_of(mrb, obj, mrb_class_get(mrb, "UnboundMethod"))) {
+ if (mrb_obj_is_kind_of(mrb, obj, mrb_class_get(mrb, "UnboundMethod")) ||
+ mrb_obj_is_kind_of(mrb, obj, mrb_class_get(mrb, "Method"))){
size += os_memsize_of_method(mrb, obj);
}
- else {
- size += os_memsize_of_methods(mrb, obj);
- }
break;
}
case MRB_TT_HASH: {
