开发者

Simple Serialization Faster Than JSON? (in Ruby)

I have an application written in ruby (that runs in the JRuby VM). When profiling it, I realized that it spe开发者_运维技巧nds 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.


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.


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


(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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