diff options
author | glass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-11-28 08:39:16 +0000 |
---|---|---|
committer | glass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2013-11-28 08:39:16 +0000 |
commit | 79e63364953573d642537c0fe4eeeb03d85c15a7 (patch) | |
tree | 41a3a7ab413b31642d5f49cdb9cbcaafa6229527 | |
parent | 78cfcbc6577dda03fb4786743e63c7995c1b910b (diff) | |
download | ruby-79e63364953573d642537c0fe4eeeb03d85c15a7.tar.gz |
* 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.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43895 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | ChangeLog | 9 | ||||
-rw-r--r-- | hash.c | 22 | ||||
-rw-r--r-- | include/ruby/st.h | 2 | ||||
-rw-r--r-- | st.c | 41 |
4 files changed, 70 insertions, 4 deletions
@@ -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. @@ -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); @@ -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) |