spotify / voyager

🛰️ An approximate nearest-neighbor search library for Python and Java with a focus on ease of use, simplicity, and deployability.
https://spotify.github.io/voyager/
Apache License 2.0
1.26k stars 51 forks source link

Ensure that native C++ index object doesn't get destroyed while Java threads are using it. #36

Closed psobot closed 5 months ago

psobot commented 10 months ago

This PR adds a somewhat clever solution to give us a bit more thread safety around our JNI bindings to Voyager's C++ code: instead of storing a raw pointer on the Java object, we now store a pointer to a std::shared_ptr, which gives us thread-safe destruction and ensures that the underlying C++ object doesn't get destroyed while another Java thread might still be using it.

samek commented 10 months ago

@psobot So I should remove https://github.com/spotify/voyager/pull/35 since it calls index the old way, and put in another one once this one is merged?

psobot commented 10 months ago

No need, @samek - if your PR gets merged first, I'll update this one (as it'll fail to compile), and vice versa.

markkohdev commented 9 months ago

@psobot is there a good way to test this to ensure that it's working as expected?

psobot commented 8 months ago

@markkohdev test added!