Browse Source

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
pull/159/head
Dominik Stadler 4 years ago
parent
commit
2c866cce02
1 changed files with 29 additions and 4 deletions
  1. 29
    4
      src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java

+ 29
- 4
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRow.java View File

@@ -20,7 +20,6 @@ package org.apache.poi.xssf.usermodel;
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 @@ public class XSSFRow implements Row, Comparable<XSSFRow> {
// _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 @@ public class XSSFRow implements Row, Comparable<XSSFRow> {
}

// remove any remaining illegal references in _rows.cArray
while(cArrayOrig.size() > _cells.size()) {
while(cArrayOrig.length > _cells.size()) {
_row.removeC(_cells.size());
}
}

Loading…
Cancel
Save