views:

120

answers:

2

I have a large-ish array (~400 elements) of ActiveRecord objects that I need to convert to XML. I've used array.to_xml for convenience, but it's very slow -- about 20 seconds when the server is busy, and about 5 seconds when idle.

I've just run a few benchmarks while the server was idle, and found that:

  • the ActiveRecord query (complete with two-level :include) takes about 0.3s on average.
  • converting that result set to XML takes about 4.9s on average. 4.86s of that is User CPU time.

Is there a drop-in replacement for Builder::XmlMarkup that will improve the speed of to_xml? Or will I have to hand-roll something?

+1  A: 

Following link claims a 2 - 3x speed increase. It's not a drop in replacement but rather a technique one uses to build a structure that to_xml will traverse faster.. Faster alternatives to ActiveRecord::Base.to_xml (Rails Performance Series)

glongman
A: 

You might as well want to check out http://github.com/rti/FastXml

This is a simple Rails plug-in which replaces Array#to_xml and ActiveRecord::Base#to_xml. It uses the 'libxml-ruby' gem (which is a native binding to libxml) to generate the documents.

rTi