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

ruby-changes:3968

From: ko1@a...
Date: Wed, 13 Feb 2008 20:53:07 +0900 (JST)
Subject: [ruby-changes:3968] akr - Ruby:r15458 (trunk): * include/ruby/ruby.h (RObject): add iv_index_tbl for shortcut of

akr	2008-02-13 20:52:46 +0900 (Wed, 13 Feb 2008)

  New Revision: 15458

  Modified files:
    trunk/ChangeLog
    trunk/include/ruby/ruby.h
    trunk/object.c
    trunk/variable.c

  Log:
    * include/ruby/ruby.h (RObject): add iv_index_tbl for shortcut of
      RCLASS_IV_INDEX_TBL(rb_obj_class(obj)).
      (ROBJECT_IV_INDEX_TBL): defined.
    
    * object.c (init_copy): initialize iv_index_tbl in struct RObject.
    
    * variable.c (ivar_get): use ROBJECT_IV_INDEX_TBL.
      (rb_ivar_defined): ditto.
      (obj_ivar_each): ditto.
      (rb_obj_remove_instance_variable): ditto.
      (rb_ivar_set): update iv_index_tbl in struct RObject.


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/include/ruby/ruby.h?r1=15458&r2=15457&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/variable.c?r1=15458&r2=15457&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=15458&r2=15457&diff_format=u
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/object.c?r1=15458&r2=15457&diff_format=u

Index: include/ruby/ruby.h
===================================================================
--- include/ruby/ruby.h	(revision 15457)
+++ include/ruby/ruby.h	(revision 15458)
@@ -398,6 +398,7 @@
 	struct {
 	    long len;
 	    VALUE *ptr;
+            struct st_table *iv_index_tbl; /* shortcut for RCLASS_IV_INDEX_TBL(rb_obj_class(obj)) */
 	} heap;
 	VALUE ary[ROBJECT_EMBED_LEN_MAX];
     } as;
@@ -411,6 +412,10 @@
     ((RBASIC(o)->flags & ROBJECT_EMBED) ? \
      ROBJECT(o)->as.ary : \
      ROBJECT(o)->as.heap.ptr)
+#define ROBJECT_IV_INDEX_TBL(o) \
+    ((RBASIC(o)->flags & ROBJECT_EMBED) ? \
+     RCLASS_IV_INDEX_TBL(rb_obj_class(o)) : \
+     ROBJECT(o)->as.heap.iv_index_tbl)
 
 struct RValues {
     struct RBasic basic;
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 15457)
+++ ChangeLog	(revision 15458)
@@ -1,3 +1,17 @@
+Wed Feb 13 20:48:50 2008  Tanaka Akira  <akr@f...>
+
+	* include/ruby/ruby.h (RObject): add iv_index_tbl for shortcut of
+	  RCLASS_IV_INDEX_TBL(rb_obj_class(obj)).
+	  (ROBJECT_IV_INDEX_TBL): defined.
+
+	* object.c (init_copy): initialize iv_index_tbl in struct RObject.
+
+	* variable.c (ivar_get): use ROBJECT_IV_INDEX_TBL.
+	  (rb_ivar_defined): ditto.
+	  (obj_ivar_each): ditto.
+	  (rb_obj_remove_instance_variable): ditto.
+	  (rb_ivar_set): update iv_index_tbl in struct RObject.
+
 Wed Feb 13 16:21:48 2008  NARUSE, Yui  <naruse@r...>
 
 	* lib/uri/generic.rb: revert r15442. 2nd argument of String#sub parse
