aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorglass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-10-10 12:06:01 +0000
committerglass <glass@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-10-10 12:06:01 +0000
commit026955e37517d07679ea8044d939400d6e9beaaf (patch)
tree0892965fb6152241df67902c60b6918a1f7cac13
parent4599d3efd7bfda591fd29e5647af2d5aa8be486c (diff)
downloadruby-026955e37517d07679ea8044d939400d6e9beaaf.tar.gz
* st.c (st_keys): define st_keys() for performance improvement of
Hash#keys and Array#uniq. * st.h: ditto. * hash.c (rb_hash_keys): use st_keys(). git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43238 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog9
-rw-r--r--hash.c15
-rw-r--r--include/ruby/st.h1
-rw-r--r--st.c31
4 files changed, 44 insertions, 12 deletions
diff --git a/ChangeLog b/ChangeLog
index 6268bf2fa3..d364242095 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+Thu Oct 10 21:00:38 2013 Masaki Matsushita <glass.saga@gmail.com>
+
+ * st.c (st_keys): define st_keys() for performance improvement of
+ Hash#keys and Array#uniq.
+
+ * st.h: ditto.
+
+ * hash.c (rb_hash_keys): use st_keys().
+
Thu Oct 10 17:25:28 2013 Koichi Sasada <ko1@atdot.net>
* vm.c (vm_exec): support :b_return event for "lambda{return}.call".
diff --git a/hash.c b/hash.c
index 83d179964f..2cedeefb10 100644
--- a/hash.c
+++ b/hash.c
@@ -1663,13 +1663,6 @@ rb_hash_to_h(VALUE hash)
return hash;
}
-static int
-keys_i(VALUE key, VALUE value, VALUE ary)
-{
- rb_ary_push(ary, key);
- return ST_CONTINUE;
-}
-
/*
* call-seq:
* hsh.keys -> array
@@ -1685,12 +1678,10 @@ keys_i(VALUE key, VALUE value, VALUE ary)
VALUE
rb_hash_keys(VALUE hash)
{
- VALUE ary;
-
- ary = rb_ary_new_capa(RHASH_SIZE(hash));
- rb_hash_foreach(hash, keys_i, ary);
+ st_table *table = RHASH(hash)->ntbl;
- return ary;
+ if (!table) return rb_ary_new();
+ return st_keys(table);
}
static int
diff --git a/include/ruby/st.h b/include/ruby/st.h
index e71301b519..5968a1fc22 100644
--- a/include/ruby/st.h
+++ b/include/ruby/st.h
@@ -112,6 +112,7 @@ int st_update(st_table *table, st_data_t key, st_update_callback_func *func, st_
int st_foreach(st_table *, int (*)(ANYARGS), st_data_t);
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);
+VALUE st_keys(st_table *table);
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 6e3df628a7..43e2d71d24 100644
--- a/st.c
+++ b/st.c
@@ -1091,6 +1091,37 @@ st_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)
return 0;
}
+VALUE
+st_keys(st_table *table)
+{
+ st_table_entry *ptr = NULL;
+ st_data_t key, never = (st_data_t)Qundef;
+ VALUE keys = rb_ary_new_capa(table->num_entries);
+
+ if (table->entries_packed) {
+ st_index_t i;
+
+ for (i = 0; i < table->real_entries; i++) {
+ key = PKEY(table, i);
+ if (key == never) continue;
+ rb_ary_push(keys, (VALUE)key);
+ }
+ }
+ else {
+ ptr = table->head;
+ }
+
+ if (ptr != 0) {
+ do {
+ key = ptr->key;
+ if (key != never) rb_ary_push(keys, (VALUE)key);
+ ptr = ptr->fore;
+ } while (ptr && table->head);
+ }
+
+ return keys;
+}
+
#if 0 /* unused right now */
int
st_reverse_foreach(st_table *table, int (*func)(ANYARGS), st_data_t arg)