From: "headius (Charles Nutter)" <headius@...>
Date: 2014-01-03T08:44:10+09:00
Subject: [ruby-core:59486] [ruby-trunk - Feature #8998] string keys for hash	literals should use fstrings


Issue #8998 has been updated by headius (Charles Nutter).


I've implemented something like this for JRuby 9k. Both the interpreter and the compiler pre-freeze and dedup the keys, allowing them to go straight into literal hashes.

Performance is significantly improved by doing this:

Before:

$ jruby -rbenchmark -e "10.times { puts Benchmark.measure { hash = nil; 10_000_000.times { hash = { 'a' => 1, 'b' => 1, 'c' => 1, 'd' => 1, 'e' => 1 } } } }"
  5.810000   0.100000   5.910000 (  5.282000)
  5.230000   0.030000   5.260000 (  5.101000)
  4.710000   0.020000   4.730000 (  4.509000)
  4.500000   0.030000   4.530000 (  4.375000)
  4.630000   0.030000   4.660000 (  4.516000)
  4.510000   0.020000   4.530000 (  4.390000)
  4.560000   0.030000   4.590000 (  4.433000)
  4.580000   0.030000   4.610000 (  4.455000)
  4.540000   0.020000   4.560000 (  4.416000)
  4.560000   0.030000   4.590000 (  4.435000)

After:

$ jruby -rbenchmark -e "10.times { puts Benchmark.measure { hash = nil; 10_000_000.times { hash = { 'a' => 1, 'b' => 1, 'c' => 1, 'd' => 1, 'e' => 1 } } } }"
  3.270000   0.100000   3.370000 (  2.295000)
  2.140000   0.020000   2.160000 (  2.069000)
  1.870000   0.010000   1.880000 (  1.779000)
  1.930000   0.020000   1.950000 (  1.744000)
  1.790000   0.010000   1.800000 (  1.734000)
  1.810000   0.010000   1.820000 (  1.750000)
  1.790000   0.010000   1.800000 (  1.743000)
  1.820000   0.020000   1.840000 (  1.769000)
  1.820000   0.010000   1.830000 (  1.766000)
  1.770000   0.010000   1.780000 (  1.712000)

For reference, 2.1p0 on my system:

$ rvm ruby-2.1 do ruby -rbenchmark -e "10.times { puts Benchmark.measure { hash = nil; 10_000_000.times { hash = { 'a' => 1, 'b' => 1, 'c' => 1, 'd' => 1, 'e' => 1 } } } }"
  9.060000   0.040000   9.100000 (  9.098937)
  9.030000   0.030000   9.060000 (  9.064616)
  8.980000   0.020000   9.000000 (  9.007513)
  9.050000   0.030000   9.080000 (  9.078869)
  9.250000   0.040000   9.290000 (  9.292398)
  9.020000   0.030000   9.050000 (  9.053593)
  9.080000   0.030000   9.110000 (  9.113024)
  8.970000   0.020000   8.990000 (  8.986615)
  8.870000   0.010000   8.880000 (  8.884534)
  8.940000   0.010000   8.950000 (  8.954753)

When I have the commit done (needs a little cleanup...only a couple hours work at the moment) I will link here in case it is useful.
----------------------------------------
Feature #8998: string keys for hash literals should use fstrings
https://bugs.ruby-lang.org/issues/8998#change-44033

Author: normalperson (Eric Wong)
Status: Closed
Priority: Low
Assignee: 
Category: core
Target version: 2.1.0


While we're introducing optimizations from frozen strings,
string keys inside hashes should be frozen at the compiler level
to prevent duplication.

	a = { "ABC" => :t }
	b = { "ABC" => :t }

	# the following ought to print true
	p(a.keys[0].object_id == b.keys[0].object_id)

This should introduce no incompatibilities and be transparent to users of
older rubies.



-- 
http://bugs.ruby-lang.org/