summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2021-03-28 09:48:45 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2021-03-28 09:48:45 +0900
commitfb6ebe55d91187d9635e0183d47dbf38e95b1141 (patch)
treee6544df386d120b3a9218643873725a2af0197c1
parent5e5fb72f99701dc27c66ab148471893f14e6d6f0 (diff)
Hide an intermediate array
-rw-r--r--hash.c6
1 files changed, 4 insertions, 2 deletions
diff --git a/hash.c b/hash.c
index 00beadf..17864a0 100644
--- a/hash.c
+++ b/hash.c
@@ -3239,7 +3239,7 @@ rb_hash_transform_keys(int argc, VALUE *argv, VALUE hash)
return result;
}
-static VALUE rb_hash_flatten(int argc, VALUE *argv, VALUE hash);
+static int flatten_i(VALUE key, VALUE val, VALUE ary);
/*
* call-seq:
@@ -3269,7 +3269,8 @@ rb_hash_transform_keys_bang(int argc, VALUE *argv, VALUE hash)
if (!RHASH_TABLE_EMPTY_P(hash)) {
long i;
VALUE new_keys = hash_alloc(0);
- VALUE pairs = rb_hash_flatten(0, NULL, hash);
+ VALUE pairs = rb_ary_tmp_new(RHASH_SIZE(hash) * 2);
+ rb_hash_foreach(hash, flatten_i, pairs);
for (i = 0; i < RARRAY_LEN(pairs); i += 2) {
VALUE key = RARRAY_AREF(pairs, i), new_key, val;
@@ -3292,6 +3293,7 @@ rb_hash_transform_keys_bang(int argc, VALUE *argv, VALUE hash)
rb_hash_aset(hash, new_key, val);
rb_hash_aset(new_keys, new_key, Qnil);
}
+ rb_ary_clear(pairs);
rb_hash_clear(new_keys);
}
return hash;