lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r379532 - in /lucene/java/trunk: CHANGES.txt src/java/org/apache/lucene/store/BufferedIndexOutput.java src/java/org/apache/lucene/store/RAMOutputStream.java src/test/org/apache/lucene/StoreTest.java
Date Tue, 21 Feb 2006 17:00:55 GMT
Author: cutting
Date: Tue Feb 21 09:00:40 2006
New Revision: 379532

URL: http://svn.apache.org/viewcvs?rev=379532&view=rev
Log:
Fix for LUCENE-435: Optimize BufferedIndexOutput.writeBytes().  Contributed by Lukas Zapletal.

Modified:
    lucene/java/trunk/CHANGES.txt
    lucene/java/trunk/src/java/org/apache/lucene/store/BufferedIndexOutput.java
    lucene/java/trunk/src/java/org/apache/lucene/store/RAMOutputStream.java
    lucene/java/trunk/src/test/org/apache/lucene/StoreTest.java

Modified: lucene/java/trunk/CHANGES.txt
URL: http://svn.apache.org/viewcvs/lucene/java/trunk/CHANGES.txt?rev=379532&r1=379531&r2=379532&view=diff
==============================================================================
--- lucene/java/trunk/CHANGES.txt (original)
+++ lucene/java/trunk/CHANGES.txt Tue Feb 21 09:00:40 2006
@@ -2,6 +2,15 @@
 
 $Id$
 
+1.9 RC2
+
+Optimizations
+     
+ 1. Optimized BufferedIndexOutput.writeBytes() to use
+    System.arraycopy() in more cases, rather than copying byte-by-byte.
+    (Lukas Zapletal via Cutting)
+
+
 1.9 RC1
 
 Note that this realease is mostly but not 100% source compatible with the

Modified: lucene/java/trunk/src/java/org/apache/lucene/store/BufferedIndexOutput.java
URL: http://svn.apache.org/viewcvs/lucene/java/trunk/src/java/org/apache/lucene/store/BufferedIndexOutput.java?rev=379532&r1=379531&r2=379532&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/store/BufferedIndexOutput.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/store/BufferedIndexOutput.java Tue Feb 21
09:00:40 2006
@@ -41,8 +41,41 @@
    * @see IndexInput#readBytes(byte[],int,int)
    */
   public void writeBytes(byte[] b, int length) throws IOException {
-    for (int i = 0; i < length; i++)
-      writeByte(b[i]);
+    int bytesLeft = BUFFER_SIZE - bufferPosition;
+    // is there enough space in the buffer?
+    if (bytesLeft >= length) {
+      // we add the data to the end of the buffer
+      System.arraycopy(b, 0, buffer, bufferPosition, length);
+      bufferPosition += length;
+      // if the buffer is full, flush it
+      if (BUFFER_SIZE - bufferPosition == 0)
+        flush();
+    } else {
+      // is data larger then buffer?
+      if (length > BUFFER_SIZE) {
+        // we flush the buffer
+        if (bufferPosition > 0)
+          flush();
+        // and write data at once
+        flushBuffer(b, length);
+      } else {
+        // we fill/flush the buffer (until the input is written)
+        int pos = 0; // position in the input data
+        int pieceLength;
+        while (pos < length) {
+          pieceLength = (length - pos < bytesLeft) ? length - pos : bytesLeft;
+          System.arraycopy(b, pos, buffer, bufferPosition, pieceLength);
+          pos += pieceLength;
+          bufferPosition += pieceLength;
+          // if the buffer is full, flush it
+          bytesLeft = BUFFER_SIZE - bufferPosition;
+          if (bytesLeft == 0) {
+            flush();
+            bytesLeft = BUFFER_SIZE;
+          }
+        }
+      }
+    }
   }
 
   /** Forces any buffered output to be written. */

