[Sean McGrath:QOTD] Hashtables, Pre-Cooked XML, and You + Recipes For Blazing Fast XML Processing
by M. David Peterson
Memory-based caches of "cooked" data structures are your friend.
For you .NET developers here's a pre-written recipe that handles all of the dirty work of determining whether to create a new
XmlReaderor return the in-memory cached version based on the generated ETag for the source file (see Extended Overview below for a deeper understanding of how this works.) To use this recipe you need to do nothing more than create a new
XmlServiceOperationManagerwhen your application starts up like so,
XmlServiceOperationManager myXmlServiceOperationManager = new XmlServiceOperationManager(new Dictionary<int, XmlReader>());
and then use the
GetXmlReadermethod of the
XmlServiceOperationManager, passing in the
System.Uriobject, not the string value of the URI, though I guess it would be easy enough to create an overload that takes the string value of the URI. Another task for another day. ;-)) of the desired XML file to get an
XmlReaderin return like so,
XmlReader reader = myXmlServiceOperationManager.GetXmlReader(requestUri);
That's it! Now you can use your "new"
XmlReaderhowever you might need and the next time that file is requested for processing if it hasn't changed you save all of the time it would normally take to read the source file and convert it into an
XmlReaderwhich is fairly significant.
Source code and extended explanation inline below. Enjoy!
Oh, and stay tuned for the next installment of this recipe where we learn how adding,
1 Part memcached
1 Part ETag's
1 Part GZip encoding
... can turn your lame a$$ performance sucking web application into a lean, mean, kick a$$ performing machine. For a precursor, see Joe Gregorio's AtomPub presentation slides from this past OSCON. I assure you, it's worth every second you spend studying this gem of a resource.
This code looks very interesting. I have used XML functions in .Net for years, and I know the value of eTag header for caching, but I never seen anything like this.
Something seems wrong though. I would love to be able to test the code, but I need a little help getting the complete code from SVN as well as setting up the Saxon and other external libraries.
Who do I have to knock off?
|M. David Peterson
|M. David Peterson