Skip to content

Apache Lucene open-source search software

LUCENE-2816: MMapDirectory speedups

Robert Muirb5726a4267c1
diff --git a/lucene/src/java/org/apache/lucene/store/MMapDirectory.java b/lucene/src/java/org/apache/lucene/store/MMapDirectory.java
index c0362a56164..ab0fc54f719 100644
--- a/lucene/src/java/org/apache/lucene/store/MMapDirectory.java
+++ b/lucene/src/java/org/apache/lucene/store/MMapDirectory.java
@@ -242,7 +242,34 @@ public class MMapDirectory extends FSDirectory {
throw new IOException("read past EOF");
}
}
+
+ @Override
+ public short readShort() throws IOException {
+ try {
+ return buffer.getShort();
+ } catch (BufferUnderflowException e) {
+ throw new IOException("read past EOF");
+ }
+ }
+ @Override
+ public int readInt() throws IOException {
+ try {
+ return buffer.getInt();
+ } catch (BufferUnderflowException e) {
+ throw new IOException("read past EOF");
+ }
+ }
+
+ @Override
+ public long readLong() throws IOException {
+ try {
+ return buffer.getLong();
+ } catch (BufferUnderflowException e) {
+ throw new IOException("read past EOF");
+ }
+ }
+
@Override
public long getFilePointer() {
return buffer.position();
@@ -294,7 +321,6 @@ public class MMapDirectory extends FSDirectory {
private final int maxBufSize;
private ByteBuffer curBuf; // redundant for speed: buffers[curBufIndex]
- private int curAvail; // redundant for speed: (bufSizes[curBufIndex] - curBuf.position())
private boolean isClone = false;
@@ -333,37 +359,66 @@ public class MMapDirectory extends FSDirectory {
@Override
public byte readByte() throws IOException {
- // Performance might be improved by reading ahead into an array of
- // e.g. 128 bytes and readByte() from there.
- if (curAvail == 0) {
+ try {
+ return curBuf.get();
+ } catch (BufferUnderflowException e) {
curBufIndex++;
if (curBufIndex >= buffers.length)
throw new IOException("read past EOF");
curBuf = buffers[curBufIndex];
curBuf.position(0);
- curAvail = bufSizes[curBufIndex];
+ return curBuf.get();
}
- curAvail--;
- return curBuf.get();
}
@Override
public void readBytes(byte[] b, int offset, int len) throws IOException {
- while (len > curAvail) {
- curBuf.get(b, offset, curAvail);
- len -= curAvail;
- offset += curAvail;
- curBufIndex++;
- if (curBufIndex >= buffers.length)
- throw new IOException("read past EOF");
- curBuf = buffers[curBufIndex];
- curBuf.position(0);
- curAvail = bufSizes[curBufIndex];
+ try {
+ curBuf.get(b, offset, len);
+ } catch (BufferUnderflowException e) {
+ int curAvail = curBuf.remaining();
+ while (len > curAvail) {
+ curBuf.get(b, offset, curAvail);
+ len -= curAvail;
+ offset += curAvail;
+ curBufIndex++;
+ if (curBufIndex >= buffers.length)
+ throw new IOException("read past EOF");
+ curBuf = buffers[curBufIndex];
+ curBuf.position(0);
+ curAvail = curBuf.remaining();
+ }
+ curBuf.get(b, offset, len);
}
- curBuf.get(b, offset, len);
- curAvail -= len;
}
+ @Override
+ public short readShort() throws IOException {
+ try {
+ return curBuf.getShort();
+ } catch (BufferUnderflowException e) {
+ return super.readShort();
+ }
+ }
+
+ @Override
+ public int readInt() throws IOException {
+ try {
+ return curBuf.getInt();
+ } catch (BufferUnderflowException e) {
+ return super.readInt();
+ }
+ }
+
+ @Override
+ public long readLong() throws IOException {
+ try {
+ return curBuf.getLong();
+ } catch (BufferUnderflowException e) {
+ return super.readLong();
+ }
+ }
+
@Override
public long getFilePointer() {
return ((long) curBufIndex * maxBufSize) + curBuf.position();
@@ -375,7 +430,6 @@ public class MMapDirectory extends FSDirectory {
curBuf = buffers[curBufIndex];
int bufOffset = (int) (pos - ((long) curBufIndex * maxBufSize));
curBuf.position(bufOffset);
- curAvail = bufSizes[curBufIndex] - bufOffset;
}
@Override