ruby-changes:35674
From: normal <ko1@a...>
Date: Thu, 2 Oct 2014 05:30:01 +0900 (JST)
Subject: [ruby-changes:35674] normal:r47756 (trunk): marshal.c: lazy compat_tbl allocation
normal 2014-10-02 05:29:46 +0900 (Thu, 02 Oct 2014) New Revision: 47756 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=47756 Log: marshal.c: lazy compat_tbl allocation In some common cases, compat_tbl is unused in dump_arg/load_arg, so avoid malloc/free costs for the unused table. ruby -e 'h = {a: :b}; 600000.times { Marshal.load(Marshal.dump(h)) }' before: real 0m2.458s user 0m2.450s sys 0m0.006s after: real 0m2.122s user 0m2.110s sys 0m0.011s * marshal.c (w_class): check dump_arg->compat_tbl before lookup (w_object): lazy init ->compat_tbl before insert (obj_alloc_by_class): ditto (clear_dump_arg): free only non-NULL ->compat_tbl (clear_load_arg): ditto for ->compat_tbl (marshal_dump): ->compat_tbl defaults to zero (marshal_load): ditto for ->compat_tbl (r_entry0): check l->compat_tbl before lookup (r_fixup_compat): ditto Modified files: trunk/ChangeLog trunk/marshal.c Index: ChangeLog =================================================================== --- ChangeLog (revision 47755) +++ ChangeLog (revision 47756) @@ -1,3 +1,16 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Thu Oct 2 05:27:24 2014 Eric Wong <e@8...> + + * marshal.c (w_class): check dump_arg->compat_tbl before lookup + (w_object): lazy init ->compat_tbl before insert + (obj_alloc_by_class): ditto + (clear_dump_arg): free only non-NULL ->compat_tbl + (clear_load_arg): ditto for ->compat_tbl + (marshal_dump): ->compat_tbl defaults to zero + (marshal_load): ditto for ->compat_tbl + (r_entry0): check l->compat_tbl before lookup + (r_fixup_compat): ditto + [ruby-core:65305] [Feature #10302] + Wed Oct 1 21:14:34 2014 Masaki Suketa <masaki.suketa@n...> * ext/win32ole/win32ole_type.c: use typed data. Index: marshal.c =================================================================== --- marshal.c (revision 47755) +++ marshal.c (revision 47756) @@ -495,7 +495,8 @@ w_class(char type, VALUE obj, struct dum https://github.com/ruby/ruby/blob/trunk/marshal.c#L495 st_data_t real_obj; VALUE klass; - if (st_lookup(arg->compat_tbl, (st_data_t)obj, &real_obj)) { + if (arg->compat_tbl && + st_lookup(arg->compat_tbl, (st_data_t)obj, &real_obj)) { obj = (VALUE)real_obj; } klass = CLASS_OF(obj); @@ -738,6 +739,9 @@ w_object(VALUE obj, struct dump_arg *arg https://github.com/ruby/ruby/blob/trunk/marshal.c#L739 marshal_compat_t *compat = (marshal_compat_t*)compat_data; VALUE real_obj = obj; obj = compat->dumper(real_obj); + if (!arg->compat_tbl) { + arg->compat_tbl = st_init_numtable(); + } st_insert(arg->compat_tbl, (st_data_t)obj, (st_data_t)real_obj); if (obj != real_obj && !ivtbl) hasiv = 0; } @@ -911,8 +915,10 @@ clear_dump_arg(struct dump_arg *arg) https://github.com/ruby/ruby/blob/trunk/marshal.c#L915 arg->symbols = 0; st_free_table(arg->data); arg->data = 0; - st_free_table(arg->compat_tbl); - arg->compat_tbl = 0; + if (arg->compat_tbl) { + st_free_table(arg->compat_tbl); + arg->compat_tbl = 0; + } if (arg->encodings) { st_free_table(arg->encodings); arg->encodings = 0; @@ -985,7 +991,7 @@ marshal_dump(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/marshal.c#L991 arg->symbols = st_init_numtable(); arg->data = st_init_numtable(); arg->infection = 0; - arg->compat_tbl = st_init_numtable(); + arg->compat_tbl = 0; arg->encodings = 0; arg->str = rb_str_buf_new(0); if (!NIL_P(port)) { @@ -1365,7 +1371,7 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/marshal.c#L1371 r_entry0(VALUE v, st_index_t num, struct load_arg *arg) { st_data_t real_obj = (VALUE)Qundef; - if (st_lookup(arg->compat_tbl, v, &real_obj)) { + if (arg->compat_tbl && st_lookup(arg->compat_tbl, v, &real_obj)) { st_insert(arg->data, num, (st_data_t)real_obj); } else { @@ -1384,7 +1390,7 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/marshal.c#L1390 r_fixup_compat(VALUE v, struct load_arg *arg) { st_data_t data; - if (st_lookup(arg->compat_tbl, v, &data)) { + if (arg->compat_tbl && st_lookup(arg->compat_tbl, v, &data)) { VALUE real_obj = (VALUE)data; rb_alloc_func_t allocator = rb_get_alloc_func(CLASS_OF(real_obj)); st_data_t key = v; @@ -1490,6 +1496,10 @@ obj_alloc_by_klass(VALUE klass, struct l https://github.com/ruby/ruby/blob/trunk/marshal.c#L1496 VALUE real_obj = rb_obj_alloc(klass); VALUE obj = rb_obj_alloc(compat->oldclass); if (oldclass) *oldclass = compat->oldclass; + + if (!arg->compat_tbl) { + arg->compat_tbl = st_init_numtable(); + } st_insert(arg->compat_tbl, (st_data_t)obj, (st_data_t)real_obj); return obj; } @@ -1950,8 +1960,10 @@ clear_load_arg(struct load_arg *arg) https://github.com/ruby/ruby/blob/trunk/marshal.c#L1960 arg->symbols = 0; st_free_table(arg->data); arg->data = 0; - st_free_table(arg->compat_tbl); - arg->compat_tbl = 0; + if (arg->compat_tbl) { + st_free_table(arg->compat_tbl); + arg->compat_tbl = 0; + } } /* @@ -1996,7 +2008,7 @@ marshal_load(int argc, VALUE *argv) https://github.com/ruby/ruby/blob/trunk/marshal.c#L2008 arg->offset = 0; arg->symbols = st_init_numtable(); arg->data = st_init_numtable(); - arg->compat_tbl = st_init_numtable(); + arg->compat_tbl = 0; arg->proc = 0; arg->readable = 0; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/