1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/test
tenderlove 2f37847820 Speed up hash literals by duping
This commit replaces the `newhashfromarray` instruction with a `duphash`
instruction.  Instead of allocating a new hash from an array stored in
the Instruction Sequences, store a hash directly in the instruction
sequences and dup it on execution.

== Instruction sequence changes ==

```ruby
code = <<-eorby
  { "foo" => "bar", "baz" => "lol" }
eorby

insns = RubyVM::InstructionSequence.compile(code, __FILE__, nil, 0, frozen_string_literal: true)
puts insns.disasm
```

On Ruby 2.5:

```
== disasm: #<ISeq:<compiled>@test.rb:0 (0,0)-(0,36)>====================
0000 putobject        "foo"
0002 putobject        "bar"
0004 putobject        "baz"
0006 putobject        "lol"
0008 newhash          4
0010 leave
```

Ruby 2.6@r66174 3b6321083a:

```
$ ./ruby test.rb
== disasm: #<ISeq:<compiled>@test.rb:0 (0,0)-(0,36)> (catch: FALSE)
0000 newhashfromarray             2, ["foo", "bar", "baz", "lol"]
0003 leave
```

Ruby 2.6 + This commit:

```
$ ./ruby test.rb
== disasm: #<ISeq:<compiled>@test.rb:0 (0,0)-(0,36)> (catch: FALSE)
0000 duphash                      {"foo"=>"bar", "baz"=>"lol"}
0002 leave
```

== Benchmark Results ==

Compared to 2.5.3:

```
$ make benchmark ITEM=hash_literal_small COMPARE_RUBY=/Users/aaron/.rbenv/versions/2.5.3/bin/ruby
generating known_errors.inc
known_errors.inc unchanged
./revision.h unchanged
/Users/aaron/.rbenv/shims/ruby --disable=gems -rrubygems -I./benchmark/lib ./benchmark/benchmark-driver/exe/benchmark-driver \
	            --executables="compare-ruby::/Users/aaron/.rbenv/versions/2.5.3/bin/ruby -I.ext/common --disable-gem" \
	            --executables="built-ruby::./miniruby -I./lib -I. -I.ext/common  -r./prelude --disable-gem" \
	            $(find ./benchmark -maxdepth 1 -name '*hash_literal_small*.yml' -o -name '*hash_literal_small*.rb' | sort)
Calculating -------------------------------------
                     compare-ruby  built-ruby
 hash_literal_small2        1.498       1.877 i/s -       1.000 times in 0.667581s 0.532656s
 hash_literal_small4        1.197       1.642 i/s -       1.000 times in 0.835375s 0.609160s
 hash_literal_small8        0.620       1.215 i/s -       1.000 times in 1.611638s 0.823090s

Comparison:
              hash_literal_small2
          built-ruby:         1.9 i/s
        compare-ruby:         1.5 i/s - 1.25x  slower

              hash_literal_small4
          built-ruby:         1.6 i/s
        compare-ruby:         1.2 i/s - 1.37x  slower

              hash_literal_small8
          built-ruby:         1.2 i/s
        compare-ruby:         0.6 i/s - 1.96x  slower
```

Compared to r66255

