goankur commented on code in PR #13572: URL: https://github.com/apache/lucene/pull/13572#discussion_r1828574298
########## lucene/core/src/java21/org/apache/lucene/internal/vectorization/PanamaVectorUtilSupport.java: ########## @@ -291,25 +296,125 @@ private float squareDistanceBody(float[] a, float[] b, int limit) { return res1.add(res2).reduceLanes(ADD); } - // Binary functions, these all follow a general pattern like this: - // - // short intermediate = a * b; - // int accumulator = (int)accumulator + (int)intermediate; - // - // 256 or 512 bit vectors can process 64 or 128 bits at a time, respectively - // intermediate results use 128 or 256 bit vectors, respectively - // final accumulator uses 256 or 512 bit vectors, respectively - // - // We also support 128 bit vectors, going 32 bits at a time. - // This is slower but still faster than not vectorizing at all. - + /** + * This method SHOULD NOT be used directly when the native dot-product is enabled. This is because + * it allocates off-heap memory in the hot code-path and copies the input byte-vectors onto it. + * This is necessary even with Panama APIs because native code is given a pointer to a memory + * address and if the content at that address can be moved at anytime (by Java GC in case of heap + * allocated memory) then safety of the native computation cannot be guaranteed. If we try to pass + * MemorySegment backing on-heap memory to native code we get + * "java.lang.UnsupportedOperationException: Not a native address" + * + * <p>Stack overflow thread: + * https://stackoverflow.com/questions/69521289/jep-412-pass-a-on-heap-byte-array-to-native-code-getting-unsupportedoperatione + * explains the issue in more detail. + * + * <p>Q1. Why did we enable the native code path here if its inefficient ? A1. So that it can be Review Comment: makes sense! The changes here were undone and `TestVectorUtilSupport`, `BaseVectorizationTestCase` were modified in the next revision to exercise the native code path by obtaining and invoking method handle for `dotProduct(MemorySegment a, MemorySegment b)` where 'a' and 'b' are constructed as off-heap MemorySegments. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org