Index: variable.c
===================================================================
--- variable.c	(revision 15457)
+++ variable.c	(revision 15458)
@@ -932,17 +932,20 @@
 static VALUE
 ivar_get(VALUE obj, ID id, int warn)
 {
-    VALUE val;
-    VALUE klass;
+    VALUE val, *ptr;
+    struct st_table *iv_index_tbl;
+    long len;
     st_data_t index;
 
     switch (TYPE(obj)) {
       case T_OBJECT:
-        klass = rb_obj_class(obj);
-        if (!RCLASS_IV_INDEX_TBL(klass)) break; 
-        if (!st_lookup(RCLASS_IV_INDEX_TBL(klass), id, &index)) break;
-        if (ROBJECT_LEN(obj) <= index) break;
-        val = ROBJECT_PTR(obj)[index];
+        len = ROBJECT_LEN(obj);
+        ptr = ROBJECT_PTR(obj);
+        iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
+        if (!iv_index_tbl) break; 
+        if (!st_lookup(iv_index_tbl, id, &index)) break;
+        if (len <= index) break;
+        val = ptr[index];
         if (val != Qundef)
             return val;
 	break;
@@ -977,7 +980,7 @@
 VALUE
 rb_ivar_set(VALUE obj, ID id, VALUE val)
 {
-    VALUE klass;
+    struct st_table *iv_index_tbl;
     st_data_t index;
     long i, len;
     int ivar_extended;
@@ -987,13 +990,18 @@
     if (OBJ_FROZEN(obj)) rb_error_frozen("object");
     switch (TYPE(obj)) {
       case T_OBJECT:
-        klass = rb_obj_class(obj);
-        if (!RCLASS_IV_INDEX_TBL(klass))
-            RCLASS_IV_INDEX_TBL(klass) = st_init_numtable();
+        iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
+        if (!iv_index_tbl) {
+            VALUE klass = rb_obj_class(obj);
+            iv_index_tbl = RCLASS_IV_INDEX_TBL(klass);
+            if (!iv_index_tbl) {
+                iv_index_tbl = RCLASS_IV_INDEX_TBL(klass) = st_init_numtable();
+            }
+        }
         ivar_extended = 0;
-        if (!st_lookup(RCLASS_IV_INDEX_TBL(klass), id, &index)) {
-            index = RCLASS_IV_INDEX_TBL(klass)->num_entries;
-            st_add_direct(RCLASS_IV_INDEX_TBL(klass), id, index);
+        if (!st_lookup(iv_index_tbl, id, &index)) {
+            index = iv_index_tbl->num_entries;
+            st_add_direct(iv_index_tbl, id, index);
             ivar_extended = 1;
         }
         len = ROBJECT_LEN(obj);
@@ -1010,8 +1018,8 @@
                 VALUE *newptr;
                 long newsize = (index+1) + (index+1)/4; /* (index+1)*1.25 */
                 if (!ivar_extended &&
-                    RCLASS_IV_INDEX_TBL(klass)->num_entries < newsize) {
-                    newsize = RCLASS_IV_INDEX_TBL(klass)->num_entries;
+                    iv_index_tbl->num_entries < newsize) {
+                    newsize = iv_index_tbl->num_entries;
                 }
                 if (RBASIC(obj)->flags & ROBJECT_EMBED) {
                     newptr = ALLOC_N(VALUE, newsize);
@@ -1026,6 +1034,7 @@
                 for (; len < newsize; len++)
                     newptr[len] = Qundef;
                 ROBJECT(obj)->as.heap.len = newsize;
+                ROBJECT(obj)->as.heap.iv_index_tbl = iv_index_tbl;
             }
         }
         ROBJECT_PTR(obj)[index] = val;
@@ -1045,13 +1054,14 @@
 VALUE
 rb_ivar_defined(VALUE obj, ID id)
 {
-    VALUE klass, val;
+    VALUE val;
+    struct st_table *iv_index_tbl;
     st_data_t index;
     switch (TYPE(obj)) {
       case T_OBJECT:
-        klass = rb_obj_class(obj);
-        if (!RCLASS_IV_INDEX_TBL(klass)) break; 
-        if (!st_lookup(RCLASS_IV_INDEX_TBL(klass), id, &index)) break;
+        iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
+        if (!iv_index_tbl) break;
+        if (!st_lookup(iv_index_tbl, id, &index)) break;
         if (ROBJECT_LEN(obj) <= index) break;
         val = ROBJECT_PTR(obj)[index];
         if (val != Qundef)
@@ -1091,11 +1101,10 @@
 static void
 obj_ivar_each(VALUE obj, int (*func)(ANYARGS), st_data_t arg)
 {
-    VALUE klass = rb_obj_class(obj);
     st_table *tbl;
     struct obj_ivar_tag data;
 
-    tbl = RCLASS_IV_INDEX_TBL(klass);
+    tbl = ROBJECT_IV_INDEX_TBL(obj);
     if (!tbl)
         return;
 
@@ -1194,7 +1203,7 @@
 {
     VALUE val = Qnil;
     ID id = rb_to_id(name);
-    VALUE klass;
+    struct st_table *iv_index_tbl;
     st_data_t index;
 
     if (!OBJ_TAINTED(obj) && rb_safe_level() >= 4)
@@ -1206,9 +1215,9 @@
 
     switch (TYPE(obj)) {
       case T_OBJECT:
-        klass = rb_obj_class(obj);
-        if (!RCLASS_IV_INDEX_TBL(klass)) break; 
-        if (!st_lookup(RCLASS_IV_INDEX_TBL(klass), id, &index)) break;
+        iv_index_tbl = ROBJECT_IV_INDEX_TBL(obj);
+        if (!iv_index_tbl) break;
+        if (!st_lookup(iv_index_tbl, id, &index)) break;
         if (ROBJECT_LEN(obj) <= index) break;
         val = ROBJECT_PTR(obj)[index];
         if (val != Qundef) {
Index: object.c
===================================================================
--- object.c	(revision 15457)
+++ object.c	(revision 15458)
@@ -170,6 +170,7 @@
             xfree(ROBJECT_PTR(dest));
             ROBJECT(dest)->as.heap.ptr = 0;
             ROBJECT(dest)->as.heap.len = 0;
+            ROBJECT(dest)->as.heap.iv_index_tbl = 0;
         }
         if (RBASIC(obj)->flags & ROBJECT_EMBED) {
             MEMCPY(ROBJECT(dest)->as.ary, ROBJECT(obj)->as.ary, VALUE, ROBJECT_EMBED_LEN_MAX);
@@ -181,6 +182,7 @@
             MEMCPY(ptr, ROBJECT(obj)->as.heap.ptr, VALUE, len);
             ROBJECT(dest)->as.heap.ptr = ptr;
             ROBJECT(dest)->as.heap.len = len;
+            ROBJECT(dest)->as.heap.iv_index_tbl = ROBJECT(obj)->as.heap.iv_index_tbl;
             RBASIC(dest)->flags &= ~ROBJECT_EMBED;
         }
         break;

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

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