summaryrefslogtreecommitdiff
path: root/hash.c
diff options
context:
space:
mode:
authorglass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-11-27 16:07:10 +0000
committerglass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-11-27 16:07:10 +0000
commitb426e1b1fafdab688c646cef8245fa72403d37bb (patch)
treef2f8f4a5cdc96af495ee1867c3aaef0f0f2d244a /hash.c
parent884682755a471545145eb4a4598d06d9bc4dd2d6 (diff)
* st.c (st_keys): define st_keys(). it writes each key to buffer.
* hash.c (rb_hash_keys): use st_keys() for performance improvement if st_data_t and VALUE are compatible. * st.h: define macro ST_DATA_COMPATIBLE_P() to predicate whether st_data_t and passed type are compatible. * configure.in: check existence of builtin function to use in ST_DATA_COMPATIBLE_P(). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43885 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c22
1 files changed, 18 insertions, 4 deletions
diff --git a/hash.c b/hash.c
index a114773..4fc7b88 100644
--- a/hash.c
+++ b/hash.c
@@ -1702,12 +1702,26 @@ keys_i(VALUE key, VALUE value, VALUE ary)
VALUE
rb_hash_keys(VALUE hash)
{
- VALUE ary;
+ VALUE keys;
+ int size = RHASH_SIZE(hash);
- ary = rb_ary_new_capa(RHASH_SIZE(hash));
- rb_hash_foreach(hash, keys_i, ary);
+ keys = rb_ary_new_capa(size);
+ if (size == 0) return keys;
- return ary;
+ if (ST_DATA_COMPATIBLE_P(VALUE)) {
+ st_table *table = RHASH(hash)->ntbl;
+
+ if (OBJ_PROMOTED(keys)) rb_gc_writebarrier_remember_promoted(keys);
+ RARRAY_PTR_USE(keys, ptr, {
+ size = st_keys(table, ptr, size);
+ });
+ rb_ary_set_len(keys, size);
+ }
+ else {
+ rb_hash_foreach(hash, keys_i, keys);
+ }
+
+ return keys;
}
static int