From c5600176d299dab4f395037e62cb4b0c365f8210 Mon Sep 17 00:00:00 2001 From: Bernhard Messer Date: Sun, 15 May 2005 15:04:39 +0000 Subject: [PATCH] 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 --- .../apache/lucene/index/TermVectorsReader.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/java/org/apache/lucene/index/TermVectorsReader.java b/src/java/org/apache/lucene/index/TermVectorsReader.java index 6cfc57eca7c..dc7005e6dc8 100644 --- a/src/java/org/apache/lucene/index/TermVectorsReader.java +++ b/src/java/org/apache/lucene/index/TermVectorsReader.java @@ -241,22 +241,24 @@ class TermVectorsReader implements Cloneable { int start = 0; int deltaLength = 0; int totalLength = 0; - char [] buffer = {}; - String previousString = ""; + char [] buffer = new char[10]; // init the buffer with a length of 10 character + char[] previousBuffer = {}; for (int i = 0; i < numTerms; i++) { start = tvf.readVInt(); deltaLength = tvf.readVInt(); 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]; - 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); terms[i] = new String(buffer, 0, totalLength); - previousString = terms[i]; + previousBuffer = buffer; int freq = tvf.readVInt(); termFreqs[i] = freq;