Bug 63657: Optimize onDocumentWrite() to not do the full re-assignment
if not necessary at all, also use getCArray() instead of getCList() as
access-operations are much quicker this way
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1865209 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java
index 5a6133a..cc1ae4a 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java
@@ -20,7 +20,6 @@
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
-import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.TreeMap;
@@ -557,9 +556,35 @@
// _row.cArray and _cells.getCTCell might be out of sync after adding/removing cells,
// thus we need to re-order it here to make the resulting file correct
+ // do a quick check if there is work to do to not incur the overhead if not necessary anyway
+ CTCell[] cArrayOrig = _row.getCArray();
+ if(cArrayOrig.length == _cells.size()) {
+ boolean allEqual = true;
+ Iterator<XSSFCell> it = _cells.values().iterator();
+ for (CTCell ctCell : cArrayOrig) {
+ XSSFCell cell = it.next();
+
+ // we want to compare on identity here on purpose
+ // as we want to ensure that both lists contain the
+ // same documents, not copies!
+ if (ctCell != cell.getCTCell()) {
+ allEqual = false;
+ break;
+ }
+ }
+
+ // we did not find any difference, so we can skip the work
+ if(allEqual) {
+ return;
+ }
+ }
+
+ fixupCTCells(cArrayOrig);
+ }
+
+ private void fixupCTCells(CTCell[] cArrayOrig) {
// copy all values to 2nd array and a map for lookup of index
- List<CTCell> cArrayOrig = _row.getCList();
- CTCell[] cArrayCopy = new CTCell[cArrayOrig.size()];
+ CTCell[] cArrayCopy = new CTCell[cArrayOrig.length];
IdentityHashMap<CTCell, Integer> map = new IdentityHashMap<>(_cells.size());
int i = 0;
for (CTCell ctCell : cArrayOrig) {
@@ -583,7 +608,7 @@
}
// remove any remaining illegal references in _rows.cArray
- while(cArrayOrig.size() > _cells.size()) {
+ while(cArrayOrig.length > _cells.size()) {
_row.removeC(_cells.size());
}
}