```
$ make benchmark ITEM=hash_literal_small COMPARE_RUBY=/Users/aaron/.rbenv/versions/ruby-trunk/bin/ruby
generating known_errors.inc
known_errors.inc unchanged
./revision.h unchanged
/Users/aaron/.rbenv/shims/ruby --disable=gems -rrubygems -I./benchmark/lib ./benchmark/benchmark-driver/exe/benchmark-driver \
	            --executables="compare-ruby::/Users/aaron/.rbenv/versions/ruby-trunk/bin/ruby -I.ext/common --disable-gem" \
	            --executables="built-ruby::./miniruby -I./lib -I. -I.ext/common  -r./prelude --disable-gem" \
	            $(find ./benchmark -maxdepth 1 -name '*hash_literal_small*.yml' -o -name '*hash_literal_small*.rb' | sort)
Calculating -------------------------------------
                     compare-ruby  built-ruby
 hash_literal_small2        1.567       1.831 i/s -       1.000 times in 0.638056s 0.546039s
 hash_literal_small4        1.298       1.652 i/s -       1.000 times in 0.770214s 0.605182s
 hash_literal_small8        0.873       1.216 i/s -       1.000 times in 1.145304s 0.822047s

Comparison:
              hash_literal_small2
          built-ruby:         1.8 i/s
        compare-ruby:         1.6 i/s - 1.17x  slower

              hash_literal_small4
          built-ruby:         1.7 i/s
        compare-ruby:         1.3 i/s - 1.27x  slower

              hash_literal_small8
          built-ruby:         1.2 i/s
        compare-ruby:         0.9 i/s - 1.39x  slower
```

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66258 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-06 18:28:21 +00:00
..
-ext- Show diff in failure messages 2018-12-06 05:29:07 +00:00
base64
benchmark
bigdecimal Import bigdecimal 1.4.0.pre.20181205a 2018-12-05 11:30:24 +00:00
cgi
coverage ext/coverage/: add the oneshot mode 2018-10-20 05:33:04 +00:00
csv Merge csv-3.0.0 from ruby/csv repository. 2018-09-05 13:33:21 +00:00
date date_core.c: respect COMPLEX_DAT bit 2018-11-13 04:29:31 +00:00
dbm Support old versions of Ruby with FrozenError. 2018-08-07 12:47:11 +00:00
digest
drb Just skip on armv7l-linux 2018-11-03 23:08:08 +00:00
dtrace
erb erb.rb: warn invalid trim_mode [Misc #15294] 2018-11-12 00:37:15 +00:00
etc
excludes test/excludes/_appveyor: exclude memory-inefficient 2018-12-04 09:37:23 +00:00
fiddle test_function.rb: loosen delta boundary 2018-11-13 06:17:57 +00:00
fileutils
gdbm Support old versions of Ruby with FrozenError. 2018-08-07 12:47:11 +00:00
io io + socket: make pipes and sockets nonblocking by default 2018-11-22 08:46:51 +00:00
irb Suppress read of ~/.irbrc 2018-08-18 04:09:48 +00:00
json Fix JSON::Parser against bigdecimal updates 2018-12-02 05:21:57 +00:00
lib mjit_build_dir: separate MJIT_BUILD_DIR 2018-11-07 02:49:26 +00:00
logger
matrix lib/matrix.rb: Make Matrix & Vector mutable. Add #[]=, #map!. 2018-11-02 17:52:51 +00:00
minitest object.c: Deprecate Object#=~ and add NilClass#=~` 2018-11-26 07:55:07 +00:00
misc
mkmf test/mkmf/base.rb: use CC_WRAPPER same as the toplevel 2018-11-17 01:42:30 +00:00
monitor lib/monitor.rb: avoid race conditions by Thread.handle_interrupt 2018-11-28 01:51:44 +00:00
net test_ftp.rb: loosen another timeout requirement 2018-11-14 08:21:05 +00:00
nkf
objspace ext/objspace/objspace_dump.c: print addresses consistently 2018-10-09 03:21:57 +00:00
open-uri
openssl openssl: sync with upstream repository 2018-10-18 00:24:49 +00:00
optparse
ostruct Support old versions of Ruby with FrozenError. 2018-08-07 12:47:11 +00:00
pathname Pathname: Have #relative_path_from accept String argument. 2018-11-13 06:20:06 +00:00
psych Make rubygems follow the upstream of psych 2018-11-11 00:20:27 +00:00
rdoc Merge rdoc-6.1.0.bata2 2018-11-27 07:02:36 +00:00
readline Readline: expose rl_completion_quote_character variable 2018-09-02 23:27:08 +00:00
resolv Fix a typo [ci skip] 2018-11-11 11:58:38 +00:00
rexml
rinda Skip EHOSTUNREACH by host issues 2018-09-18 12:55:05 +00:00
ripper parse.y: remove "shadowing outer local variable" warning 2018-10-26 03:10:02 +00:00
rss
ruby Speed up hash literals by duping 2018-12-06 18:28:21 +00:00
rubygems Re-commit r65722 2018-12-04 11:45:05 +00:00
scanf
sdbm
shell
socket disable non-blocking pipes and sockets by default 2018-11-29 20:00:00 +00:00
stringio Support old versions of Ruby with FrozenError. 2018-08-07 12:47:11 +00:00
strscan
syslog
testunit test/testunit/test_parallel.rb: respect subprocess timeout scale 2018-11-30 03:56:34 +00:00
uri
webrick webrick: fix tests on Debian sid/unstable with OpenSSL 1.1.1a 2018-12-03 04:51:08 +00:00
win32ole test_win32ole_event.rb: retry #test_s_new_loop with sleep 2018-11-01 04:20:26 +00:00
yaml
zlib
colors
runner.rb
test_abbrev.rb
test_cmath.rb Add test for coverage. 2018-08-07 09:52:53 +00:00
test_delegate.rb
test_extlibs.rb
test_find.rb
test_forwardable.rb
test_ipaddr.rb
test_mutex_m.rb
test_observer.rb
test_open3.rb
test_pp.rb test_pp.rb: show the node when failed 2018-12-03 00:57:06 +00:00
test_prettyprint.rb
test_prime.rb
test_pstore.rb
test_pty.rb
test_rbconfig.rb
test_securerandom.rb
test_set.rb
test_shellwords.rb
test_singleton.rb
test_sync.rb Move obsoleted test/thread/test_*.rb with the current implementation. 2018-07-31 03:49:38 +00:00
test_syslog.rb
test_tempfile.rb
test_time.rb Time.parse based from non-Time object 2018-10-09 05:55:29 +00:00
test_timeout.rb
test_tmpdir.rb
test_tracer.rb
test_tsort.rb
test_unicode_normalize.rb
test_weakref.rb
test_win32api.rb test_win32api.rb: suppress a warning 2018-09-23 12:40:46 +00:00