java.util.Map#[]= is up to 20 times slower than #put
----------------------------------------------------

                 Key: JRUBY-5885
                 URL: https://jira.codehaus.org/browse/JRUBY-5885
             Project: JRuby
          Issue Type: Improvement
          Components: Java Integration
    Affects Versions: JRuby 1.6.2
         Environment: jruby 1.6.2 (ruby-1.9.2-p136) (2011-05-23 e2ea975) (Java 
HotSpot(TM) 64-Bit Server VM 1.6.0_24) [darwin-x86_64-java]

            Reporter: Theo Hultberg
            Priority: Minor


For some reason using {{#[]=}} on a Java {{Map}} object is much slower than 
using its {{#put}} method. It is much slower than would be expected and a 
trivial alias from {{#put}} to {{#[]=}} makes them equally fast.

Here's a benchmark:

{code}
                                    user     system      total        real
HashMap#[]=                     0.303000   0.000000   0.303000 (  0.303000)
HashMap#put                     0.014000   0.000000   0.014000 (  0.014000)
Hash#[]=                        0.006000   0.000000   0.006000 (  0.006000)
{code}

code:

{code}
require 'benchmark'
require 'java'


n = 1000

import java.util.HashMap


Benchmark.bm(30) do |x|
  x.report('HashMap#[]=') do
    h = HashMap.new
    n.times do |i|
      h["#{i}"] = i
    end
  end
  
  x.report('HashMap#put') do
    h = HashMap.new
    n.times do |i|
      h.put("#{i}", i)
    end
  end
  
  x.report('Hash#[]=') do
    h = {}
    n.times do |i|
      h["#{i}"] = i
    end
  end
end
{code}

adding 

{code}
class HashMap
  alias_method :[]=, :put
end
{code}

makes the numbers for {{#[]=}} and {{#put}} the same.

I have no idea why the {{#[]=}} method is so slow. I couldn't find where it was 
defined in the JRuby source. I found {{List#[]=}}, but not {{Map#[]=}} -- but 
perhaps I just didn't look for the right thing, and I hope there's nothing 
obvious I've overlooked.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to