Chapter 12Caching compiled resources

Some steps have inputs that are compiled before use. An XSLT stylesheet, for example, is compiled before it is used to transform an input document. Similarly, Invisible XML grammars are compiled and queries passed to the XPath or XQuery steps are compiled.

If you are processing many documents with the same stylesheet (grammar, or query), you may see a performance benefit in compiling the resource once and then subsequently reusing the compiled form.

This can be accomplished by setting an extension attribute to “true” on the step:

StepExtension attribute
p:invisible-xmlcx:cache-grammar
p:xsltcx:cache-stylesheet
p:xquerycx:cache-query
cx:xpathcx:cache-expression
cx:markup-blitzcx:cache-grammar

These attributes can be attribute value templates, but they can only reference static options.

In order to be cached, the input must have a URI. The URI is the primary key for the cache lookup; however, a cached resource carries with it more than just the input source. For example, a compiled stylesheet includes any static parameters passed to the compiler, and an Invisible XML grammar includes any parsing options provided.

Consequently, you can’t change any of those aspects. For example, on any p:xslt step that’s reusing a cached grammar, any static parameters passed to the step are ignored.

Scope

In principle, the cache could keep track of all the context that is part of the compilation, but in practice that would be difficult. Instead, the cache is effectively scoped to each individual step. That is, no two distinct instances of a step share the same cache.

That means a step in a loop, or a step in a subpipeline that’s called several times, can benefit from the cache, but two distinct steps of the same type, cannot.

Cache management

There are no mechanisms to clear or invalidate the cache. The cache can be avoided by calling a step that doesn’t specify the extension attribute. The same source can be cached with a different static parameters or options by changing its base URI.