ruby performance marshalling JSON YAML

In: rails tricks

10 Aug 2011

While we are using memcached to keep a lot of things available for our app, and now we switched to Rails 3 and for memcached we are using the faster Dalli Client, I was kind of interested about the marshalling performance of different methods.

For this performance test I have made a copy of a quite big object from one of our production apps. If this object is converted plain to YAML it takes about of 3.7 MB, and in byte stream ruby marshalling about 1.8 MB. It consists mainly of big hashes, whereas the hashes hold strings as keys and integers as values. Summed all values together in that object that might be around 200k objects (basically short strings and DWORD integers)

Using ruby 1.8.7 converting that object to YAML is quite a pain. Not using only over 200 MB of RAM after calling the .to_yaml method but also taking 25.6 seconds to fullfill that request on a small Debian 6 system.

The .to_json performs an edge faster with 3.8 seconds and using only about ~ 30 MB of RAM usage for the ruby process.

Much better performance showed the built-in marshalling method. The ruby built-in Marshal.dump resp. Marshal.load methods did not show a significant difference. Dumping and loading take each approximately 90 ms.

Finally retrieving that object from memcached, whose sub objects are kept in 5 chunks to not excess the 1 MB limit takes about 40 ms. And in this time not only the bytestream is being converted to an object, but also the whole data is transmitted via TCP sockets.

YAML*25.6 s3.7 MB0.351 %
JSON3.8 s2.8 MB2.36 %
marshal0.09 s1.8 MB100%
0.04 sn.a.225 %

*excessive RAM usage when streaming to YAML.


We can conclude ruby marshalling in this scenario is about 40 times faster than converting the object to JSON and almost 300 times faster than YAML. All measured with ruby 1.8.7. But the crown goes to memcached and its Dalli gem, delivering even more than 2 times of rubys built-in marshalling performance.

Finally I hope there is going to be some major improvements to the built-in JSON and YAML performance to come with future ruby and rails versions. Enough yaml libraries out there ...

Comment Form

You need to enable javascript in order to use Simple CAPTCHA.
Security Code: