performance improvement

This commit is contained in:
Oliver Burn 2013-07-12 13:51:43 +10:00
parent 4e4a635fab
commit c6edc74a2c
2 changed files with 24 additions and 13 deletions

View File

@ -18,11 +18,13 @@
////////////////////////////////////////////////////////////////////////////////
package com.puppycrawl.tools.checkstyle.api;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
@ -30,6 +32,7 @@ import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.UnsupportedCharsetException;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.List;
@ -81,7 +84,7 @@ public final class FileText extends AbstractList<String>
/**
* The full text contents of the file.
*/
private final CharSequence mFullText;
private final String mFullText;
/**
* The lines of the file, without terminators.
@ -145,16 +148,20 @@ public final class FileText extends AbstractList<String>
// buf.trimToSize(); // could be used instead of toString().
mFullText = buf.toString();
final String[] lines = LINE_TERMINATOR.split(mFullText, -1);
if (lines.length > 0 && lines[lines.length - 1].length() == 0) {
// drop empty line after last newline
mLines = new String[lines.length - 1];
System.arraycopy(lines, 0, mLines, 0, lines.length - 1);
}
else {
// no newline at end, so we keep the last line as is
mLines = lines;
// Use the BufferedReader to break down the lines as this
// is about 30% faster than using the
// LINE_TERMINATOR.split(mFullText, -1) method
final ArrayList<String> lines = new ArrayList<String>();
final BufferedReader br =
new BufferedReader(new StringReader(mFullText));
for (;;) {
final String l = br.readLine();
if (null == l) {
break;
}
lines.add(l);
}
mLines = lines.toArray(new String[lines.size()]);
}
/**
@ -178,7 +185,7 @@ public final class FileText extends AbstractList<String>
mFile = aFile;
mCharset = null;
mFullText = buf;
mFullText = buf.toString();
mLines = aLines.toArray(new String[aLines.size()]);
}

View File

@ -14,7 +14,7 @@
<p>New features:</p>
<ul>
<li>
Support
Support
<a href="config.html#SuppressWarningsFilter">SuppressWarningsFilter</a>
to use <code>@SuppressWarnings</code> annotations to suppress
violations. Thanks to Trevor Robinson for patch #156.
@ -29,7 +29,11 @@
<p>Notes:</p>
<ul>
<li>
Upgraded dependencies to Guava 13.0.1 and JUnit 4.11.
Improved the performance of FileText construction, which
increased file loading times by up to 30%.
</li>
<li>
Upgraded dependencies to Guava 14.0.1 and JUnit 4.11.
</li>
</ul>
</section>