aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog9
-rw-r--r--hash.c22
-rw-r--r--include/ruby/st.h2
-rw-r--r--st.c41
4 files changed, 70 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index f9b805f180..7809652cdc 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+Thu Nov 28 17:34:42 2013 Masaki Matsushita <glass.saga@gmail.com>
+
+ * 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@gmail.com>
* st.c (st_keys): fix not to use Qundef in st.c.
diff --git a/hash.c b/hash.c
index b03c13dc13..e347c79192 100644
--- a/hash.c
+++ b/hash.c
@@ -1746,12 +1746,26 @@ values_i(VALUE key, VALUE value, VALUE ary)
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;
}
/*
diff --git a/include/ruby/st.h b/include/ruby/st.h
index 839eb1ef0b..975da655bf 100644
--- a/include/ruby/st.h
+++ b/include/ruby/st.h
@@ -121,6 +121,8 @@ int st_foreach_check(st_table *, int (*)(ANYARGS), st_data_t, st_data_t);
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);
diff --git a/st.c b/st.c
index e04b2a1d19..163135912b 100644
--- a/st.c
+++ b/st.c
@@ -1132,6 +1132,47 @@ st_keys_check(st_table *table, st_data_t *keys, st_index_t size, st_data_t never
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)