From d87e2d811a1be0fa04e9c47a2198a48ca9cb333b Mon Sep 17 00:00:00 2001 From: matz Date: Mon, 10 Nov 2008 00:51:14 +0000 Subject: * struct.c (rb_struct_initialize_m): avoid unnecessary array allocation. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20164 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- struct.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'struct.c') diff --git a/struct.c b/struct.c index add6bfa03d..0cc836123f 100644 --- a/struct.c +++ b/struct.c @@ -352,25 +352,30 @@ num_members(VALUE klass) /* */ -VALUE -rb_struct_initialize(VALUE self, VALUE values) +static VALUE +rb_struct_initialize_m(int argc, VALUE *argv, VALUE self) { VALUE klass = rb_obj_class(self); long n; rb_struct_modify(self); n = num_members(klass); - if (n < RARRAY_LEN(values)) { + if (n < argc) { rb_raise(rb_eArgError, "struct size differs"); } - MEMCPY(RSTRUCT_PTR(self), RARRAY_PTR(values), VALUE, RARRAY_LEN(values)); - if (n > RARRAY_LEN(values)) { - rb_mem_clear(RSTRUCT_PTR(self)+RARRAY_LEN(values), - n-RARRAY_LEN(values)); + MEMCPY(RSTRUCT_PTR(self), argv, VALUE, argc); + if (n > argc) { + rb_mem_clear(RSTRUCT_PTR(self)+argc, n-argc); } return Qnil; } +VALUE +rb_struct_initialize(VALUE self, VALUE values) +{ + return rb_struct_initialize_m(RARRAY_LEN(values), RARRAY_PTR(values), self); +} + static VALUE struct_alloc(VALUE klass) { @@ -879,7 +884,7 @@ Init_Struct(void) rb_undef_alloc_func(rb_cStruct); rb_define_singleton_method(rb_cStruct, "new", rb_struct_s_def, -1); - rb_define_method(rb_cStruct, "initialize", rb_struct_initialize, -2); + rb_define_method(rb_cStruct, "initialize", rb_struct_initialize_m, -1); rb_define_method(rb_cStruct, "initialize_copy", rb_struct_init_copy, 1); rb_define_method(rb_cStruct, "==", rb_struct_equal, 1); -- cgit v1.2.3