optimize lookups by caching bytebuffer copy

This commit is contained in:
Xavier Léauté 2014-05-19 22:55:09 -07:00
parent be92c322e3
commit 7a351dfde3
5 changed files with 31 additions and 15 deletions

View File

@ -128,8 +128,9 @@ public class HyperUniquesSerde extends ComplexMetricSerde
@Override @Override
public HyperLogLogCollector fromByteBuffer(ByteBuffer buffer, int numBytes) public HyperLogLogCollector fromByteBuffer(ByteBuffer buffer, int numBytes)
{ {
buffer.limit(buffer.position() + numBytes); final ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
return HyperLogLogCollector.makeCollector(buffer); readOnlyBuffer.limit(readOnlyBuffer.position() + numBytes);
return HyperLogLogCollector.makeCollector(readOnlyBuffer);
} }
@Override @Override

View File

@ -123,8 +123,9 @@ public class ConciseCompressedIndexedInts implements IndexedInts, Comparable<Con
@Override @Override
public ImmutableConciseSet fromByteBuffer(ByteBuffer buffer, int numBytes) public ImmutableConciseSet fromByteBuffer(ByteBuffer buffer, int numBytes)
{ {
buffer.limit(buffer.position() + numBytes); final ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
return new ImmutableConciseSet(buffer.asReadOnlyBuffer()); readOnlyBuffer.limit(readOnlyBuffer.position() + numBytes);
return new ImmutableConciseSet(readOnlyBuffer);
} }
@Override @Override

View File

@ -154,6 +154,7 @@ public class GenericIndexed<T> implements Indexed<T>
private final int size; private final int size;
private final boolean cacheable; private final boolean cacheable;
private final ThreadLocal<ByteBuffer> cachedBuffer;
private final ThreadLocal<SizedLRUMap<Integer, T>> cachedValues = new ThreadLocal<SizedLRUMap<Integer, T>>() { private final ThreadLocal<SizedLRUMap<Integer, T>> cachedValues = new ThreadLocal<SizedLRUMap<Integer, T>>() {
@Override @Override
protected SizedLRUMap<Integer, T> initialValue() protected SizedLRUMap<Integer, T> initialValue()
@ -178,6 +179,15 @@ public class GenericIndexed<T> implements Indexed<T>
indexOffset = theBuffer.position(); indexOffset = theBuffer.position();
valuesOffset = theBuffer.position() + (size << 2); valuesOffset = theBuffer.position() + (size << 2);
this.cachedBuffer = new ThreadLocal<ByteBuffer>()
{
@Override
protected ByteBuffer initialValue()
{
return theBuffer.asReadOnlyBuffer();
}
};
this.cacheable = strategy instanceof CacheableObjectStrategy; this.cacheable = strategy instanceof CacheableObjectStrategy;
} }
@ -210,23 +220,24 @@ public class GenericIndexed<T> implements Indexed<T>
} }
} }
final ByteBuffer copyBuffer = this.cachedBuffer.get();
final int startOffset; final int startOffset;
final int endOffset; final int endOffset;
if (index == 0) { if (index == 0) {
startOffset = 4; startOffset = 4;
endOffset = theBuffer.getInt(indexOffset); endOffset = copyBuffer.getInt(indexOffset);
} else { } else {
final int position = indexOffset + ((index - 1) * 4); copyBuffer.position(indexOffset + ((index - 1) * 4));
startOffset = theBuffer.getInt(position) + 4; startOffset = copyBuffer.getInt() + 4;
endOffset = theBuffer.getInt(position + Ints.BYTES); endOffset = copyBuffer.getInt();
} }
if (startOffset == endOffset) { if (startOffset == endOffset) {
return null; return null;
} }
final ByteBuffer copyBuffer = this.theBuffer.asReadOnlyBuffer();
copyBuffer.position(valuesOffset + startOffset); copyBuffer.position(valuesOffset + startOffset);
final int size = endOffset - startOffset; final int size = endOffset - startOffset;
final T value = strategy.fromByteBuffer(copyBuffer, size); final T value = strategy.fromByteBuffer(copyBuffer, size);
@ -310,9 +321,9 @@ public class GenericIndexed<T> implements Indexed<T>
} }
@Override @Override
public String fromByteBuffer(ByteBuffer buffer, int numBytes) public String fromByteBuffer(final ByteBuffer buffer, final int numBytes)
{ {
byte[] bytes = new byte[numBytes]; final byte[] bytes = new byte[numBytes];
buffer.get(bytes); buffer.get(bytes);
return new String(bytes, Charsets.UTF_8); return new String(bytes, Charsets.UTF_8);
} }

View File

@ -81,8 +81,10 @@ public class IndexedRTree implements Comparable<IndexedRTree>
@Override @Override
public ImmutableRTree fromByteBuffer(ByteBuffer buffer, int numBytes) public ImmutableRTree fromByteBuffer(ByteBuffer buffer, int numBytes)
{ {
buffer.limit(buffer.position() + numBytes);
return new ImmutableRTree(buffer.asReadOnlyBuffer()); final ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
readOnlyBuffer.limit(readOnlyBuffer.position() + numBytes);
return new ImmutableRTree(readOnlyBuffer);
} }
@Override @Override

View File

@ -101,8 +101,9 @@ public class IntBufferIndexedInts implements IndexedInts, Comparable<IntBufferIn
@Override @Override
public IntBufferIndexedInts fromByteBuffer(ByteBuffer buffer, int numBytes) public IntBufferIndexedInts fromByteBuffer(ByteBuffer buffer, int numBytes)
{ {
buffer.limit(buffer.position() + numBytes); final ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
return new IntBufferIndexedInts(buffer); readOnlyBuffer.limit(readOnlyBuffer.position() + numBytes);
return new IntBufferIndexedInts(readOnlyBuffer);
} }
@Override @Override