Ruby XML Generation (Rails Performance Series)

The standard way to build XML in Ruby is with the Builder gem.

Builder iterates over each character in the output text to determine if it needs to escape it. When generating larger XML documents, this can be quite slow.

Thankfully, the solution is already available via the 1fast_xs gem. This gem does compile an extension, so you’ll need a working compiler.

1gem install fast_xs

Then just require it after Builder:

1require 'builder'
2require 'fast_xs'

It will be used automatically by Builder going forward.

If your app is built on Rails, it’s even easier. Just install the gem—that’s it! Recent versions of Rails will auto-detect the existence of 1fast_xs and enable it.

tags: performance, builder, xml, ruby, rails