views:

170

answers:

3

I have an application written in ruby (that runs in the JRuby VM). When profiling it, I realized that it spends a lot (actually almost all of) its time converting some hashes into JSON.

These hashes have keys of symbols, values of other similar hashes, arrays, strings, and numbers.

Is there a serialization method that is suitable for such an input, and would typically run faster than JSON? It would preferable if it is has a Java or JRuby-compatible gem, too.

I am currently using the jruby-json gem, which is the fastest JSON implementation in JRuby (as I am told), so the move will most likely be to a different serialization method rather than just a different library.

Any help is appreciated! Thanks.

+2  A: 

I don't know how different the performance might be on JRuby, but in MRI I believe that Marshal is usually fastest.

One article I found found Marshal to be about 3x faster than JSON (which was itself a similiar amount faster than YAML), while another one references about a 2x difference (see the first comment for JSON).

None of which guarantees anything for your particular situation, of course. Could you try each option with Benchmark? Something like this:

require 'benchmark'
# other requires as necessary
N = 100 # or whatever multiple is needed to get a sensible result
Benchmark.bm(20) do |rpt|
  rpt.report("YAML") do
    N.times do
      # perform your task using YAML
    end
  end
  rpt.report("JSON") do
    # as above for JSON
  end
  rpt.report("Marshal") do
    # as above for JSON
  end
end
Mike Woodhouse
+4  A: 

I've just heard of this project 20 minutes ago (posted on hacker news), it has a Ruby implementation: http://msgpack.sourceforge.net/#GettingStarted

MessagePack is a binary-based efficient object serialization library. It enables to exchange structured objects between many languages like JSON. But unlike JSON, it is very fast and small.

kraymer
http://www.rubyinside.com/messagepack-binary-object-serialization-3150.html
clyfe
Is the actual format documented, or just some proprietary data format some company owns? One benefit of using an open format is that there is no vendor lock-in. Also: how is performance verified? Is it just its creator saying it is fast, or are there independent evaluations?
StaxMan
+1  A: 

(Edit: I realize now that this answer probably doesn't meet your requirements - I don't think YAJL is available for Java or JRuby. I'm leaving this here, however, since YAJL itself rocks, and this may help some future Googler.)

If you like JSON (and already have code that implements it), I highly recommend that you take a look at YAJL. There are Ruby bindings available as well: YAJL-Ruby.

It's significantly faster than Ruby's built-in JSON engine in my experience.

Telemachus