Skip to content

Commit

Permalink
GH-528 experimental caching
Browse files Browse the repository at this point in the history
  • Loading branch information
hmottestad committed Nov 27, 2024
1 parent 1259d23 commit f7e8e5e
Show file tree
Hide file tree
Showing 14 changed files with 141 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,9 @@ public void updateIndex() {
*/
@Override
public boolean access(long bitIndex) {
if (bitIndex < 0)
if (bitIndex < 0) {
throw new IndexOutOfBoundsException("bitIndex < 0: " + bitIndex);
}

long wordIndex = wordIndex(bitIndex);
if (wordIndex >= words.length()) {
Expand Down Expand Up @@ -324,15 +325,14 @@ public long select1(long x) {
return 0;
}
// Search superblock (binary Search)
long superBlockIndex = binarySearch(superBlocks, x);
long superBlockIndex = binarySearchNew(superBlocks, x);

// If there is a run of many zeros, two correlative superblocks may have
// the same value,
// We need to position at the first of them.

while (superBlockIndex > 0 && (superBlocks.get(superBlockIndex) >= x)) {
superBlockIndex--;

}

long countdown = x - superBlocks.get(superBlockIndex);
Expand Down Expand Up @@ -444,6 +444,7 @@ public static long binarySearch0(LongArray arr, long fromIndex, long toIndex, lo
* @param val val
* @return index
*/

public static long binarySearch(LongArray arr, long val) {
long min = 0, max = arr.length(), mid;

Expand All @@ -460,11 +461,67 @@ public static long binarySearch(LongArray arr, long val) {
return min;
}

public static long binarySearchNew(LongArray arr, long val) {
long min = 0;
long max = arr.length();
long mid;

long[] prevFound = arr.getPrevFound();

int index = (int) (val / 65536 + 1);

if (index > prevFound.length) {
throw new IllegalArgumentException("Index out of bounds: " + index);
}

if (index + 1 < prevFound.length) {
long t = prevFound[index + 1];
if (t > 0) {
max = Math.min(max, t);
}
}

if (index - 1 >= 0) {
long t = prevFound[index - 1];
if (t > 0) {
min = t;
}
}

long t = prevFound[index];
if (t > min && t < max) {
mid = t;
} else {
mid = (min + max) / 2;
}

while (min + 1 < max) {

long l = arr.get(mid);

if (l >= val) {
max = mid;
} else {
min = mid;
}
mid = (min + max) / 2;
}

prevFound[index] = min;

return min;
}

public CloseSuppressPath getBlocksPath() {
return blocksPath;
}

public CloseSuppressPath getSuperBlocksPath() {
return superBlocksPath;
}

@Override
public String toString() {
return "Bitmap375Big{}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class SequenceInt32 implements DynamicSequence {
/** The array that holds the objects **/
int[] data;
int numelements;
private final long[] prevFound = new long[16384];

/**
* Basic constructor
Expand Down Expand Up @@ -67,6 +68,11 @@ public void clear() {
Arrays.fill(data, 0);
}

@Override
public long[] getPrevFound() {
return prevFound;
}

private void resizeArray(int size) {
int[] newData = new int[size];
System.arraycopy(data, 0, newData, 0, Math.min(newData.length, data.length));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class SequenceInt64 implements DynamicSequence {
/** The array that holds the objects **/
long[] data;
long numelements;
private final long[] prevFound = new long[16384];

/**
* Basic constructor
Expand Down Expand Up @@ -68,6 +69,11 @@ public void clear() {
Arrays.fill(data, 0);
}

@Override
public long[] getPrevFound() {
return prevFound;
}

private void resizeArray(int size) {
long[] newData = new long[size];
System.arraycopy(data, 0, newData, 0, Math.min(newData.length, data.length));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class SequenceLog64 implements DynamicSequence {
protected int numbits;
protected long numentries;
protected long maxvalue;
private final long[] prevFound = new long[16384];

public SequenceLog64() {
this(W);
Expand Down Expand Up @@ -304,6 +305,11 @@ public void clear() {
Arrays.fill(data, 0);
}

@Override
public long[] getPrevFound() {
return prevFound;
}

/*
* (non-Javadoc)
* @see hdt.triples.array.Stream#getNumberOfElements()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class SequenceLog64Big implements DynamicSequence {
private int numbits;
private long numentries;
private long maxvalue;
private final long[] prevFound = new long[16384];

public SequenceLog64Big() {
this(W);
Expand Down Expand Up @@ -282,6 +283,11 @@ public void clear() {
data.clear();
}

@Override
public long[] getPrevFound() {
return prevFound;
}

/*
* (non-Javadoc)
* @see hdt.triples.array.Stream#getNumberOfElements()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class SequenceLog64BigDisk implements DynamicSequence, Closeable {
private long numentries;
private long maxvalue;

private final long[] prevFound = new long[16384];

public SequenceLog64BigDisk(String location) {
this(location, W);
}
Expand Down Expand Up @@ -292,6 +294,11 @@ public void clear() {
data.clear();
}

@Override
public long[] getPrevFound() {
return prevFound;
}

/*
* (non-Javadoc)
* @see hdt.triples.array.Stream#getNumberOfElements()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,19 @@ public long getNpredicates() {
return predicates.getNumberOfElements();
}

long cachedNobjects = -1;

@Override
public long getNobjects() {
return getNshared() + nonTyped.getNumberOfElements()
if (cachedNobjects != -1) {
return cachedNobjects;
}

long l = getNshared() + nonTyped.getNumberOfElements()
+ languages.values().stream().mapToLong(DictionarySection::getNumberOfElements).sum()
+ typed.values().stream().mapToLong(DictionarySection::getNumberOfElements).sum();
cachedNobjects = l;
return l;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
public class LargeLongArray implements LongArray {
private UnsafeLongArray array;
private final long[] prevFound = new long[16384];

/**
* @param array large array
Expand Down Expand Up @@ -55,4 +56,9 @@ public void resize(long newSize) throws IOException {
public void clear() {
array.clear();
}

@Override
public long[] getPrevFound() {
return prevFound;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,6 @@ public Long next() {
}
};
}

long[] getPrevFound();
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class LongArrayDisk implements Closeable, LongArray {
private long size;
private final long startByte;
private final Path location;
private final long[] prevFound = new long[16384];

public LongArrayDisk(String location, long size) {
this(location, size, true);
Expand Down Expand Up @@ -289,6 +290,11 @@ public void clear() {
set0(0, length());
}

@Override
public long[] getPrevFound() {
return prevFound;
}

/**
* @return the location of the array disk
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Arrays;

public class SimpleLongArray implements LongArray {
private final long[] prevFound = new long[16384];

public static LongArray of(int size) {
return wrapper(new long[size]);
Expand Down Expand Up @@ -60,4 +61,9 @@ public void resize(long newSize) throws IOException {
public void clear() {
Arrays.fill(array, 0);
}

@Override
public long[] getPrevFound() {
return prevFound;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class SimpleSplitLongArray implements LongArray, Closeable {
private final long max;
private final int indexMask;
private final int numbits;
private final long[] prevFound = new long[16384];

private long size;

Expand Down Expand Up @@ -113,6 +114,11 @@ public void clear() {
array.clear();
}

@Override
public long[] getPrevFound() {
return prevFound;
}

@Override
public void close() throws IOException {
IOUtil.closeObject(array);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
* @author Antoine Willerval
*/
public class SyncLongArray implements LongArray {

private final long[] prevFound = new long[16384];

/**
* Sync a long array
*
Expand Down Expand Up @@ -57,4 +60,10 @@ public synchronized void resize(long newSize) throws IOException {
public synchronized void clear() {
array.clear();
}

@Override
public long[] getPrevFound() {
return prevFound;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class WriteLongArrayBuffer implements LongArray, Closeable {
private ArrayElementInt[] bufferInt;
private int index = 0;
private boolean lastOrder;
private final long[] prevFound = new long[16384];

/**
* create the buffer
Expand Down Expand Up @@ -54,6 +55,11 @@ public void clear() {
index = 0;
}

@Override
public long[] getPrevFound() {
return prevFound;
}

public void free() {
flush();
bufferInt = null;
Expand Down

0 comments on commit f7e8e5e

Please sign in to comment.