There has been a performance issue that has been well documented when using Builder::XMLMarkup. Over the course of time, I’d noticed that my rails app has gotten slower and slower building XML as more data ended up in the database. On my development environment, it would take approximately 15 seconds to render the xml generated by this controller.
As it turns out, the pure ruby implementation of String#to_xs is pretty slow. Thankfully, there is a C extension which you can install to alleviate this. Installing fast_xs is as easy as:
sudo gem install fast_xs
If you’re using Rails 2.0, you’re done. If you’re using Rails 1.2.x, we’ll do some monkey patching to make this essentially a drop-in fix. We’ll utilize the same techniques that are used to address the issue in Rails 2.0. Essentially, we want to extend String to take advantage of the fast_xs method if it is available. To do this, open $(RAILS)/config/environment.rb and add the following lines to the bottom.
begin require 'fast_xs' class String alias_method :original_xs, :to_xs if method_defined?(:to_xs) alias_method :to_xs, :fast_xs end rescue LoadError #fast_xs extension unavailable end
Again, this is exactly how Rails 2.0 takes care of it for you. By the way, this one single change dropped my render time from roughly 15 seconds to just under 3 seconds. If you’re building a lot of XML with Builder::XMLMarkup, you need to take a look at this.