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

ruby-changes:11833

From: matz <ko1@a...>
Date: Tue, 19 May 2009 13:58:57 +0900 (JST)
Subject: [ruby-changes:11833] Ruby:r23488 (trunk): * struct.c (struct_ivar_get): new function to avoid repeated

matz	2009-05-19 13:58:36 +0900 (Tue, 19 May 2009)

  New Revision: 23488

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

  Log:
    * struct.c (struct_ivar_get): new function to avoid repeated
      rb_intern() calls.
    * struct.c (rb_struct_iv_get): use struct_ivar_get()
    
    * struct.c (num_members): ditto.
    
    * struct.c (rb_struct_s_members): ditto.
    
    * class.c (rb_singleton_class): cache symbol to reduce calls to
      rb_intern().

  Modified files:
    trunk/ChangeLog
    trunk/class.c
    trunk/struct.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 23487)
+++ ChangeLog	(revision 23488)
@@ -1,3 +1,17 @@
+Tue May 19 13:54:15 2009  Yukihiro Matsumoto  <matz@r...>
+
+	* struct.c (struct_ivar_get): new function to avoid repeated
+	  rb_intern() calls.
+
+	* struct.c (rb_struct_iv_get): use struct_ivar_get()
+
+	* struct.c (num_members): ditto.
+
+	* struct.c (rb_struct_s_members): ditto.
+
+	* class.c (rb_singleton_class): cache symbol to reduce calls to
+	  rb_intern().
+
 Tue May 19 07:52:05 2009  Tanaka Akira  <akr@f...>
 
 	* test/test_time.rb: make tests timezone independent.
Index: struct.c
===================================================================
--- struct.c	(revision 23487)
+++ struct.c	(revision 23488)
@@ -12,15 +12,13 @@
 #include "ruby/ruby.h"
 
 VALUE rb_cStruct;
+static ID id_members;
 
 static VALUE struct_alloc(VALUE);
 
-VALUE
-rb_struct_iv_get(VALUE c, const char *name)
+static inline VALUE
+struct_ivar_get(VALUE c, ID id)
 {
-    ID id;
-
-    id = rb_intern(name);
     for (;;) {
 	if (rb_ivar_defined(c, id))
 	    return rb_ivar_get(c, id);
@@ -31,9 +29,15 @@
 }
 
 VALUE
+rb_struct_iv_get(VALUE c, const char *name)
+{
+    return struct_ivar_get(c, rb_intern(name));
+}
+
+VALUE
 rb_struct_s_members(VALUE klass)
 {
-    VALUE members = rb_struct_iv_get(klass, "__members__");
+    VALUE members = struct_ivar_get(klass, id_members);
 
     if (NIL_P(members)) {
 	rb_raise(rb_eTypeError, "uninitialized struct");
@@ -193,7 +197,7 @@
 	}
 	nstr = rb_define_class_under(klass, rb_id2name(id), klass);
     }
-    rb_iv_set(nstr, "__members__", members);
+    rb_ivar_set(nstr, id_members, members);
 
     rb_define_alloc_func(nstr, struct_alloc);
     rb_define_singleton_method(nstr, "new", rb_class_new_instance, -1);
@@ -248,7 +252,7 @@
 	rb_class_inherited(super, klass);
     }
 
-    rb_iv_set(klass, "__members__", members);
+    rb_ivar_set(klass, id_members, members);
 
     if (alloc)
         rb_define_alloc_func(klass, alloc);
@@ -342,7 +346,7 @@
 num_members(VALUE klass)
 {
     VALUE members;
-    members = rb_struct_iv_get(klass, "__members__");
+    members = struct_ivar_get(klass, id_members);
     if (TYPE(members) != T_ARRAY) {
 	rb_raise(rb_eTypeError, "broken members");
     }
@@ -907,4 +911,5 @@
     rb_define_method(rb_cStruct, "values_at", rb_struct_values_at, -1);
 
     rb_define_method(rb_cStruct, "members", rb_struct_members_m, 0);
+    id_members = rb_intern("__members__");
 }
Index: class.c
===================================================================
--- class.c	(revision 23487)
+++ class.c	(revision 23488)
@@ -857,6 +857,7 @@
 rb_singleton_class(VALUE obj)
 {
     VALUE klass;
+    ID attached;
 
     if (FIXNUM_P(obj) || SYMBOL_P(obj)) {
 	rb_raise(rb_eTypeError, "can't define singleton");
@@ -868,8 +869,9 @@
 	rb_bug("unknown immediate %ld", obj);
     }
 
+    CONST_ID(attached, "__attached__");
     if (FL_TEST(RBASIC(obj)->klass, FL_SINGLETON) &&
-	rb_iv_get(RBASIC(obj)->klass, "__attached__") == obj) {
+	rb_ivar_get(RBASIC(obj)->klass, attached) == obj) {
 	klass = RBASIC(obj)->klass;
     }
     else {

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

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