From a3298f9c1644baeb9e9d551974492d33112918d3 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Thu, 15 Aug 2019 07:55:06 +0000 Subject: [PATCH] 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 --- .../apache/poi/xssf/usermodel/XSSFRow.java | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) 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 5a6133ab67..cc1ae4ab8b 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 @@ 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 { // _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 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 cArrayOrig = _row.getCList(); - CTCell[] cArrayCopy = new CTCell[cArrayOrig.size()]; + CTCell[] cArrayCopy = new CTCell[cArrayOrig.length]; IdentityHashMap map = new IdentityHashMap<>(_cells.size()); int i = 0; for (CTCell ctCell : cArrayOrig) { @@ -583,7 +608,7 @@ public class XSSFRow implements Row, Comparable { } // remove any remaining illegal references in _rows.cArray - while(cArrayOrig.size() > _cells.size()) { + while(cArrayOrig.length > _cells.size()) { _row.removeC(_cells.size()); } } -- 2.39.5