This is an automated email from the ASF dual-hosted git repository.

pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git


The following commit(s) were added to refs/heads/trunk by this push:
     new b025d5b633 Cache the TransformerFactory and skip unwanted comment 
lookups (#1241)
b025d5b633 is described below

commit b025d5b633b08cde9fa8d546aded68463b5decb5
Author: PJ Fanning <[email protected]>
AuthorDate: Thu Sep 3 19:45:44 2026 +0100

    Cache the TransformerFactory and skip unwanted comment lookups (#1241)
    
    * Cache the TransformerFactory and skip unwanted comment lookups
    
    XMLHelper.newTransformer() called TransformerFactory.newInstance() on
    every invocation, repeating the JAXP provider lookup and the security
    configuration each time. It is called once per part with relationships
    on every OPC save (ZipPartMarshaller.marshallRelationshipPart) plus
    once for the content types part. Measured locally, newInstance() plus
    the configuration is ~87% of the call cost, so the factory is now built
    once and reused. TransformerFactory is not guaranteed thread-safe and
    the implementation is pluggable, so the creation call is synchronized;
    the lock is free when uncontended and only guards the cheap identity
    transformer creation, not the transform itself.
    
    The other two factories in this class are already cached the same way.
    
    XSSFExcelExtractor called cell.getCellComment() for every cell before
    checking includeCellComments, so the default extraction path paid a
    comment-table lookup per cell (and a linear VML shape scan for every
    commented cell) only to discard the result.
    
    Co-Authored-By: Claude Fable 5 <[email protected]>
    
    * Document the thread-safety position of the cached XML factories
    
    JAXP does not specify whether DocumentBuilderFactory or SAXParserFactory
    may be used concurrently, so the previous "newDocumentBuilder is
    thread-safe" comment claimed more than the spec gives. The JDK
    implementations only read the factory's feature/attribute state when
    creating a parser, and POI configures these factories once and never
    mutates them, so sharing them is safe in practice - and guarding them
    would serialize much hotter paths than the transformer one.
    
    Co-Authored-By: Claude Fable 5 <[email protected]>
    
    ---------
    
    Co-authored-by: Claude Fable 5 <[email protected]>
---
 .../apache/poi/xssf/extractor/XSSFExcelExtractor.java    | 16 +++++++++-------
 poi/src/main/java/org/apache/poi/util/XMLHelper.java     | 16 ++++++++++++++--
 2 files changed, 23 insertions(+), 9 deletions(-)

diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java 
b/poi-ooxml/src/main/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java
index 151e7d6da4..78b67e3f66 100644
--- 
a/poi-ooxml/src/main/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java
+++ 
b/poi-ooxml/src/main/java/org/apache/poi/xssf/extractor/XSSFExcelExtractor.java
@@ -169,13 +169,15 @@ public class XSSFExcelExtractor
                     }
 
                     // Output the comment, if requested and exists
-                    Comment comment = cell.getCellComment();
-                    if(includeCellComments && comment != null) {
-                        // Replace any newlines with spaces, otherwise it
-                        //  breaks the output
-                        String commentText = 
comment.getString().getString().replace('\n', ' ');
-                        checkMaxTextSize(text, commentText);
-                        text.append(" Comment by 
").append(comment.getAuthor()).append(": ").append(commentText);
+                    if(includeCellComments) {
+                        Comment comment = cell.getCellComment();
+                        if(comment != null) {
+                            // Replace any newlines with spaces, otherwise it
+                            //  breaks the output
+                            String commentText = 
comment.getString().getString().replace('\n', ' ');
+                            checkMaxTextSize(text, commentText);
+                            text.append(" Comment by 
").append(comment.getAuthor()).append(": ").append(commentText);
+                        }
                     }
 
                     if(ri.hasNext()) {
diff --git a/poi/src/main/java/org/apache/poi/util/XMLHelper.java 
b/poi/src/main/java/org/apache/poi/util/XMLHelper.java
index 58e9acfaa5..d0a4892561 100644
--- a/poi/src/main/java/org/apache/poi/util/XMLHelper.java
+++ b/poi/src/main/java/org/apache/poi/util/XMLHelper.java
@@ -83,11 +83,20 @@ public final class XMLHelper {
     private static final Logger LOG = PoiLogManager.getLogger(XMLHelper.class);
     private static long lastLog;
 
-    // DocumentBuilderFactory.newDocumentBuilder is thread-safe
+    // JAXP does not state whether these two factories may be used by several 
threads at once, but
+    // the JDK implementations only read their feature/attribute state when 
creating a parser, and
+    // both are configured here once and never mutated afterwards, so they are 
shared unguarded.
+    // Guarding them would serialize the much hotter parser-creation paths.
     private static final DocumentBuilderFactory documentBuilderFactory = 
getDocumentBuilderFactory();
 
     private static final SAXParserFactory saxFactory = getSaxParserFactory();
 
+    // TransformerFactory differs: its javadoc says "Different 
TransformerFactories can be used
+    // concurrently by different Threads", i.e. one factory is not meant to be 
shared, so
+    // newTransformer synchronizes on it. Only the (cheap) transformer 
creation is guarded -
+    // the transformation itself runs outside the lock.
+    private static final TransformerFactory transformerFactory = 
getTransformerFactory();
+
     @FunctionalInterface
     private interface SecurityFeature {
         void accept(String name, boolean value) throws 
ParserConfigurationException, SAXException, TransformerException;
@@ -237,7 +246,10 @@ public final class XMLHelper {
     }
 
     public static Transformer newTransformer() throws 
TransformerConfigurationException {
-        Transformer serializer = getTransformerFactory().newTransformer();
+        final Transformer serializer;
+        synchronized (transformerFactory) {
+            serializer = transformerFactory.newTransformer();
+        }
         // TODO set encoding from a command argument
         serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
         serializer.setOutputProperty(OutputKeys.INDENT, "no");


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to