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 03681ab76e Avoid building XmlBeans objects for every shared string
(#1244)
03681ab76e is described below
commit 03681ab76e4e5d7507479899bb9a8a81c2c5e25f
Author: PJ Fanning <[email protected]>
AuthorDate: Thu Sep 3 20:31:24 2026 +0100
Avoid building XmlBeans objects for every shared string (#1244)
Reading and writing string cells created an XmlBeans document store per
cell on both paths.
SharedStringsTable.addEntry serialised the CTRst to an XML fragment with
xmlText() purely to use it as a map key, for every string cell written
including duplicates, and readFrom did the same for every entry on open.
Entries that hold nothing but text (no formatting runs, no phonetic
data) are now keyed by that text in a separate map, so the raw text can
never collide with the XML fragment of a rich string. Text with leading
or trailing whitespace still uses the XML key, because for those the
xml:space attribute is significant but is not part of the text.
The event model round-tripped every string through XmlBeans and back:
ReadOnlySharedStringsTable stores plain Strings but getItemAt wrapped
each one in an XSSFRichTextString, and XSSFSheetXMLHandler immediately
called toString() on it. The inline-string branch did the same to a
string it already had. That round trip is not the identity - it applies
utfDecode - so the handler now applies exactly that decoding, keeping
the XSSFRichTextString path for the rare strings containing "_x".
getItemAt keeps its signature and behaviour; the raw accessor added
next to it is package private, so no public API changes.
Co-authored-by: Claude Fable 5 <[email protected]>
---
.../eventusermodel/ReadOnlySharedStringsTable.java | 20 +++++++-
.../xssf/eventusermodel/XSSFSheetXMLHandler.java | 31 ++++++++++--
.../apache/poi/xssf/model/SharedStringsTable.java | 57 +++++++++++++++++++---
3 files changed, 97 insertions(+), 11 deletions(-)
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/ReadOnlySharedStringsTable.java
b/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/ReadOnlySharedStringsTable.java
index df69936c7c..1569408449 100644
---
a/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/ReadOnlySharedStringsTable.java
+++
b/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/ReadOnlySharedStringsTable.java
@@ -224,10 +224,28 @@ public class ReadOnlySharedStringsTable extends
DefaultHandler implements Shared
@Override
public RichTextString getItemAt(int idx) {
+ return new XSSFRichTextString(getRawItemAt(idx));
+ }
+
+ /**
+ * Return the raw text of the string at the given index, ie the text as it
was stored in the
+ * shared strings table, without wrapping it in a {@link RichTextString}.
Note that any
+ * <code>_xHHHH_</code> escape sequences are <em>not</em> decoded, just
like they are not
+ * decoded by {@link #getItemAt(int)} - the decoding happens in
+ * {@link XSSFRichTextString#toString()}.
+ * <p>
+ * This is kept package private on purpose - it exists so that
+ * {@link XSSFSheetXMLHandler} can avoid creating an {@link
XSSFRichTextString}
+ * (and the XmlBeans object backing it) for every string cell.
+ *
+ * @param idx index of the string to return
+ * @return the raw text at the specified position in this Shared String
table
+ */
+ String getRawItemAt(int idx) {
if (strings == null || idx >= strings.size()) {
throw new IllegalStateException("Cannot get item at " + idx + "
with strings: " + strings);
}
- return new XSSFRichTextString(strings.get(idx));
+ return strings.get(idx);
}
//// ContentHandler methods ////
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java
b/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java
index 2a82e3e358..3d158b3ff8 100644
---
a/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java
+++
b/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java
@@ -413,8 +413,7 @@ public class XSSFSheetXMLHandler extends DefaultHandler {
case INLINE_STRING:
// TODO: Can these ever have formatting on them?
- XSSFRichTextString rtsi = new
XSSFRichTextString(value.toString());
- thisStr = rtsi.toString();
+ thisStr = decodeText(value.toString());
break;
case SST_STRING:
@@ -422,8 +421,14 @@ public class XSSFSheetXMLHandler extends DefaultHandler {
if (!sstIndex.isEmpty()) {
try {
int idx = parseInt(sstIndex);
- RichTextString rtss =
sharedStringsTable.getItemAt(idx);
- thisStr = rtss.toString();
+ if (sharedStringsTable instanceof
ReadOnlySharedStringsTable roSst) {
+ // this table holds plain strings anyway, so
we can skip creating
+ // a RichTextString (and its XmlBeans backing
object) per cell
+ thisStr = decodeText(roSst.getRawItemAt(idx));
+ } else {
+ RichTextString rtss =
sharedStringsTable.getItemAt(idx);
+ thisStr = rtss.toString();
+ }
} catch (NumberFormatException ex) {
LOG.atError().withThrowable(ex).log("Failed to
parse SST index '{}'", sstIndex);
}
@@ -462,6 +467,24 @@ public class XSSFSheetXMLHandler extends DefaultHandler {
output.cell(cellRef, thisStr, comment);
}
+ /**
+ * Returns the same text as <code>new
XSSFRichTextString(text).toString()</code> would,
+ * but without building the XmlBeans object backing the rich text string.
+ * <p>
+ * The only transformation that such a round-trip applies to a plain
string is the decoding
+ * of the <code>_xHHHH_</code> escape sequences, so the (rare) strings
that may contain such
+ * a sequence are still handled by {@link XSSFRichTextString}.
+ */
+ private static String decodeText(String text) {
+ if (text == null) {
+ return "";
+ }
+ if (!text.contains("_x")) {
+ return text;
+ }
+ return new XSSFRichTextString(text).toString();
+ }
+
/**
* Do a check for, and output, comments in otherwise empty cells.
*/
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/SharedStringsTable.java
b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/SharedStringsTable.java
index 8eca6c710d..1b608eb74a 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/SharedStringsTable.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/SharedStringsTable.java
@@ -70,10 +70,20 @@ public class SharedStringsTable extends POIXMLDocumentPart
implements SharedStri
private final List<CTRst> strings = new ArrayList<>();
/**
- * Maps strings and their indexes in the <code>strings</code> arrays
+ * Maps the XML fragments of the rich strings and their indexes in the
<code>strings</code> arrays
*/
private final Map<String, Integer> stmap = new HashMap<>();
+ /**
+ * Maps plain strings and their indexes in the <code>strings</code>
arrays.
+ * <p>
+ * Entries that hold nothing but a piece of text (see {@link
#plainText(CTRst)}) can be keyed
+ * by that text, which saves serialising them to XML just to be able to
look them up. This map
+ * is kept separate from {@link #stmap} so that the raw text can never
collide with the XML
+ * fragment of a rich string.
+ */
+ private final Map<String, Integer> plainmap = new HashMap<>();
+
/**
* An integer representing the total count of strings in the workbook.
This count does not
* include any numbers, it counts only the total of text strings in the
workbook.
@@ -128,7 +138,12 @@ public class SharedStringsTable extends POIXMLDocumentPart
implements SharedStri
uniqueCount = Math.toIntExact(sst.getUniqueCount());
//noinspection deprecation
for (CTRst st : sst.getSiArray()) {
- stmap.put(xmlText(st), cnt);
+ String plain = plainText(st);
+ if (plain != null) {
+ plainmap.put(plain, cnt);
+ } else {
+ stmap.put(xmlText(st), cnt);
+ }
strings.add(st);
cnt++;
}
@@ -141,6 +156,32 @@ public class SharedStringsTable extends POIXMLDocumentPart
implements SharedStri
return st.xmlText(options);
}
+ /**
+ * Returns the text of the given entry if the entry can safely be
identified by its plain text
+ * alone, or <code>null</code> if it has to be identified by its
serialised XML fragment.
+ * <p>
+ * Only entries that consist of nothing but a piece of text qualify, ie no
formatting runs and
+ * no phonetic information. Text with leading or trailing whitespace is
excluded too, because
+ * for such entries the <code>xml:space="preserve"</code> attribute is
significant but is not
+ * part of the text itself.
+ *
+ * @param st the entry to check
+ * @return the text to key the entry by, or <code>null</code> if the XML
fragment has to be used
+ */
+ private static String plainText(CTRst st) {
+ if (!st.isSetT() || st.sizeOfRArray() != 0 || st.sizeOfRPhArray() != 0
|| st.isSetPhoneticPr()) {
+ return null;
+ }
+ String text = st.getT();
+ if (text == null || text.isEmpty()) {
+ return text;
+ }
+ if (Character.isWhitespace(text.charAt(0)) ||
Character.isWhitespace(text.charAt(text.length() - 1))) {
+ return null;
+ }
+ return text;
+ }
+
/**
* Return a string item by index
*
@@ -188,10 +229,14 @@ public class SharedStringsTable extends
POIXMLDocumentPart implements SharedStri
*/
@Internal
int addEntry(CTRst st) {
- String s = xmlText(st);
+ // plain strings can be keyed by their text, which avoids serialising
them to XML
+ String plain = plainText(st);
+ Map<String, Integer> map = plain != null ? plainmap : stmap;
+ String s = plain != null ? plain : xmlText(st);
count++;
- if (stmap.containsKey(s)) {
- return stmap.get(s);
+ Integer existing = map.get(s);
+ if (existing != null) {
+ return existing;
}
uniqueCount++;
@@ -199,7 +244,7 @@ public class SharedStringsTable extends POIXMLDocumentPart
implements SharedStri
CTRst newSt = _sstDoc.getSst().addNewSi();
newSt.set(st);
int idx = strings.size();
- stmap.put(s, idx);
+ map.put(s, idx);
strings.add(newSt);
return idx;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]