Problem
At the moment, the general patterns to extract float[] arrays from VectorFloat<?> instances are as such:
The reality is that all current implementations of VectorFloat<?> (but not necessarily future ones) are backed by float[] and could provide fast, zero-copy access to it.
Possible solutions
New API, opt-in only
Add FloatArray interface to allow easy float[] vector extraction from MemorySegmentVectorFloat and ArrayVectorFloat instances. It solves the problem by directly exposing the backed array through float[] array() method, no copying. This is opt-in only, the change is 100% non-breaking.
See please: #694
Change existing API
Add toArray method to existing VectorFloat<?> API. This is simpler change (since we could provide the default implementation using length() + get(index)) but we cannot guarantee that every toArray implementation is going to be zero copy, although we could document that.
See please: #697
CC @ashkrisk @akash-shankaran
Problem
At the moment, the general patterns to extract
float[]arrays fromVectorFloat<?>instances are as such:length()+get(index)to have a full copy, using per-element accessVectorFloat<?>::get()and check if it instance offloat[](covered byArrayVectorFloat)VectorFloat<?>::get()and check if it instance ofMemorySegment, use reflection (since API is only available on JDK-22) to accessheapBase()orValueLayout(see please https://github.com/opensearch-project/opensearch-jvector/pull/507/changes#diff-af8a9e9960c5820cdc11161a01241bc490fb4aa17184c0e6b5397ae10af85307R18 as an example)The reality is that all current implementations of
VectorFloat<?>(but not necessarily future ones) are backed byfloat[]and could provide fast, zero-copy access to it.Possible solutions
New API, opt-in only
Add
FloatArrayinterface to allow easyfloat[]vector extraction fromMemorySegmentVectorFloatandArrayVectorFloatinstances. It solves the problem by directly exposing the backed array throughfloat[] array()method, no copying. This is opt-in only, the change is 100% non-breaking.See please: #694
Change existing API
Add
toArraymethod to existingVectorFloat<?>API. This is simpler change (since we could provide the default implementation usinglength()+get(index)) but we cannot guarantee that everytoArrayimplementation is going to be zero copy, although we could document that.See please: #697
CC @ashkrisk @akash-shankaran