garydgregory commented on code in PR #378:
URL: https://github.com/apache/commons-compress/pull/378#discussion_r1186894242


##########
src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java:
##########
@@ -371,9 +372,14 @@ public static void closeQuietly(final ZipFile zipFile) {
     private final List<ZipArchiveEntry> entries = new LinkedList<>();
 
     /**
-     * Maps String to list of ZipArchiveEntrys, name -> actual entries.
+     * Maps a string to the first entry named it.
      */
-    private final Map<String, LinkedList<ZipArchiveEntry>> nameMap = new 
HashMap<>(HASH_SIZE);
+    private final Map<String, ZipArchiveEntry> nameMap = new 
HashMap<>(HASH_SIZE);
+
+    /**
+     * If multiple entries have the same name, maps the name to entries named 
it.
+     */
+    private Map<String, List<ZipArchiveEntry>> duplicateNameMap = null;

Review Comment:
   Don't initialize to the default value.



##########
src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java:
##########
@@ -792,11 +798,24 @@ private BoundedArchiveInputStream 
createBoundedInputStream(final long start, fin
 
     private void fillNameMap() {
         entries.forEach(ze -> {
-            // entries is filled in populateFromCentralDirectory and
+            // entries are filled in populateFromCentralDirectory and
             // never modified
             final String name = ze.getName();
-            final LinkedList<ZipArchiveEntry> entriesOfThatName = 
nameMap.computeIfAbsent(name, k -> new LinkedList<>());
-            entriesOfThatName.addLast(ze);
+            ZipArchiveEntry firstEntry = nameMap.putIfAbsent(name, ze);
+
+            if (firstEntry != null) {
+                if (duplicateNameMap == null) {
+                    duplicateNameMap = new HashMap<>();
+                }
+
+                List<ZipArchiveEntry> entriesOfThatName = 
duplicateNameMap.computeIfAbsent(name, k -> {
+                    ArrayList<ZipArchiveEntry> list = new ArrayList<>(2);

Review Comment:
   Why 2?



##########
src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java:
##########
@@ -792,11 +798,24 @@ private BoundedArchiveInputStream 
createBoundedInputStream(final long start, fin
 
     private void fillNameMap() {
         entries.forEach(ze -> {
-            // entries is filled in populateFromCentralDirectory and
+            // entries are filled in populateFromCentralDirectory and
             // never modified
             final String name = ze.getName();
-            final LinkedList<ZipArchiveEntry> entriesOfThatName = 
nameMap.computeIfAbsent(name, k -> new LinkedList<>());
-            entriesOfThatName.addLast(ze);
+            ZipArchiveEntry firstEntry = nameMap.putIfAbsent(name, ze);

Review Comment:
   Use final.



##########
src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java:
##########
@@ -899,13 +921,17 @@ public Enumeration<ZipArchiveEntry> 
getEntriesInPhysicalOrder() {
      * @since 1.6
      */
     public Iterable<ZipArchiveEntry> getEntriesInPhysicalOrder(final String 
name) {
-        ZipArchiveEntry[] entriesOfThatName = ZipArchiveEntry.EMPTY_ARRAY;
-        final LinkedList<ZipArchiveEntry> linkedList = nameMap.get(name);
-        if (linkedList != null) {
-            entriesOfThatName = linkedList.toArray(entriesOfThatName);
-            Arrays.sort(entriesOfThatName, offsetComparator);
+        if (duplicateNameMap != null) {
+            List<ZipArchiveEntry> list = duplicateNameMap.get(name);
+            if (list != null) {
+                ZipArchiveEntry[] entriesOfThatName = 
list.toArray(ZipArchiveEntry.EMPTY_ARRAY);

Review Comment:
   Instead of: 
   
   1. allocate array
   2. sort array
   3. allocate list
    
   would it be better to change the type from `List` to `LinkedList` (which is 
what is used in the instance variable decl and:
   
   1. `clone()` the list
   2. `Collections.sort()` the cloned list
   
   ?



##########
src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java:
##########
@@ -868,9 +887,12 @@ public Enumeration<ZipArchiveEntry> getEntries() {
      * @since 1.6
      */
     public Iterable<ZipArchiveEntry> getEntries(final String name) {
-        final List<ZipArchiveEntry> entriesOfThatName = nameMap.get(name);
-        return entriesOfThatName != null ? entriesOfThatName
-            : Collections.emptyList();
+        List<ZipArchiveEntry> entriesOfThatName = duplicateNameMap == null ? 
null : duplicateNameMap.get(name);

Review Comment:
   Use final.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to