[
https://issues.apache.org/jira/browse/IO-670?focusedWorklogId=464156&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-464156
]
ASF GitHub Bot logged work on IO-670:
-------------------------------------
Author: ASF GitHub Bot
Created on: 29/Jul/20 17:41
Start Date: 29/Jul/20 17:41
Worklog Time Spent: 10m
Work Description: melloware commented on a change in pull request #118:
URL: https://github.com/apache/commons-io/pull/118#discussion_r462222165
##########
File path: src/main/java/org/apache/commons/io/IOUtils.java
##########
@@ -790,16 +842,342 @@ public static boolean contentEqualsIgnoreEOL(final
Reader input1, final Reader i
if (input1 == null ^ input2 == null) {
return false;
}
- final BufferedReader br1 = toBufferedReader(input1);
- final BufferedReader br2 = toBufferedReader(input2);
- String line1 = br1.readLine();
- String line2 = br2.readLine();
- while (line1 != null && line1.equals(line2)) {
- line1 = br1.readLine();
- line2 = br2.readLine();
+ char[] charArray1 = new char[CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE];
+ char[] charArray2 = new char[CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE];
+ int nowPos1 = 0;
+ int nowPos2 = 0;
+ int nowRead1;
+ int nowRead2;
+ int nowCheck1 = 0;
+ int nowCheck2 = 0;
+ boolean readEnd1 = false;
+ boolean readEnd2 = false;
+ LastState lastState1 = LastState.newLine;
+ LastState lastState2 = LastState.newLine;
+ while (true) {
+ if (nowPos1 == nowCheck1) {
+ if (nowCheck1 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
+ nowPos1 = nowCheck1 = 0;
+ }
+ do {
+ nowRead1 = input1.read(charArray1, nowPos1,
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1);
+ } while (nowRead1 == 0);
+ if (nowRead1 == -1) {
+ readEnd1 = true;
+ } else {
+ nowPos1 += nowRead1;
+ }
+ }
+ if (nowPos2 == nowCheck2) {
+ if (nowCheck2 == CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
+ nowPos2 = nowCheck2 = 0;
+ }
+ do {
+ nowRead2 = input2.read(charArray2, nowPos2,
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2);
+ } while (nowRead2 == 0);
+ if (nowRead2 == -1) {
+ readEnd2 = true;
+ } else {
+ nowPos2 += nowRead2;
+ }
+ }
+ if (readEnd1) {
+ if (readEnd2) {
+ return true;
+ } else {
+ switch (lastState1) {
+ case r:
+ case newLine:
+ switch (lastState2) {
+ case r:
+ if (charArray2[nowCheck2] == '\n') {
+ nowCheck2++;
+ if (nowPos2 == nowCheck2) {
+ if (nowCheck2 ==
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
+ nowPos2 = nowCheck2 = 0;
+ }
+ do {
+ nowRead2 =
input2.read(charArray2, nowPos2,
+
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2);
+ } while (nowRead2 == 0);
+ if (nowRead2 == -1) {
+ readEnd2 = true;
+ } else {
+ nowPos2 += nowRead2;
+ }
+ }
+ return readEnd2;
+ }
+ return false;
+ default:
+ return false;
+ }
+ case normal:
+ switch (lastState2) {
+ case normal:
+ switch (charArray2[nowCheck2]) {
+ case '\r':
+ nowCheck2++;
+ if (nowPos2 == nowCheck2) {
+ if (nowCheck2 ==
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
+ nowPos2 = nowCheck2 = 0;
+ }
+ do {
+ nowRead2 =
input2.read(charArray2, nowPos2,
+
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2);
+ } while (nowRead2 == 0);
+ if (nowRead2 == -1) {
+ readEnd2 = true;
+ } else {
+ nowPos2 += nowRead2;
+ }
+ }
+ if (readEnd2) {
+ return true;
+ } else if (charArray2[nowCheck2]
== '\n') {
+ nowCheck2++;
+ if (nowPos2 == nowCheck2) {
+ if (nowCheck2 ==
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
+ nowPos2 = nowCheck2 =
0;
+ }
+ do {
+ nowRead2 =
input2.read(charArray2, nowPos2,
+
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2);
+ } while (nowRead2 == 0);
+ if (nowRead2 == -1) {
+ readEnd2 = true;
+ } else {
+ nowPos2 += nowRead2;
+ }
+ }
+ return readEnd2;
+ }
+ return false;
+ case '\n':
+ nowCheck2++;
+ if (nowPos2 == nowCheck2) {
+ if (nowCheck2 ==
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
+ nowPos2 = nowCheck2 = 0;
+ }
+ do {
+ nowRead2 =
input2.read(charArray2, nowPos2,
+
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2);
+ } while (nowRead2 == 0);
+ if (nowRead2 == -1) {
+ readEnd2 = true;
+ } else {
+ nowPos2 += nowRead2;
+ }
+ }
+ return readEnd2;
+ default:
+ return false;
+ }
+ default:
+ return false;
+ }
+ default:
+ //shall never enter
+ }
+ }
+ } else if (readEnd2) {
+ switch (lastState2) {
+ case r:
+ case newLine:
+ switch (lastState1) {
+ case r:
+ if (charArray1[nowCheck1] == '\n') {
+ nowCheck1++;
+ if (nowPos1 == nowCheck1) {
+ if (nowCheck1 ==
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
+ nowPos1 = nowCheck1 = 0;
+ }
+ do {
+ nowRead1 = input1.read(charArray1,
nowPos1,
+
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1);
+ } while (nowRead1 == 0);
+ if (nowRead1 == -1) {
+ readEnd1 = true;
+ } else {
+ nowPos1 += nowRead1;
+ }
+ }
+ return readEnd1;
+ }
+ return false;
+ default:
+ return false;
+ }
+ case normal:
+ switch (lastState1) {
+ case normal:
+ switch (charArray1[nowCheck1]) {
+ case '\r':
+ nowCheck1++;
+ if (nowPos1 == nowCheck1) {
+ if (nowCheck1 ==
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
+ nowPos1 = nowCheck1 = 0;
+ }
+ do {
+ nowRead1 =
input1.read(charArray1, nowPos1,
+
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1);
+ } while (nowRead1 == 0);
+ if (nowRead1 == -1) {
+ readEnd1 = true;
+ } else {
+ nowPos1 += nowRead1;
+ }
+ }
+ if (readEnd1) {
+ return true;
+ } else if (charArray1[nowCheck1] ==
'\n') {
+ nowCheck1++;
+ if (nowPos1 == nowCheck1) {
+ if (nowCheck1 ==
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
+ nowPos1 = nowCheck1 = 0;
+ }
+ do {
+ nowRead1 =
input1.read(charArray1, nowPos1,
+
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1);
+ } while (nowRead1 == 0);
+ if (nowRead1 == -1) {
+ readEnd1 = true;
+ } else {
+ nowPos1 += nowRead1;
+ }
+ }
+ return readEnd1;
+ }
+ return false;
+ case '\n':
+ nowCheck1++;
+ if (nowPos1 == nowCheck1) {
+ if (nowCheck1 ==
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
+ nowPos1 = nowCheck1 = 0;
+ }
+ do {
+ nowRead1 =
input1.read(charArray1, nowPos1,
+
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1);
+ } while (nowRead1 == 0);
+ if (nowRead1 == -1) {
+ readEnd1 = true;
+ } else {
+ nowPos1 += nowRead1;
+ }
+ }
+ return readEnd1;
+ default:
+ return false;
+ }
+ default:
+ return false;
+ }
+ default:
+ //shall never enter
+ }
+ }
+
+ switch (charArray1[nowCheck1]) {
+ case '\r':
+ switch (charArray2[nowCheck2]) {
+ case '\r':
+ lastState1 = lastState2 = LastState.r;
+ nowCheck1++;
+ nowCheck2++;
+ continue;
+ case '\n':
+ nowCheck1++;
+ if (nowPos1 == nowCheck1) {
+ if (nowCheck1 ==
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
+ nowPos1 = nowCheck1 = 0;
+ }
+ do {
+ nowRead1 = input1.read(charArray1, nowPos1,
+CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos1);
+ } while (nowRead1 == 0);
+ if (nowRead1 == -1) {
+ readEnd1 = true;
+ } else {
+ nowPos1 += nowRead1;
+ }
+ }
+ lastState1 = lastState2 = LastState.newLine;
+ nowCheck2++;
+ if (readEnd1) {
+ continue;
+ }
+ if (charArray1[nowCheck1] == '\n') {
+ nowCheck1++;
+ }
+ continue;
+ default:
+ return false;
+ }
+ case '\n':
+ switch (charArray2[nowCheck2]) {
+ case '\n':
+ lastState1 = lastState2 = LastState.newLine;
+ nowCheck1++;
+ nowCheck2++;
+ continue;
+ case '\r':
+ nowCheck2++;
+ if (nowPos2 == nowCheck2) {
+ if (nowCheck2 ==
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE) {
+ nowPos2 = nowCheck2 = 0;
+ }
+ do {
+ nowRead2 = input2.read(charArray2, nowPos2,
+
CONTENT_EQUALS_CHAR_ARRAY_BUFFER_SIZE - nowPos2);
+ } while (nowRead2 == 0);
+ if (nowRead2 == -1) {
+ readEnd2 = true;
+ } else {
+ nowPos2 += nowRead2;
+ }
+ }
+ lastState1 = lastState2 = LastState.newLine;
+ nowCheck1++;
+ if (readEnd2) {
+ continue;
+ }
+ if (charArray2[nowCheck2] == '\n') {
+ nowCheck2++;
+ }
+ continue;
+ default:
+ if (lastState1 == LastState.r) {
+ lastState1 = LastState.newLine;
+ nowCheck1++;
+ continue;
+ } else {
+ return false;
+ }
+ }
+ default:
+ switch (charArray2[nowCheck2]) {
+ case '\n':
+ if (lastState2 == LastState.r) {
+ lastState2 = LastState.newLine;
+ nowCheck2++;
+ continue;
+ } else {
+ return false;
+ }
+ case '\r':
+ return false;
+ default:
+ if (charArray1[nowCheck1] !=
charArray2[nowCheck2]) {
+ return false;
+ }
+ lastState1 = lastState2 = LastState.normal;
+ nowCheck1++;
+ nowCheck2++;
+ continue;
+ }
+ }
}
Review comment:
I agree with Gary on this one. In some cases performance gains do not
outweigh complexity of the code.
----------------------------------------------------------------
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:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 464156)
Time Spent: 4h 50m (was: 4h 40m)
> IOUtils.contentEquals is of low performance. I will refine it.
> --------------------------------------------------------------
>
> Key: IO-670
> URL: https://issues.apache.org/jira/browse/IO-670
> Project: Commons IO
> Issue Type: Improvement
> Reporter: Jin Xu
> Priority: Critical
> Attachments: jmh-result.org.apache.json
>
> Time Spent: 4h 50m
> Remaining Estimate: 0h
>
> [https://github.com/apache/commons-io/pull/118]
--
This message was sent by Atlassian Jira
(v8.3.4#803005)
