You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2021/11/17 22:41:01 UTC

svn commit: r1895125 - /poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java

Author: fanningpj
Date: Wed Nov 17 22:41:00 2021
New Revision: 1895125

URL: http://svn.apache.org/viewvc?rev=1895125&view=rev
Log:
try to improve performance when removing rows

Modified:
    poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java

Modified: poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java?rev=1895125&r1=1895124&r2=1895125&view=diff
==============================================================================
--- poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (original)
+++ poi/trunk/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java Wed Nov 17 22:41:00 2021
@@ -24,18 +24,7 @@ import static org.apache.poi.xssf.usermo
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.LinkedHashMap;
-import java.util.LinkedHashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.SortedMap;
-import java.util.TreeMap;
+import java.util.*;
 
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamException;
@@ -3034,13 +3023,15 @@ public class XSSFSheet extends POIXMLDoc
     }
 
     // remove all rows which will be overwritten
-    private void removeOverwritten(XSSFVMLDrawing vml, int startRow, int endRow, final int n){
+    private void removeOverwritten(XSSFVMLDrawing vml, int startRow, int endRow, final int n) {
+        HashSet<Integer> rowsToRemoveSet = new HashSet<>();
         for (Iterator<Row> it = rowIterator() ; it.hasNext() ; ) {
             XSSFRow row = (XSSFRow)it.next();
             int rownum = row.getRowNum();
 
             // check if we should remove this row as it will be overwritten by the data later
             if (shouldRemoveRow(startRow, endRow, n, rownum)) {
+                rowsToRemoveSet.add(rownum);
                 for (Cell c : row) {
                     if (!c.isPartOfArrayFormulaGroup()) {
                         //the support for deleting cells that are part of array formulas is not implemented yet
@@ -3056,35 +3047,35 @@ public class XSSFSheet extends POIXMLDoc
 
                 // remove row from _rows
                 it.remove();
+            }
+        }
 
-                // FIXME: (performance optimization) this should be moved outside the for-loop so that comments only needs to be iterated over once.
-                // also remove any comments associated with this row
-                if(sheetComments != null){
-                    CTCommentList lst = sheetComments.getCTComments().getCommentList();
-                    for (CTComment comment : lst.getCommentArray()) {
-                        String strRef = comment.getRef();
-                        CellAddress ref = new CellAddress(strRef);
-
-                        // is this comment part of the current row?
-                        if(ref.getRow() == rownum) {
-                            sheetComments.removeComment(ref);
-                            vml.removeCommentShape(ref.getRow(), ref.getColumn());
-                        }
-                    }
+        // also remove any comments associated with this row
+        if (sheetComments != null) {
+            CTCommentList lst = sheetComments.getCTComments().getCommentList();
+            for (CTComment comment : lst.getCommentArray()) {
+                String strRef = comment.getRef();
+                CellAddress ref = new CellAddress(strRef);
+
+                // is this comment part of the current row?
+                if(rowsToRemoveSet.contains(ref.getRow())) {
+                    sheetComments.removeComment(ref);
+                    vml.removeCommentShape(ref.getRow(), ref.getColumn());
                 }
-                // FIXME: (performance optimization) this should be moved outside the for-loop so that hyperlinks only needs to be iterated over once.
-                // also remove any hyperlinks associated with this row
-                if (hyperlinks != null) {
-                    for (XSSFHyperlink link : new ArrayList<>(hyperlinks)) {
-                        CellReference ref = new CellReference(link.getCellRef());
-                        if (ref.getRow() == rownum) {
-                            hyperlinks.remove(link);
-                        }
-                    }
+            }
+        }
+
+        // also remove any hyperlinks associated with this row
+        if (hyperlinks != null) {
+            for (XSSFHyperlink link : new ArrayList<>(hyperlinks)) {
+                CellReference ref = new CellReference(link.getCellRef());
+                if (rowsToRemoveSet.contains(ref.getRow())) {
+                    hyperlinks.remove(link);
                 }
             }
         }
 
+
     }
 
     private void shiftCommentsAndRows(XSSFVMLDrawing vml, int startRow, int endRow, final int n){



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org