[前][次][番号順一覧][スレッド一覧]

ruby-changes:39001

From: nobu <ko1@a...>
Date: Wed, 1 Jul 2015 09:30:44 +0900 (JST)
Subject: [ruby-changes:39001] nobu:r51082 (trunk): struct.c: hide internal objects

nobu	2015-07-01 09:30:38 +0900 (Wed, 01 Jul 2015)

  New Revision: 51082

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=51082

  Log:
    struct.c: hide internal objects
    
    * struct.c (struct_set_members): hide internal back_members
      object, and members object does not need to be duped as it
      should be frozen and hidden.

  Modified files:
    trunk/ChangeLog
    trunk/struct.c
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 51081)
+++ ChangeLog	(revision 51082)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Jul  1 09:30:36 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* struct.c (struct_set_members): hide internal back_members
+	  object, and members object does not need to be duped as it
+	  should be frozen and hidden.
+
 Wed Jul  1 09:28:47 2015  Nobuyoshi Nakada  <nobu@r...>
 
 	* struct.c (struct_member_pos): revert r51080 to fix other
Index: struct.c
===================================================================
--- struct.c	(revision 51081)
+++ struct.c	(revision 51082)
@@ -85,26 +85,25 @@ struct_member_pos_probe(long prev, long https://github.com/ruby/ruby/blob/trunk/struct.c#L85
 }
 
 static VALUE
-struct_set_members(VALUE klass, VALUE members)
+struct_set_members(VALUE klass, VALUE /* frozen hidden array */ members)
 {
     VALUE back;
+    const long members_length = RARRAY_LEN(members);
 
-    members = rb_ary_dup(members);
-    rb_obj_freeze(members);
-
-    if (RARRAY_LEN(members) <= AREF_HASH_THRESHOLD) {
+    if (members_length <= AREF_HASH_THRESHOLD) {
 	back = members;
-    } else {
+    }
+    else {
 	long i, j, mask = 64;
 	VALUE name;
 
-	while (mask < RARRAY_LEN(members) * 5) mask *= 2;
+	while (mask < members_length * 5) mask *= 2;
 
-	back = rb_ary_new2(mask + 1);
-	rb_ary_store(back, mask, INT2FIX(RARRAY_LEN(members)));
+	back = rb_ary_tmp_new(mask + 1);
+	rb_ary_store(back, mask, INT2FIX(members_length));
 	mask -= 2;			  /* mask = (2**k-1)*2 */
 
-	for (i=0; i<RARRAY_LEN(members); i++) {
+	for (i=0; i < members_length; i++) {
 	    name = RARRAY_AREF(members, i);
 
 	    j = struct_member_pos_ideal(name, mask);
@@ -118,8 +117,8 @@ struct_set_members(VALUE klass, VALUE me https://github.com/ruby/ruby/blob/trunk/struct.c#L117
 		j = struct_member_pos_probe(j, mask);
 	    }
 	}
+	OBJ_FREEZE_RAW(back);
     }
-    rb_obj_freeze(back);
     rb_ivar_set(klass, id_members, members);
     rb_ivar_set(klass, id_back_members, back);
 
@@ -144,7 +143,7 @@ struct_member_pos(VALUE s, VALUE name) https://github.com/ruby/ruby/blob/trunk/struct.c#L143
     mask = RARRAY_LEN(back);
 
     if (mask <= AREF_HASH_THRESHOLD) {
-	if (UNLIKELY(RSTRUCT_LEN(s) != RARRAY_LEN(back))) {
+	if (UNLIKELY(RSTRUCT_LEN(s) != mask)) {
 	    rb_raise(rb_eTypeError,
 		     "struct size differs (%ld required %ld given)",
 		     mask, RSTRUCT_LEN(s));
@@ -377,7 +376,7 @@ rb_struct_define_without_accessor_under( https://github.com/ruby/ruby/blob/trunk/struct.c#L376
         rb_ary_push(members, ID2SYM(rb_intern(name)));
     }
     va_end(ar);
-    OBJ_FREEZE(members);
+    OBJ_FREEZE_RAW(members);
 
     return struct_define_without_accessor(outer, class_name, super, alloc, members);
 }
@@ -395,7 +394,7 @@ rb_struct_define_without_accessor(const https://github.com/ruby/ruby/blob/trunk/struct.c#L394
         rb_ary_push(members, ID2SYM(rb_intern(name)));
     }
     va_end(ar);
-    OBJ_FREEZE(members);
+    OBJ_FREEZE_RAW(members);
 
     return struct_define_without_accessor(0, class_name, super, alloc, members);
 }
@@ -415,6 +414,7 @@ rb_struct_define(const char *name, ...) https://github.com/ruby/ruby/blob/trunk/struct.c#L414
 	rb_ary_push(ary, ID2SYM(slot));
     }
     va_end(ar);
+    OBJ_FREEZE_RAW(ary);
 
     if (!name) st = anonymous_struct(rb_cStruct);
     else st = new_struct(rb_str_new2(name), rb_cStruct);
@@ -436,6 +436,7 @@ rb_struct_define_under(VALUE outer, cons https://github.com/ruby/ruby/blob/trunk/struct.c#L436
 	rb_ary_push(ary, ID2SYM(slot));
     }
     va_end(ar);
+    OBJ_FREEZE_RAW(ary);
 
     return setup_struct(rb_define_class_under(outer, name, rb_cStruct), ary);
 }
@@ -512,6 +513,7 @@ rb_struct_s_def(int argc, VALUE *argv, V https://github.com/ruby/ruby/blob/trunk/struct.c#L513
 	RARRAY_ASET(rest, i, ID2SYM(id));
 	rb_ary_set_len(rest, i+1);
     }
+    OBJ_FREEZE_RAW(rest);
     if (NIL_P(name)) {
 	st = anonymous_struct(klass);
     }

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]