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

ruby-changes:31816

From: glass <ko1@a...>
Date: Thu, 28 Nov 2013 17:39:25 +0900 (JST)
Subject: [ruby-changes:31816] glass:r43895 (trunk): * st.c: add st_values() and st_values_check().

glass	2013-11-28 17:39:16 +0900 (Thu, 28 Nov 2013)

  New Revision: 43895

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

  Log:
    * st.c: add st_values() and st_values_check().
    
    * include/ruby/st.h: add prototypes for above.
    
    * hash.c (rb_hash_values): use st_values_check() for performance
     improvement if VALUE and st_data_t are compatible.

  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 43894)
+++ include/ruby/st.h	(revision 43895)
@@ -121,6 +121,8 @@ int st_foreach_check(st_table *, int (*) https://github.com/ruby/ruby/blob/trunk/include/ruby/st.h#L121
 int st_reverse_foreach(st_table *, int (*)(ANYARGS), st_data_t);
 st_index_t st_keys(st_table *table, st_data_t *keys, st_index_t size);
 st_index_t st_keys_check(st_table *table, st_data_t *keys, st_index_t size, st_data_t never);
+st_index_t st_values(st_table *table, st_data_t *values, st_index_t size);
+st_index_t st_values_check(st_table *table, st_data_t *values, st_index_t size, st_data_t never);
 void st_add_direct(st_table *, st_data_t, st_data_t);
 void st_free_table(st_table *);
 void st_cleanup_safe(st_table *, st_data_t);
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 43894)
+++ ChangeLog	(revision 43895)
@@ -1,3 +1,12 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Thu Nov 28 17:34:42 2013  Masaki Matsushita  <glass.saga@g...>
+
+	* st.c: add st_values() and st_values_check().
+
+	* include/ruby/st.h: add prototypes for above.
+
+	* hash.c (rb_hash_values): use st_values_check() for performance
+	 improvement if VALUE and st_data_t are compatible.
+
 Thu Nov 28 17:14:14 2013  Masaki Matsushita  <glass.saga@g...>
 
 	* st.c (st_keys): fix not to use Qundef in st.c.
Index: st.c
===================================================================
--- st.c	(revision 43894)
+++ st.c	(revision 43895)
@@ -1132,6 +1132,47 @@ st_keys_check(st_table *table, st_data_t https://github.com/ruby/ruby/blob/trunk/st.c#L1132
     return get_keys(table, keys, size, 1, never);
 }
 
+static st_index_t
+get_values(st_table *table, st_data_t *values, st_index_t size, int check, st_data_t never)
+{
+    st_data_t key;
+    st_data_t *values_start = values;
+
+    if (table->entries_packed) {
+	st_index_t i;
+
+	if (size > table->real_entries) size = table->real_entries;
+	for (i = 0; i < size; i++) {
+	    key = PKEY(table, i);
+	    if (check && key == never) continue;
+	    *values++ = PVAL(table, i);
+	}
+    }
+    else {
+	st_table_entry *ptr = table->head;
+	st_data_t *values_end = values + size;
+	for (; ptr && values < values_end; ptr = ptr->fore) {
+	    key = ptr->key;
+	    if (check && key == never) continue;
+	    *values++ = ptr->record;
+	}
+    }
+
+    return values - values_start;
+}
+
+st_index_t
+st_values(st_table *table, st_data_t *values, st_index_t size)
+{
+    return get_values(table, values, size, 0, 0);
+}
+
+st_index_t
+st_values_check(st_table *table, st_data_t *values, st_index_t size, st_data_t never)
+{
+    return get_values(table, values, size, 1, never);
+}
+
 #if 0  /* unused right now */
 int
 st_reverse_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
Index: hash.c
===================================================================
--- hash.c	(revision 43894)
+++ hash.c	(revision 43895)
@@ -1746,12 +1746,26 @@ values_i(VALUE key, VALUE value, VALUE a https://github.com/ruby/ruby/blob/trunk/hash.c#L1746
 VALUE
 rb_hash_values(VALUE hash)
 {
-    VALUE ary;
+    VALUE values;
+    st_index_t size = RHASH_SIZE(hash);
 
-    ary = rb_ary_new_capa(RHASH_SIZE(hash));
-    rb_hash_foreach(hash, values_i, ary);
+    values = rb_ary_new_capa(size);
+    if (size == 0) return values;
 
-    return ary;
+    if (ST_DATA_COMPATIBLE_P(VALUE)) {
+	st_table *table = RHASH(hash)->ntbl;
+
+	if (OBJ_PROMOTED(values)) rb_gc_writebarrier_remember_promoted(values);
+	RARRAY_PTR_USE(values, ptr, {
+	    size = st_values_check(table, ptr, size, Qundef);
+	});
+	rb_ary_set_len(values, size);
+    }
+    else {
+	rb_hash_foreach(hash, values_i, values);
+    }
+
+    return values;
 }
 
 /*

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

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