From 16907944fc99a268a3fffc7ecf26a2c5dcf6fe59 Mon Sep 17 00:00:00 2001 From: Sokolov Yura aka funny_falcon Date: Wed, 10 Dec 2014 15:53:36 +0300 Subject: [PATCH 1/4] struct.c: speedup struct.name = v for small structs --- struct.c | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/struct.c b/struct.c index 080c0f3..e710505 100644 --- a/struct.c +++ b/struct.c @@ -144,6 +144,43 @@ rb_struct_modify(VALUE s) } static VALUE +rb_struct_set(VALUE obj, long i, VALUE val) +{ + if (i >= RSTRUCT_LEN(obj)) { + rb_raise(rb_eTypeError, "corrupted struct"); + } + rb_struct_modify(obj); + RSTRUCT_SET(obj, i, val); + return val; +} + +static VALUE rb_struct_set0(VALUE obj, VALUE val) {return rb_struct_set(obj, 0, val);} +static VALUE rb_struct_set1(VALUE obj, VALUE val) {return rb_struct_set(obj, 1, val);} +static VALUE rb_struct_set2(VALUE obj, VALUE val) {return rb_struct_set(obj, 2, val);} +static VALUE rb_struct_set3(VALUE obj, VALUE val) {return rb_struct_set(obj, 3, val);} +static VALUE rb_struct_set4(VALUE obj, VALUE val) {return rb_struct_set(obj, 4, val);} +static VALUE rb_struct_set5(VALUE obj, VALUE val) {return rb_struct_set(obj, 5, val);} +static VALUE rb_struct_set6(VALUE obj, VALUE val) {return rb_struct_set(obj, 6, val);} +static VALUE rb_struct_set7(VALUE obj, VALUE val) {return rb_struct_set(obj, 7, val);} +static VALUE rb_struct_set8(VALUE obj, VALUE val) {return rb_struct_set(obj, 8, val);} +static VALUE rb_struct_set9(VALUE obj, VALUE val) {return rb_struct_set(obj, 9, val);} + +#define N_SET_FUNC numberof(ref_func) + +static VALUE (*const set_func[])(VALUE, VALUE) = { + rb_struct_set0, + rb_struct_set1, + rb_struct_set2, + rb_struct_set3, + rb_struct_set4, + rb_struct_set5, + rb_struct_set6, + rb_struct_set7, + rb_struct_set8, + rb_struct_set9, +}; + +static VALUE anonymous_struct(VALUE klass) { VALUE nstr; @@ -217,7 +254,12 @@ setup_struct(VALUE nstr, VALUE members) else { define_aref_method(nstr, ptr_members[i], off); } - define_aset_method(nstr, ID2SYM(rb_id_attrset(id)), off); + if (i < N_SET_FUNC) { + rb_define_method_id(nstr, rb_id_attrset(id), set_func[i], 1); + } + else { + define_aset_method(nstr, ID2SYM(rb_id_attrset(id)), off); + } } return nstr; -- 2.1.0