libxml-ruby 1.1.3 – Boosting Performance

I’m happy to announce the release of libxml-ruby 1.1.3. Besides including the usual assortment of new features and bug fixes, this release also includes a speed boost of roughly 10% to 20%.

This resulted from RubyInside’s recent post summarizing the performance of Ruby parsers. As expected, libxml-ruby blew away Hpricot and REXML in pure parsing speed (which of course is a simplistic view of what is important in an xml processor, but nevertheless still important). But it consistently finished a bit behind Nokogiri.

I was a bit surprised by that since libxml-ruby and Nokogiri use the libxml2 library as their parsing engine. Since the specific test cases almost exclusively tested parsing, the two extensions should have identical run times.

Since the times were different, then the obvious conclusion was that the two extensions were using different libxml2 APIs or using different settings. I suspected the second, but when investigating performance you never know beforehand.

Not to bore everyone with the nitty-gritty details of using libxml2, but when looking into the first test, parsing an in-memory string, it didn’t look there was much difference in API calls.

For libxml-ruby:

xmlCreateMemoryParserCtxt
xmlParseDocument

For Nokogiri:

xmlReadMemory
  -> xmlCreateMemoryParserCtxt
  -> xmlDoRead
     -> xmlParseDocument

So that didn’t solve the mystery.

The next possibility was xmlDoRead was modifying the libxml2 parser context. Now a libxml2 parser context is a beast of a thing – for those brave souls who want to take a peek, its defined in libxml2’s online documentation.

Working through the options one-by-one, I finally found the culprit, an obscure field in the structure:

int	dictNames	: Use dictionary names for the tree

What this setting controls is whether libxml2 uses a dictionary to cache strings it has previously parsed. Caching strings makes a big difference, so by default it should be enabled. That is now the case with libxml-ruby 1.1.3 and higher.

Rerunning the published benchmarks now shows libxml-ruby and Nokogiri to have equivalent performance. If you run the tests yourself, beware though. The order in which the extensions are tested changes the results. Whichever extension is tested first will always be faster, at least on my Fedora 10 box. I assume that’s because the first parser has more memory available to it when the test begins and therefore invokes Ruby’s garbage collector a few times less.

  1. Ted Han
    March 22, 2009

    Hey Charlie,

    Cool stuff. Is the 10-20% boost also an artefact of the test inputs?

    Reply
  2. March 23, 2009

    FYI, I believe this introduced a change in Node#add_child, with respect to its return type. Specifically, node.add_child(subnode) previously evaluated to subnode, while (node << subnode) evaluates to node. It seems the 1.1.3 behavior of add_child evaluates to node now as well.

    Reply
  3. March 23, 2009

    That’s what I get for not previewing. The gist, again:

    # 1.1.2
    >> node.add_child(subnode)
    => subnode

    # 1.1.3
    >> node < < subnode => node

    >> node.add_child(subnode)
    => node

    Reply
  4. Charlie
    March 29, 2009

    Hi Ted,

    The performance boost is with parsing xml documents (versus creating them). So its more noticeable on longer documents of course.

    Reply

Leave a Reply

Your email address will not be published.

Top