aboutsummaryrefslogtreecommitdiffstats
path: root/hash.c
diff options
context:
space:
mode:
authorcharliesome <charliesome@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-04-25 05:03:30 +0000
committercharliesome <charliesome@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-04-25 05:03:30 +0000
commit5d7b5481ca60260ef05ee969c8b142143c4aa8c2 (patch)
tree484c920c961d1dc14b8745489589f97cd2ac0ee0 /hash.c
parented33fcae5a1e6fca6fc7e10776b579c668d35451 (diff)
downloadruby-5d7b5481ca60260ef05ee969c8b142143c4aa8c2.tar.gz
* benchmark/bm_hash_shift.rb: add benchmark for Hash#shift
* hash.c (rb_hash_shift): use st_shift if hash is not being iterated to delete element without iterating the whole hash. * hash.c (shift_i): remove function * include/ruby/st.h (st_shift): add st_shift function * st.c (st_shift): ditto [Bug #8312] [ruby-core:54524] Patch by funny-falcon git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40457 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c26
1 files changed, 9 insertions, 17 deletions
diff --git a/hash.c b/hash.c
index a06e7a352b..43b6757f14 100644
--- a/hash.c
+++ b/hash.c
@@ -875,17 +875,6 @@ struct shift_var {
};
static int
-shift_i(VALUE key, VALUE value, VALUE arg)
-{
- struct shift_var *var = (struct shift_var *)arg;
-
- if (var->key != Qundef) return ST_STOP;
- var->key = key;
- var->val = value;
- return ST_DELETE;
-}
-
-static int
shift_i_safe(VALUE key, VALUE value, VALUE arg)
{
struct shift_var *var = (struct shift_var *)arg;
@@ -916,14 +905,17 @@ rb_hash_shift(VALUE hash)
rb_hash_modify_check(hash);
if (RHASH(hash)->ntbl) {
var.key = Qundef;
- rb_hash_foreach(hash, RHASH_ITER_LEV(hash) > 0 ? shift_i_safe : shift_i,
- (VALUE)&var);
-
- if (var.key != Qundef) {
- if (RHASH_ITER_LEV(hash) > 0) {
+ if (RHASH_ITER_LEV(hash) == 0) {
+ if (st_shift(RHASH(hash)->ntbl, &var.key, &var.val)) {
+ return rb_assoc_new(var.key, var.val);
+ }
+ }
+ else {
+ rb_hash_foreach(hash, shift_i_safe, (VALUE)&var);
+ if (var.key != Qundef) {
rb_hash_delete_key(hash, var.key);
+ return rb_assoc_new(var.key, var.val);
}
- return rb_assoc_new(var.key, var.val);
}
}
return hash_default_value(hash, Qnil);