Better XML parsing with Rails
We recently built a Ruby on Rails application that was receiving a large amount of XML. This as you can imagine was pretty CPU intensive, especially as Rails (ActiveSupport) uses REXML under the covers to parse the incoming requests. I started to wonder if it was possible to use a different XML parsing library, for an overview of some of the available libraries and benchmarks see John Nunemaker's post about parsing xml with ruby and the rubyinside post ruby xml performance benchmarks. I've used most of these libraries at some point or another for parsing XML and I was really impressed with Nokogiri, and I desperately wanted to use it in production for the Rails backed parsing. So I started to look at the Rails source and I noticed this commit http://github.com/rails/rails/commit/694998ee4fb8d257ba78424cab630846327a0889.
YES! All you have to do (besides installing Nokogiri on your server) is specify:
YES! All you have to do (besides installing Nokogiri on your server) is specify:
ActiveSupport::XmlMini.backend = 'Nokogiri'
In your production.rb environment and your done. The results speak for themselves, here is a snapshot of our CPU usage before and after, with the same load.
