Skip to content
This repository has been archived by the owner on Jul 22, 2019. It is now read-only.

Allow multi-instances of annoy trees to be loaded #6

Merged
merged 7 commits into from
Apr 24, 2017
Merged

Conversation

codewitch
Copy link
Collaborator

We are passing the pointer for the tree so it can be shared between cpp and java. Now, the trees have to be explicitly closed so there is no build up of memory on the cpp side.

Next steps:

  • update readme to let users know to always close the tree.
  • make sure this version works for current users

@ijklr
Copy link
Collaborator

ijklr commented Apr 19, 2017

👍

@ijklr
Copy link
Collaborator

ijklr commented Apr 19, 2017

@yonromai romain this has the change I told you about yesterday: having multiple instances of the annoy tree.

import java.util.List;

/**
* Annoy interface
* Modeled after: https://github.com/spotify/annoy/blob/master/annoy/__init__.py, sorta
*/
public interface AnnoyIndex {
public interface AnnoyIndex extends Closeable {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason against extending AutoCloseable instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope!


public List<Integer> getNearestByVector(List<Float> vector, int nbNeighbors) {
validateVecSize(vector);
return primitiveToBoxed(cppGetNearestByVector(boxedToPrimitive(vector), nbNeighbors));
return primitiveToBoxed(
cppGetNearestByVector(this.cppPtr, boxedToPrimitive(vector), nbNeighbors));
Copy link
Owner

@yonromai yonromai Apr 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why using this. everywhere? cppPtr is a member field

@@ -94,7 +94,6 @@ public void euclideanDistanceTest() {
assertEquals(annoyIndex.getDistance(0, 1), expectedEuclideanDistance, EPS);
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it would be useful to add a test for multiple trees?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

@yonromai
Copy link
Owner

Thanks a lot for your PR!
A quick question: you said Now, the trees have to be explicitly closed so there is no build up of memory on the cpp side..
Considering this change, if 1 tree is opened and not closed, is the memory build up any different than before this change?

@codewitch
Copy link
Collaborator Author

I see what you mean. In the current state, since you are always reassigning the static annoy_tree with the newly loaded one the older tress don't get destructed.
So this will probably be better for memory management :)

@codewitch
Copy link
Collaborator Author

Updated the PR with all the changes.

The test fails on master but passes on this branch!

Copy link
Owner

@yonromai yonromai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for the improvements!

@yonromai yonromai merged commit 442b6aa into master Apr 24, 2017
@yonromai yonromai deleted the multinst branch April 24, 2017 17:03
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants