eolivelli commented on a change in pull request #118:
URL: https://github.com/apache/commons-io/pull/118#discussion_r462045891



##########
File path: src/main/java/org/apache/commons/io/IOUtils.java
##########
@@ -752,19 +777,46 @@ public static boolean contentEquals(final Reader input1, 
final Reader input2)
         if (input1 == null ^ input2 == null) {
             return false;
         }
-        final BufferedReader bufferedInput1 = toBufferedReader(input1);
-        final BufferedReader bufferedInput2 = toBufferedReader(input2);
 
-        int ch = bufferedInput1.read();
-        while (EOF != ch) {
-            final int ch2 = bufferedInput2.read();
-            if (ch != ch2) {
-                return false;
+        char[] charArray1 = new char[CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE];
+        char[] charArray2 = new char[CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE];
+        int nowPos1;
+        int nowPos2;
+        int nowRead1;
+        int nowRead2;
+        while (true) {
+            nowPos1 = 0;
+            nowPos2 = 0;
+            for (int nowCheck = 0; nowCheck < 
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE; nowCheck++) {
+                if (nowPos1 == nowCheck) {
+                    do {
+                        nowRead1 = input1.read(charArray1, nowPos1, 
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1);
+                    } while (nowRead1 == 0);
+                    if (nowRead1 == -1) {
+                        return nowPos2 == nowCheck && input2.read() == -1;
+                    }
+                    nowPos1 += nowRead1;
+                }
+                if (nowPos2 == nowCheck) {
+                    do {
+                        nowRead2 = input2.read(charArray2, nowPos2, 
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2);
+                    } while (nowRead2 == 0);
+                    if (nowRead2 == -1) {
+                        return nowPos1 == nowCheck && input1.read() == -1;
+                    }
+                    nowPos2 += nowRead2;
+                }
+                if (charArray1[nowCheck] != charArray2[nowCheck]) {
+                    return false;
+                }
             }
-            ch = bufferedInput1.read();
         }
+    }
 
-        return bufferedInput2.read() == EOF;
+    private enum LastState {
+        r,

Review comment:
       Can you please use UPPERCASE identifiers?
   Also, IMHO it is better to give meaningful names or add a minimal 
explanation 

##########
File path: 
src/test/java/org/apache/commons/io/performance/IOUtilsContentEqualsPerformanceTest.java
##########
@@ -0,0 +1,178 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.io.performance;
+
+import org.apache.commons.io.IOUtils;
+import org.openjdk.jmh.annotations.*;
+
+import java.io.*;
+import java.util.concurrent.TimeUnit;
+
+import static org.apache.commons.io.IOUtils.EOF;
+import static org.apache.commons.io.IOUtils.toBufferedReader;
+
+/**
+ * Test to show whether using BitSet for removeAll() methods is faster than 
using HashSet.
+ */
+@BenchmarkMode(Mode.AverageTime)
+@OutputTimeUnit(TimeUnit.NANOSECONDS)
+@State(Scope.Thread)
+public class IOUtilsContentEqualsPerformanceTest {
+
+    private static final String[] STRINGS = new String[3];
+
+    static {
+        STRINGS[0] = getString0();
+        STRINGS[1] = STRINGS[0] + 'c';
+        STRINGS[2] = STRINGS[0] + 'd';
+    }
+
+    private static final int LOOP = 10;
+
+    @Benchmark
+    public boolean[] testContentEqualsForFileNew() throws IOException {
+        boolean[] res = new boolean[2];
+        for (int i = 0; i < LOOP; i++) {
+            try (InputStream inputStream1 =
+                         
this.getClass().getResourceAsStream("/org/apache/commons/io/testfileBOM.xml");
+                 InputStream inputStream2 =
+                         
this.getClass().getResourceAsStream("/org/apache/commons/io/testfileNoBOM.xml");
+                 Reader inputReader1 = new InputStreamReader(inputStream1);
+                 Reader inputReader2 = new InputStreamReader(inputStream2);
+            ) {
+                res[0] = IOUtils.contentEquals(inputReader1, inputReader2);
+            }
+            try (InputStream inputStream1 =
+                         
this.getClass().getResourceAsStream("/org/apache/commons/io/testfileBOM.xml");
+                 InputStream inputStream2 =
+                         
this.getClass().getResourceAsStream("/org/apache/commons/io/testfileBOM.xml");
+                 Reader inputReader1 = new InputStreamReader(inputStream1);
+                 Reader inputReader2 = new InputStreamReader(inputStream2);
+            ) {
+                res[1] = IOUtils.contentEquals(inputReader1, inputReader2);
+            }
+        }
+        return res;
+    }
+
+    @Benchmark
+    public boolean[] testContentEqualsOld() throws IOException {
+        boolean[] res = new boolean[2];
+        for (int i = 0; i < LOOP; i++) {
+            try (InputStream inputStream1 =
+                         
this.getClass().getResourceAsStream("/org/apache/commons/io/testfileBOM.xml");
+                 InputStream inputStream2 =
+                         
this.getClass().getResourceAsStream("/org/apache/commons/io/testfileNoBOM.xml");
+                 Reader inputReader1 = new InputStreamReader(inputStream1);
+                 Reader inputReader2 = new InputStreamReader(inputStream2);
+            ) {
+                res[0] = contentEqualsOld(inputReader1, inputReader2);
+            }
+            try (InputStream inputStream1 =
+                         
this.getClass().getResourceAsStream("/org/apache/commons/io/testfileBOM.xml");
+                 InputStream inputStream2 =
+                         
this.getClass().getResourceAsStream("/org/apache/commons/io/testfileBOM.xml");
+                 Reader inputReader1 = new InputStreamReader(inputStream1);
+                 Reader inputReader2 = new InputStreamReader(inputStream2);
+            ) {
+                res[1] = contentEqualsOld(inputReader1, inputReader2);
+            }
+        }
+        return res;
+    }
+
+    @Benchmark
+    public boolean[] testContentEqualsNew2() throws IOException {
+        boolean[] res = new boolean[9];
+        for (int i = 0; i < 3; i++) {
+            for (int j = 0; j < 3; j++) {
+                try (Reader inputReader1 = new StringReader(STRINGS[i]);
+                     Reader inputReader2 = new StringReader(STRINGS[j]);
+                ) {
+                    res[i * 3 + j] = IOUtils.contentEquals(inputReader1, 
inputReader2);
+                }
+            }
+        }
+        return res;
+    }
+
+    @Benchmark
+    public boolean[] testContentEqualsOld2() throws IOException {
+        boolean[] res = new boolean[9];
+        for (int i = 0; i < 3; i++) {
+            for (int j = 0; j < 3; j++) {
+                try (Reader inputReader1 = new StringReader(STRINGS[i]);
+                     Reader inputReader2 = new StringReader(STRINGS[j]);
+                ) {
+                    res[i * 3 + j] = contentEqualsOld(inputReader1, 
inputReader2);
+                }
+            }
+        }
+        return res;
+    }
+
+    /**
+     * Old version of IOUtils.contentEquals(Reader, Reader)
+     *
+     * Compares the contents of two Readers to determine if they are equal or
+     * not.
+     * <p>
+     * This method buffers the input internally using
+     * <code>BufferedReader</code> if they are not already buffered.
+     * </p>
+     *
+     * @param input1 the first reader
+     * @param input2 the second reader
+     * @return true if the content of the readers are equal or they both don't
+     * exist, false otherwise
+     * @throws NullPointerException if either input is null
+     * @throws IOException          if an I/O error occurs
+     * @since 1.1
+     */
+    @SuppressWarnings("resource")
+    public static boolean contentEqualsOld(final Reader input1, final Reader 
input2)
+            throws IOException {
+        if (input1 == input2) {
+            return true;
+        }
+        if (input1 == null ^ input2 == null) {
+            return false;
+        }
+        final BufferedReader bufferedInput1 = toBufferedReader(input1);
+        final BufferedReader bufferedInput2 = toBufferedReader(input2);
+
+        int ch = bufferedInput1.read();
+        while (EOF != ch) {
+            final int ch2 = bufferedInput2.read();
+            if (ch != ch2) {
+                return false;
+            }
+            ch = bufferedInput1.read();
+        }
+
+        return bufferedInput2.read() == EOF;
+    }
+
+    public static String getString0() {
+        StringBuilder stringBuilder = new StringBuilder("ab");
+        for (int i = 0; i < 24; i++) {

Review comment:
       Did you test with larger strings?
   24 looks like a small string 




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to