Uploaded image for project: 'Ruby Driver'
  1. Ruby Driver
  2. RUBY-521

Why ruby gem bson performs slower on MRI in comparing with JRuby?

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 12_01_17
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:
      Rubies:
      ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-linux]
      jruby 1.7.1 (1.9.3p327) 2012-12-03 30a153b on OpenJDK 64-Bit Server VM 1.7.0_09-b30 [linux-amd64]

      Gems:
      bson (1.8.0)
      bson_ext (1.8.0)
      bson (1.8.0 java)

      I've tested bson gem performance on MRI & JRuby and I'm curious why C code performs slower than java?
      Used contrived example:

      require 'benchmark'
      require 'active_support/time'   # for Time.parse()
      require 'bson'
      
      h = {:_id=>"1-1651543736", :s_id=>23896626, :o_id=>1, :hash_code=>2074639573, :type=>0, :date_1=>Time.parse('2013-01-20 00:00:00 UTC'), :duration=>7, :s1=>2512.0, :s2=>2512.0, :cur=>2, :date_range=>[{:date=>{:from=>Time.parse('2013-01-20 00:00:00 UTC'), :to=>Time.parse('2013-01-27 00:00:00 UTC')}, :hash_code=>-344124839}], :extra_hashes=>["1-0-ce3a1ae5bf8c6936b168f7f7ef13500f", "1-0-d2c44ee305712dc61a1740ac30ace545"], :del=>[], :state=>1, :created_at=>Time.now.utc}
      h_s = BSON.serialize h
      
      Benchmark.bmbm do |x|
        x.report('serialization') { 1000000.times {a = BSON.serialize h} }
        x.report('deserialization')  { 1000000.times {a = BSON.deserialize h_s} }
      end
      

      MRI results:

      Rehearsal ---------------------------------------------------
      serialization    37.110000   0.000000  37.110000 ( 37.158225)
      deserialization  77.020000   2.200000  79.220000 ( 79.323779)
      ---------------------------------------- total: 116.330000sec
      
                            user     system      total        real
      serialization    37.520000   0.010000  37.530000 ( 37.573440)
      deserialization  75.870000   2.420000  78.290000 ( 78.388029)
      

      JRuby results:

      Rehearsal ---------------------------------------------------
      serialization    13.630000   0.070000  13.700000 ( 12.320000)
      deserialization  25.850000   0.100000  25.950000 ( 23.235000)
      ----------------------------------------- total: 39.650000sec
      
                            user     system      total        real
      serialization    12.260000   0.000000  12.260000 ( 11.864000)
      deserialization  24.040000   0.140000  24.180000 ( 22.598000)
      

            Assignee:
            brandon.black@10gen.com Brandon Black
            Reporter:
            happysoft Vadim Voitsiakh
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: