Skip to content

Commit

Permalink
* hash.c (rb_hash_flatten): performance improvement by not using
Browse files Browse the repository at this point in the history
  rb_hash_to_a() to avoid array creation with rb_assoc_new().

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@42039 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
glass committed Jul 18, 2013
1 parent faa9d5e commit 852caed
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Thu Jul 18 17:35:41 2013 Masaki Matsushita <glass.saga@gmail.com>

* hash.c (rb_hash_flatten): performance improvement by not using
rb_hash_to_a() to avoid array creation with rb_assoc_new().

Thu Jul 18 16:16:17 2013 Koichi Sasada <ko1@atdot.net>

* array.c: add logging feature for RGenGC's write barrier unprotect
Expand Down
28 changes: 21 additions & 7 deletions hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -2187,6 +2187,18 @@ rb_hash_rassoc(VALUE hash, VALUE obj)
return args[1];
}

static int
flatten_i(VALUE key, VALUE val, VALUE ary)
{
VALUE pair[2];

pair[0] = key;
pair[1] = val;
rb_ary_cat(ary, pair, 2);

return ST_CONTINUE;
}

/*
* call-seq:
* hash.flatten -> an_array
Expand All @@ -2206,15 +2218,17 @@ rb_hash_rassoc(VALUE hash, VALUE obj)
static VALUE
rb_hash_flatten(int argc, VALUE *argv, VALUE hash)
{
VALUE ary, tmp;
VALUE ary;

ary = rb_hash_to_a(hash);
if (argc == 0) {
argc = 1;
tmp = INT2FIX(1);
argv = &tmp;
ary = rb_ary_new_capa(RHASH_SIZE(hash) * 2);
rb_hash_foreach(hash, flatten_i, ary);
if (argc) {
int level = FIX2INT(*argv) - 1;
if (level > 0) {
*argv = INT2FIX(level);
rb_funcall2(ary, rb_intern("flatten!"), argc, argv);
}
}
rb_funcall2(ary, rb_intern("flatten!"), argc, argv);
return ary;
}

Expand Down

0 comments on commit 852caed

Please sign in to comment.