Modified: lucene/java/trunk/src/java/org/apache/lucene/store/RAMOutputStream.java
URL: http://svn.apache.org/viewcvs/lucene/java/trunk/src/java/org/apache/lucene/store/RAMOutputStream.java?rev=379532&r1=379531&r2=379532&view=diff
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/store/RAMOutputStream.java (original)
+++ lucene/java/trunk/src/java/org/apache/lucene/store/RAMOutputStream.java Tue Feb 21 09:00:40
2006
@@ -66,27 +66,27 @@
   }
 
   public void flushBuffer(byte[] src, int len) {
-    int bufferNumber = pointer/BUFFER_SIZE;
-    int bufferOffset = pointer%BUFFER_SIZE;
-    int bytesInBuffer = BUFFER_SIZE - bufferOffset;
-    int bytesToCopy = bytesInBuffer >= len ? len : bytesInBuffer;
+    byte[] buffer;
+    int bufferPos = 0;
+    while (bufferPos != len) {
+      int bufferNumber = pointer/BUFFER_SIZE;
+      int bufferOffset = pointer%BUFFER_SIZE;
+      int bytesInBuffer = BUFFER_SIZE - bufferOffset;
+      int remainInSrcBuffer = len - bufferPos;
+      int bytesToCopy = bytesInBuffer >= remainInSrcBuffer ? remainInSrcBuffer : bytesInBuffer;
 
-    if (bufferNumber == file.buffers.size())
-      file.buffers.addElement(new byte[BUFFER_SIZE]);
+      if (bufferNumber == file.buffers.size()) {
+        buffer = new byte[BUFFER_SIZE];
+        file.buffers.addElement(buffer);
+      } else {
+        buffer = (byte[]) file.buffers.elementAt(bufferNumber);
+      }
 
-    byte[] buffer = (byte[])file.buffers.elementAt(bufferNumber);
-    System.arraycopy(src, 0, buffer, bufferOffset, bytesToCopy);
-
-    if (bytesToCopy < len) {			  // not all in one buffer
-      int srcOffset = bytesToCopy;
-      bytesToCopy = len - bytesToCopy;		  // remaining bytes
-      bufferNumber++;
-      if (bufferNumber == file.buffers.size())
-        file.buffers.addElement(new byte[BUFFER_SIZE]);
-      buffer = (byte[])file.buffers.elementAt(bufferNumber);
-      System.arraycopy(src, srcOffset, buffer, 0, bytesToCopy);
+      System.arraycopy(src, bufferPos, buffer, bufferOffset, bytesToCopy);
+      bufferPos += bytesToCopy;
+      pointer += bytesToCopy;
     }
-    pointer += len;
+
     if (pointer > file.length)
       file.length = pointer;
 

Modified: lucene/java/trunk/src/test/org/apache/lucene/StoreTest.java
URL: http://svn.apache.org/viewcvs/lucene/java/trunk/src/test/org/apache/lucene/StoreTest.java?rev=379532&r1=379531&r2=379532&view=diff
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/StoreTest.java (original)
+++ lucene/java/trunk/src/test/org/apache/lucene/StoreTest.java Tue Feb 21 09:00:40 2006
@@ -28,14 +28,13 @@
 class StoreTest {
   public static void main(String[] args) {
     try {
-      test(1000, true);
+      test(1000, true, true);
     } catch (Exception e) {
-      System.out.println(" caught a " + e.getClass() +
-			 "\n with message: " + e.getMessage());
+		e.printStackTrace();
     }
   }
 
-  public static void test(int count, boolean ram)
+  public static void test(int count, boolean ram, boolean buffered)
        throws Exception {
     Random gen = new Random(1251971);
     int i;
@@ -51,6 +50,8 @@
 
     final int LENGTH_MASK = 0xFFF;
 
+	final byte[] buffer = new byte[LENGTH_MASK];
+
     for (i = 0; i < count; i++) {
       String name = i + ".dat";
       int length = gen.nextInt() & LENGTH_MASK;
@@ -59,8 +60,14 @@
 
       IndexOutput file = store.createOutput(name);
 
-      for (int j = 0; j < length; j++)
-	file.writeByte(b);
+      if (buffered) {
+        for (int j = 0; j < length; j++)
+          buffer[j] = b;
+        file.writeBytes(buffer, length);
+      } else {
+        for (int j = 0; j < length; j++)
+          file.writeByte(b);
+      }
       
       file.close();
     }
@@ -89,9 +96,18 @@
       if (file.length() != length)
 	throw new Exception("length incorrect");
 
-      for (int j = 0; j < length; j++)
-	if (file.readByte() != b)
-	  throw new Exception("contents incorrect");
+      byte[] content = new byte[length];
+      if (buffered) {
+        file.readBytes(content, 0, length);
+        // check the buffer
+        for (int j = 0; j < length; j++)
+          if (content[j] != b)
+            throw new Exception("contents incorrect");
+      } else {
+        for (int j = 0; j < length; j++)
+          if (file.readByte() != b)
+            throw new Exception("contents incorrect");
+      }
 
       file.close();
     }



Mime
View raw message