Skip to content

Instantly share code, notes, and snippets.

@matriv
Last active August 9, 2016 17:49
Show Gist options
  • Save matriv/19c32fa76c7b8009c8cf119ba4f1a282 to your computer and use it in GitHub Desktop.
Save matriv/19c32fa76c7b8009c8cf119ba4f1a282 to your computer and use it in GitHub Desktop.
package org.sample;
import org.apache.lucene.util.LongBitSet;
import org.elasticsearch.common.logging.ESLogger;
import org.elasticsearch.common.logging.Loggers;
import org.openjdk.jmh.annotations.Benchmark;
import java.util.ArrayList;
public class MyBenchmark {
private final static ESLogger LOGGER = Loggers.getLogger(MyBenchmark.class);
@Benchmark
public void testLuceneBitSetSmall() {
LuceneBitSetWrapper lbsw = new LuceneBitSetWrapper();
for (long i = 0; i < 1_000_000L; i++) {
lbsw.set(i);
}
}
@Benchmark
public void testLuceneBitSetLarge() {
LuceneBitSetWrapper lbsw = new LuceneBitSetWrapper();
for (long i = 0; i < 10_000_000_000L; i++) {
lbsw.set(i);
}
}
@Benchmark
public void testCustomBitSetSmall() {
BitSet bitSet = new BitSet();
for (long i = 0; i < 1_000_000L; i++) {
bitSet.set(i);
}
}
@Benchmark
public void testCustomBitSetWrapperLarge() {
BitSet bitSet = new BitSet();
for (long i = 0; i < 10_000_000_000L; i++) {
bitSet.set(i);
}
}
private static class LuceneBitSetWrapper {
long size = 1024 * 10;
LongBitSet bitSet = new LongBitSet(size);
void set(long idx) {
if (idx >= size) {
size *= 2;
bitSet = LongBitSet.ensureCapacity(bitSet, size);
}
bitSet.set(idx);
}
boolean get(long idx) {
return bitSet.get(idx);
}
}
private static class BitSet {
private static long SEGMENT_SIZE = 1048576;
private static int INITIAL_NO_OF_SEGMENTS = 10;
private ArrayList<LongBitSet> segments;
private int noOfSegments;
BitSet() {
noOfSegments = INITIAL_NO_OF_SEGMENTS;
segments = new ArrayList<>(noOfSegments);
for (int i = 0; i < noOfSegments; i++) {
segments.add(new LongBitSet(SEGMENT_SIZE));
}
}
void set(long idx) {
int segmentIdx = segmentIdx(idx);
ensureCapacity(segmentIdx);
int idxInSegment = idxInSegment(idx);
segments.get(segmentIdx).set(idxInSegment);
}
boolean get(long idx) {
int segmentIdx = segmentIdx(idx);
ensureCapacity(segmentIdx);
int idxInSegment = idxInSegment(idx);
return segments.get(segmentIdx).get(idxInSegment);
}
private int segmentIdx(long idx) {
return (int) (idx / INITIAL_NO_OF_SEGMENTS);
}
private int idxInSegment(long idx) {
return (int) (idx % INITIAL_NO_OF_SEGMENTS);
}
private void ensureCapacity(int segment) {
if (segment >= noOfSegments) {
segments.add(new LongBitSet(SEGMENT_SIZE));
noOfSegments++;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment