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

ruby-changes:11931

From: matz <ko1@a...>
Date: Thu, 28 May 2009 00:56:28 +0900 (JST)
Subject: [ruby-changes:11931] Ruby:r23594 (trunk): * st.c (st_insert2): new function with processing new key,

matz	2009-05-28 00:56:14 +0900 (Thu, 28 May 2009)

  New Revision: 23594

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

  Log:
    * st.c (st_insert2): new function with processing new key,
      e.g. copy.
    * hash.c (rb_hash_aset): use st_insert2() to reduce redundant
      st_lookup calls.

  Modified files:
    trunk/ChangeLog
    trunk/hash.c
    trunk/include/ruby/st.h
    trunk/st.c

Index: include/ruby/st.h
===================================================================
--- include/ruby/st.h	(revision 23593)
+++ include/ruby/st.h	(revision 23594)
@@ -93,6 +93,7 @@
 int st_delete(st_table *, st_data_t *, st_data_t *); /* returns 0:notfound 1:deleted */
 int st_delete_safe(st_table *, st_data_t *, st_data_t *, st_data_t);
 int st_insert(st_table *, st_data_t, st_data_t);
+int st_insert2(st_table *, st_data_t, st_data_t, st_data_t (*)(st_data_t));
 int st_lookup(st_table *, st_data_t, st_data_t *);
 int st_get_key(st_table *, st_data_t, st_data_t *);
 int st_foreach(st_table *, int (*)(ANYARGS), st_data_t);
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 23593)
+++ ChangeLog	(revision 23594)
@@ -2,6 +2,14 @@
 
 	* parse.y (struct parser_params): lex_gets_ptr should be long.
 
+Wed May 27 14:08:39 2009  Yukihiro Matsumoto  <matz@r...>
+
+	* st.c (st_insert2): new function with processing new key,
+	  e.g. copy.
+
+	* hash.c (rb_hash_aset): use st_insert2() to reduce redundant
+	  st_lookup calls.
+
 Wed May 27 02:31:38 2009  NARUSE, Yui  <naruse@r...>
 
 	* ext/readline/readline.c (readline_getc): the function for
Index: st.c
===================================================================
--- st.c	(revision 23593)
+++ st.c	(revision 23594)
@@ -416,6 +416,46 @@
     }
 }
 
+int
+st_insert2(register st_table *table, register st_data_t key, st_data_t value,
+	   st_data_t (*func)(st_data_t))
+{
+    unsigned int hash_val, bin_pos;
+    register st_table_entry *ptr;
+
+    if (table->entries_packed) {
+        st_index_t i;
+        for (i = 0; i < table->num_entries; i++) {
+            if ((st_data_t)table->bins[i*2] == key) {
+                table->bins[i*2+1] = (struct st_table_entry*)value;
+                return 1;
+            }
+        }
+        if ((table->num_entries+1) * 2 <= table->num_bins && table->num_entries+1 <= MAX_PACKED_NUMHASH) {
+            i = table->num_entries++;
+            table->bins[i*2] = (struct st_table_entry*)key;
+            table->bins[i*2+1] = (struct st_table_entry*)value;
+            return 0;
+        }
+        else {
+            unpack_entries(table);
+        }
+    }
+
+    hash_val = do_hash(key, table);
+    FIND_ENTRY(table, ptr, hash_val, bin_pos);
+
+    if (ptr == 0) {
+	key = (*func)(key);
+	ADD_DIRECT(table, key, value, hash_val, bin_pos);
+	return 0;
+    }
+    else {
+	ptr->record = value;
+	return 1;
+    }
+}
+
 void
 st_add_direct(st_table *table, st_data_t key, st_data_t value)
 {
Index: hash.c
===================================================================
--- hash.c	(revision 23593)
+++ hash.c	(revision 23594)
@@ -1015,12 +1015,11 @@
 rb_hash_aset(VALUE hash, VALUE key, VALUE val)
 {
     rb_hash_modify(hash);
-    if (RHASH(hash)->ntbl->type == &identhash ||
-	rb_obj_class(key) != rb_cString || st_lookup(RHASH(hash)->ntbl, key, 0)) {
+    if (RHASH(hash)->ntbl->type == &identhash || rb_obj_class(key) != rb_cString) {
 	st_insert(RHASH(hash)->ntbl, key, val);
     }
     else {
-	st_add_direct(RHASH(hash)->ntbl, rb_str_new4(key), val);
+	st_insert2(RHASH(hash)->ntbl, key, val, rb_str_new4);
     }
     return val;
 }

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

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