Project

General

Profile

Actions

Feature #14146

closed

Improve performance of creating Hash object

Added by watson1978 (Shizuo Fujita) over 6 years ago. Updated over 6 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:84008]

Description

When generate Hash object, the heap area of st_table will be always allocated in internally
and seems it take a time.

To improve performance of creating Hash object,
this patch will reduce count of allocating heap areas for st_table by reuse them.

Performance of creating Hash literal -> 1.53 times faster.

Before

$ ./miniruby -v -I. -I../benchmark-ips/lib ~/tmp/bench/literal.rb
ruby 2.5.0dev (2017-11-28 hash 60926) [x86_64-darwin17]
Warming up --------------------------------------
        Hash literal    51.544k i/100ms
Calculating -------------------------------------
        Hash literal    869.132k (± 1.1%) i/s -      4.381M in   5.041574s

After

$ ./miniruby -v -I. -I../benchmark-ips/lib ~/tmp/bench/literal.rb
ruby 2.5.0dev (2017-11-28 hash 60926) [x86_64-darwin17]
Warming up --------------------------------------
        Hash literal    63.068k i/100ms
Calculating -------------------------------------
        Hash literal      1.328M (± 2.3%) i/s -      6.685M in   5.037861s

Test code

require 'benchmark/ips'

Benchmark.ips do |x|
  x.report "Hash literal" do |loop|
    count = 0
    while count < loop
      hash = {foo: 12, bar: 34, baz: 56}

      count += 1
    end
  end
end

Patch

https://github.com/ruby/ruby/pull/1766

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0