Project

Profile

Help

Support #4737

Support for thread safe XSLT 3 transformation in Saxon-HE

Added by Juhi Gupta 10 days ago. Updated 10 days ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
Multithreading
Sprint/Milestone:
-
Start date:
2020-09-15
Due date:
% Done:

0%

Estimated time:
Legacy ID:
Applies to branch:
Fix Committed on Branch:
Fixed in Maintenance Release:

Description

Currently I am using Saxon-HE, downloading the dependency from maven repository

<!-- https://mvnrepository.com/artifact/net.sf.saxon/Saxon-HE -->
<dependency>
        <groupId>net.sf.saxon</groupId>
        <artifactId>Saxon-HE</artifactId>
        <version>10.1</version>
</dependency>  

I have a function which performs transformation xml to json using XSLT 3 support in Saxon-HE,

String xmltransformationusingSaxon(String transformationMapping, String data){
        TransformerFactory factory = new TransformerFactoryImpl();
        Transformer transformer = factory.newTransformer(new StreamSource(new StringReader(transformationMapping)));
        transformer.transform(new StreamSource(new StringReader(data)), new StreamResult(writer));
        output = writer.toString();
                return output;
}

I have observed there is a performance hit when we create TransformerFactory for each thread request, If it was thread safe, we can make it static and all the threads should be able to access it without causing an issue.

or if you can suggest any other option to increase the performance of the parser or achieve the requirement in any other way, since it is quite slow now.

History

#1 Updated by Juhi Gupta 10 days ago

Juhi Gupta wrote:

Currently I am using Saxon-HE, downloading the dependency from maven repository

<!-- https://mvnrepository.com/artifact/net.sf.saxon/Saxon-HE -->
<dependency>
        <groupId>net.sf.saxon</groupId>
        <artifactId>Saxon-HE</artifactId>
        <version>10.1</version>
</dependency>  

I have a function which performs transformation xml to json using XSLT 3 support in Saxon-HE,

String xmltransformationusingSaxon(String transformationMapping, String data){
        StringWriter writer = new StringWriter();
        String output;
        TransformerFactory factory = new TransformerFactoryImpl();
        Transformer transformer = factory.newTransformer(new StreamSource(new StringReader(transformationMapping)));
        transformer.transform(new StreamSource(new StringReader(data)), new StreamResult(writer));
        output = writer.toString();
        return output;
}

I have observed there is a performance hit when we create TransformerFactory for each thread request, If it was thread safe, we can make it static and all the threads should be able to access it without causing an issue.

or if you can suggest any other option to increase the performance of the parser or achieve the requirement in any other way, since it is quite slow now.

#2 Updated by Michael Kay 10 days ago

Create a single TransformerFactory instance. This can be used as often as you want, in the same thread or in different threads.

For each stylesheet you want to compile, create a Templates instance using TransformerFactory.newTemplates(). The Templates object can be used as often as you want, in the same thread or in different threads. Creating the Templates object (that is, compiling the stylesheet) is relatively expensive so we recommend caching and reusing this object.

For each transformation you want to execute, create a Transformer object using Templates.newTransformer(). In theory the Transformer object can be reused to run one transformation at a time, in a single thread, but we recommend creating a new one for every transformation. The Transformer is not thread-safe because it holds state that is specific to a particular transformation, such as the values of parameters and the cache of documents loaded using the document() function. Creating a Transformer from a Templates object is a cheap operation and you don't need to worry about doing it repeatedly.

The method TransformerFactory.newTransformer() should only be used in cases where you only want to use the stylesheet once.

#3 Updated by Michael Kay 10 days ago

  • Tracker changed from Feature to Support

Please register to edit this issue

Also available in: Atom PDF