XSLTCompiledTransform And Caching

I'd like to cache the XSLTCompiledTransform object in a Dictionary as I'm having a performance problem with the time it takes to load XSLT from disk.

My question is, am I ok to just add the object into the dictionary without cloning it  

When I've done this before in the DNA world I've always had to clone the document but I notice this isn't supported on this object.  I also note that the docs state this is thread safe and should be ok.

Any thoughts anyone



Answer this question

XSLTCompiledTransform And Caching

  • Shivlee

    If you are talking aobut Fx 2.0 the documents are very clear:

    Thread Safety
    The XslCompiledTransform object is thread safe once it has been loaded. In other words, after the Load method has successfully completed, the Transform method can be called simultaneously from multiple threads.

    If the Load method is called again in one thread while the Transform method is being called in another thread, the XslCompiledTransform object finishes executing the Transform call by continuing to use the old state. The new state is used when the Load method successfully completes.

    The Load method is not thread safe when called simultaneously from multiple threads.

    If you are talking about Fx 1.1 the XslTransform class must be cloned, or instantiated multiple times if you want to evaluate in a multi-threaded environment.

    Regards,


  • GeorgeMTL

    Sorry, should have made it clear - .Net V2.0

    I read the above docs and figured that it must be ok.  It just didn't feel correct!


  • HamidFULL

    As stated in the documentation (http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpref/html/frlrfSystemXmlXslXslTransformClassTopic.asp)

    The .Net 1.1 XslTransform IS thread-safe ONLY for transform operations.

    Regards,

    David Hernandez



  • ArtSoft

    You will have to Clone the instance before using it.

    Diego Gonzalez
    Lagash Systems SA
    C# MVP

  • LuisGamma

    Yeah I would have thought that too, however, it seems to work and the docs imply that it's thread safe so you can use it in a multi threaded environment.

    There's also the added complication of it not implementing ICloneable.


  • crazylikeastraw

    XslCompiledTransform was designed to be cached once loaded. It provides excellent transfirmation performance at a price of slower loading (compilation XSLT into MSIL) time.

    And once it's loaded you don't need to clone it before using - Transform() methods are thread safe.


  • XSLTCompiledTransform And Caching