Skip to content

Commit

Permalink
Pool
Browse files Browse the repository at this point in the history
  • Loading branch information
tzaeschke committed Jul 28, 2023
1 parent 3b407c0 commit c51f5f4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
45 changes: 25 additions & 20 deletions src/main/java/ch/ethz/globis/phtree/v16/PhIteratorKnn.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

/**
* kNN query implementation that uses preprocessors and distance functions.
*
* <p>
* Implementation after Hjaltason and Samet (with some deviations: no MinDist or MaxDist used).
* G. R. Hjaltason and H. Samet., "Distance browsing in spatial databases.", ACM TODS 24(2):265--318. 1999
*
Expand All @@ -45,7 +45,6 @@ public boolean isValid(int bitsToIgnore, long[] prefix) {
MinHeap<NodeDistT> queueN = MinHeap.create((t1, t2) -> t1.dist < t2.dist);
MinMaxHeap<PhEntryDist<T>> queueV = MinMaxHeap.create((t1, t2) -> t1.dist() < t2.dist());
double maxNodeDist = Double.POSITIVE_INFINITY;
private PhEntryDist<T> resultFree;
private PhEntryDist<T> resultToReturn;
private boolean isFinished = false;
private int remaining;
Expand All @@ -57,7 +56,7 @@ public boolean isValid(int bitsToIgnore, long[] prefix) {
this.distFn = distFn;
this.pht = pht;
this.nodeIter = new NodeIteratorFullNoGC<>();
this.resultFree = new PhEntryDist<>(new long[pht.getDim()], null, 0);
//this.resultFree = new PhEntryDist<>(new long[pht.getDim()], null, 0);
this.resultToReturn = new PhEntryDist<>(new long[pht.getDim()], null, 0);
reset(minResults, distFn, center);
}
Expand All @@ -78,14 +77,14 @@ public PhKnnQuery<T> reset(int minResults, PhDistance distFn, long[] center) {
queueN.clear();
queueV.clear();

queueN.push(new NodeDistT(0, root, new long[pht.getDim()]));
queueN.push(createEntry(0, root));
findNextElement();
return this;
}

@Override
public long[] nextKey() {
return nextEntryReuse().getKey();
return nextEntryReuse().getKey().clone();
}

@Override
Expand All @@ -110,7 +109,7 @@ public PhEntryDist<T> nextEntryReuse() {

@Override
public boolean hasNext() {
return !isFinished; // TODO remove flag?
return !isFinished;
}

@Override
Expand All @@ -133,7 +132,6 @@ private void findNextElement() {
PhEntryDist<T> result = queueV.peekMin();
queueV.popMin();
--remaining;
resultFree = resultToReturn;
resultToReturn = result;
currentDistance = result.dist(); // TODO remove field?
return;
Expand All @@ -157,7 +155,7 @@ private void findNextElement() {
Node sub = (Node) result.getNodeInternal();
double dist = distToNode(result.getKey(), sub.getPostLen() + 1);
if (dist <= maxNodeDist) {
queueN.push(new NodeDistT(dist, sub, result.getKey()));
queueN.push(new NodeDistT(dist, sub));
}
} else {
double d = distFn.dist(center, result.getKey());
Expand Down Expand Up @@ -196,26 +194,33 @@ private double distToNode(long[] prefix, int bitsToIgnore) {
}

// TODO use this pool!
private static <T> PhEntryDist<T> createEntry(ArrayList<PhEntryDist<T>> pool,
long[] key, T val, double dist) {
if (pool.isEmpty()) {
return new PhEntryDist<>(key, val, dist);
}
PhEntryDist<T> e = pool.remove(pool.size() - 1);
e.setKeyInternal(key);
e.set(val, dist);
return e;
private PhEntryDist<T> createEntry(long[] key, T val, double dist) {
return new PhEntryDist<>(key, val, dist);
// PhEntryDist<T> e = pool.remove(pool.size() - 1);
// e.setKeyInternal(key);
// e.set(val, dist);
// return e;
}

private NodeDistT createEntry(double dist, Node node) {
return new NodeDistT(dist, node);
// PhEntryDist<T> e = pool.remove(pool.size() - 1);
// e.setKeyInternal(key);
// e.set(val, dist);
// return e;
}

private static class NodeDistT {
double dist;
Node node;
long[] prefix;

public NodeDistT(double dist, Node node, long[] prefix) {
public NodeDistT() {
dist = -1;
}

public NodeDistT(double dist, Node node) {
this.dist = dist;
this.node = node;
this.prefix = prefix;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

public class MinHeapPoolTest {

private static final int SEEDS = 100; // 100 for benchmarking
private static final int SEEDS = 10; // 100 for benchmarking

private MinHeapPoolI<Entry> create() {
return MinHeapPool.create((o1, o2) -> o1.d < o2.d, Entry::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

public class MinMaxHeapPoolTest {

private static final int SEEDS = 100; // 100 for benchmarking
private static final int SEEDS = 10; // 100 for benchmarking

private MinMaxHeapPoolI<Entry> create() {
return MinMaxHeapPool.create((o1, o2) -> o1.d < o2.d, Entry::new);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

public class MinMaxHeapTest {

private static final int SEEDS = 100; // 100 for benchmarking
private static final int SEEDS = 10; // 100 for benchmarking

private MinMaxHeapI<Entry> create() {
return MinMaxHeap.create((o1, o2) -> o1.d < o2.d);
Expand Down

0 comments on commit c51f5f4

Please sign in to comment.