From 65651b34b1b5c2ef556ed44d5ebbd98838cfe8e6 Mon Sep 17 00:00:00 2001 From: normal Date: Tue, 9 Dec 2014 15:43:49 +0000 Subject: struct: avoid all O(n) behavior on access This avoids O(n) on lookups with structs over 10 members. This also avoids O(n) behavior on all assignments on Struct members. Members 0..9 still use existing C methods to read in O(1) time Benchmark results: vm2_struct_big_aref_hi* 1.305 vm2_struct_big_aref_lo* 1.157 vm2_struct_big_aset* 3.306 vm2_struct_small_aref* 1.015 vm2_struct_small_aset* 3.273 Note: I chose use loading instructions from an array instead of writing directly to linked-lists in compile.c for ease-of-maintainability. We may move the method definitions to prelude.rb-like files in the future. I have also tested this patch with the following patch to disable the C ref_func methods and ensured the test suite and rubyspec works --- a/struct.c +++ b/struct.c @@ -209,7 +209,7 @@ setup_struct(VALUE nstr, VALUE members) ID id = SYM2ID(ptr_members[i]); VALUE off = LONG2NUM(i); - if (i < N_REF_FUNC) { + if (0 && i < N_REF_FUNC) { rb_define_method_id(nstr, id, ref_func[i], 0); } else { * iseq.c (rb_method_for_self_aref, rb_method_for_self_aset): new methods to generate bytecode for struct.c [Feature #10575] * struct.c (rb_struct_ref, rb_struct_set): remove (define_aref_method, define_aset_method): new functions (setup_struct): use new functions * test/ruby/test_struct.rb: add test for struct >10 members * benchmark/bm_vm2_struct_big_aref_hi.rb: new benchmark * benchmark/bm_vm2_struct_big_aref_lo.rb: ditto * benchmark/bm_vm2_struct_big_aset.rb: ditto * benchmark/bm_vm2_struct_small_aref.rb: ditto * benchmark/bm_vm2_struct_small_aset.rb: ditto git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48748 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- struct.c | 61 +++++++++++++++++++++++++++---------------------------------- 1 file changed, 27 insertions(+), 34 deletions(-) (limited to 'struct.c') diff --git a/struct.c b/struct.c index 93c55999c0..3a2ea47434 100644 --- a/struct.c +++ b/struct.c @@ -10,6 +10,11 @@ **********************************************************************/ #include "internal.h" +#include "vm_core.h" +#include "method.h" + +rb_iseq_t *rb_method_for_self_aref(VALUE name, VALUE arg); +rb_iseq_t *rb_method_for_self_aset(VALUE name, VALUE arg); VALUE rb_cStruct; static ID id_members; @@ -105,12 +110,6 @@ rb_struct_getmember(VALUE obj, ID id) UNREACHABLE; } -static VALUE -rb_struct_ref(VALUE obj) -{ - return rb_struct_getmember(obj, rb_frame_this_func()); -} - static VALUE rb_struct_ref0(VALUE obj) {return RSTRUCT_GET(obj, 0);} static VALUE rb_struct_ref1(VALUE obj) {return RSTRUCT_GET(obj, 1);} static VALUE rb_struct_ref2(VALUE obj) {return RSTRUCT_GET(obj, 2);} @@ -144,32 +143,6 @@ rb_struct_modify(VALUE s) rb_check_trusted(s); } -static VALUE -rb_struct_set(VALUE obj, VALUE val) -{ - VALUE members, slot, fsym; - long i, len; - ID fid = rb_frame_this_func(); - ID this_func = fid; - - members = rb_struct_members(obj); - len = RARRAY_LEN(members); - rb_struct_modify(obj); - fid = rb_id_attrget(fid); - if (!fid) not_a_member(this_func); - fsym = ID2SYM(fid); - for (i=0; iself); +} + +static void +define_aset_method(VALUE nstr, VALUE name, VALUE off) +{ + rb_iseq_t *iseq = rb_method_for_self_aset(name, off); + + rb_add_method(nstr, SYM2ID(name), VM_METHOD_TYPE_ISEQ, iseq, NOEX_PUBLIC); + RB_GC_GUARD(iseq->self); +} + static VALUE setup_struct(VALUE nstr, VALUE members) { @@ -216,13 +207,15 @@ setup_struct(VALUE nstr, VALUE members) len = RARRAY_LEN(members); for (i=0; i< len; i++) { ID id = SYM2ID(ptr_members[i]); + VALUE off = LONG2NUM(i); + if (i < N_REF_FUNC) { rb_define_method_id(nstr, id, ref_func[i], 0); } else { - rb_define_method_id(nstr, id, rb_struct_ref, 0); + define_aref_method(nstr, ptr_members[i], off); } - rb_define_method_id(nstr, rb_id_attrset(id), rb_struct_set, 1); + define_aset_method(nstr, ID2SYM(rb_id_attrset(id)), off); } return nstr; -- cgit v1.2.3