optimized buffer handling and runtime optimization.

The initial buffer size is set to 10 characters, so it doesn't have to be increased that often.
If the buffer has to grow, just copy the identical pattern and not the whole string.

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@170226 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Bernhard Messer 2005-05-15 15:04:39 +00:00
parent e8ba60ce87
commit c5600176d2
1 changed files with 9 additions and 7 deletions

View File

@ -241,22 +241,24 @@ class TermVectorsReader implements Cloneable {
int start = 0; int start = 0;
int deltaLength = 0; int deltaLength = 0;
int totalLength = 0; int totalLength = 0;
char [] buffer = {}; char [] buffer = new char[10]; // init the buffer with a length of 10 character
String previousString = ""; char[] previousBuffer = {};
for (int i = 0; i < numTerms; i++) { for (int i = 0; i < numTerms; i++) {
start = tvf.readVInt(); start = tvf.readVInt();
deltaLength = tvf.readVInt(); deltaLength = tvf.readVInt();
totalLength = start + deltaLength; totalLength = start + deltaLength;
if (buffer.length < totalLength) if (buffer.length < totalLength) { // increase buffer
{ buffer = null; // give a hint to garbage collector
buffer = new char[totalLength]; buffer = new char[totalLength];
for (int j = 0; j < previousString.length(); j++) // copy contents
buffer[j] = previousString.charAt(j); if (start > 0) // just copy if necessary
System.arraycopy(previousBuffer, 0, buffer, 0, start);
} }
tvf.readChars(buffer, start, deltaLength); tvf.readChars(buffer, start, deltaLength);
terms[i] = new String(buffer, 0, totalLength); terms[i] = new String(buffer, 0, totalLength);
previousString = terms[i]; previousBuffer = buffer;
int freq = tvf.readVInt(); int freq = tvf.readVInt();
termFreqs[i] = freq; termFreqs[i] = freq;