Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #11 from julsal/master
Browse files Browse the repository at this point in the history
casting bugs and minor optimization
  • Loading branch information
erikbern authored Jul 19, 2017
2 parents 06f7917 + 87c1ed6 commit c4521d8
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/main/java/com/spotify/annoy/ANNIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ public class ANNIndex implements AnnoyIndex {

// size of C structs in bytes (initialized in init)
private final int K_NODE_HEADER_STYLE;
private final int NODE_SIZE;
private final long NODE_SIZE;

private final int INT_SIZE = 4;
private final int FLOAT_SIZE = 4;
private final int MAX_NODES_IN_BUFFER;
private final int BLOCK_SIZE;
private RandomAccessFile memoryMappedFile;

Expand Down Expand Up @@ -69,9 +70,9 @@ public ANNIndex(final int dimension,
// them where the separating plane normally goes)
this.MIN_LEAF_SIZE = DIMENSION + 2;
this.NODE_SIZE = K_NODE_HEADER_STYLE + FLOAT_SIZE * DIMENSION;
this.BLOCK_SIZE = blockSize == 0 ?
Integer.MAX_VALUE / NODE_SIZE : blockSize * NODE_SIZE;

this.MAX_NODES_IN_BUFFER = (int) (blockSize == 0 ?
Integer.MAX_VALUE / NODE_SIZE : blockSize * NODE_SIZE);
BLOCK_SIZE = (int) (this.MAX_NODES_IN_BUFFER * NODE_SIZE);
roots = new ArrayList<>();
load(filename);
}
Expand All @@ -83,7 +84,8 @@ private void load(final String filename) throws IOException {
throw new IOException("Index is a 0-byte file?");
}

int buffIndex = (int) ((fileSize - 1) / BLOCK_SIZE);
int numNodes = (int) (fileSize / NODE_SIZE);
int buffIndex = (numNodes - 1) / MAX_NODES_IN_BUFFER;
int rest = (int) (fileSize % BLOCK_SIZE);
int blockSize = (rest > 0 ? rest : BLOCK_SIZE);
long position = fileSize - blockSize;
Expand All @@ -99,7 +101,7 @@ private void load(final String filename) throws IOException {

buffers[buffIndex--] = annBuf;

for (int i = blockSize - NODE_SIZE; process && i >= 0; i -= NODE_SIZE) {
for (int i = blockSize - (int) NODE_SIZE; process && i >= 0; i -= NODE_SIZE) {
index -= NODE_SIZE;
int k = annBuf.getInt(i); // node[i].n_descendants
if (m == -1 || k == m) {
Expand All @@ -115,21 +117,21 @@ private void load(final String filename) throws IOException {
}

private float getFloatInAnnBuf(long pos) {
int b = (int) pos / BLOCK_SIZE;
int f = (int) pos % BLOCK_SIZE;
int b = (int) (pos / BLOCK_SIZE);
int f = (int) (pos % BLOCK_SIZE);
return buffers[b].getFloat(f);
}

private int getIntInAnnBuf(long pos) {
int b = (int) pos / BLOCK_SIZE;
int i = (int) pos % BLOCK_SIZE;
int b = (int) (pos / BLOCK_SIZE);
int i = (int) (pos % BLOCK_SIZE);
return buffers[b].getInt(i);
}

@Override
public void getNodeVector(final long nodeOffset, float[] v) {
MappedByteBuffer nodeBuf = buffers[(int) nodeOffset / BLOCK_SIZE];
int offset = (int) (nodeOffset % BLOCK_SIZE) + K_NODE_HEADER_STYLE;
MappedByteBuffer nodeBuf = buffers[(int) (nodeOffset / BLOCK_SIZE)];
int offset = (int) ((nodeOffset % BLOCK_SIZE) + K_NODE_HEADER_STYLE);
for (int i = 0; i < DIMENSION; i++) {
v[i] = nodeBuf.getFloat(offset + i * FLOAT_SIZE);
}
Expand Down

0 comments on commit c4521d8

Please sign in to comment.