ChrisHegarty commented on code in PR #12311:
URL: https://github.com/apache/lucene/pull/12311#discussion_r1198189573
##########
lucene/core/src/java/org/apache/lucene/util/VectorUtil.java:
##########
@@ -270,4 +216,134 @@ public static float dotProductScore(byte[] a, byte[] b) {
float denom = (float) (a.length * (1 << 15));
return 0.5f + dotProduct(a, b) / denom;
}
+
+ interface VectorUtilProvider {
+
+ // just dot product for now
+ float dotProduct(float[] a, float[] b);
+ }
+
+ private static VectorUtilProvider lookupProvider() {
+ // TODO: add a check
+ final int runtimeVersion = Runtime.version().feature();
+ if (runtimeVersion == 20) { // TODO: do we want JDK 19?
+ try {
+ ensureReadability();
+ final var lookup = MethodHandles.lookup();
+ final var cls =
lookup.findClass("org.apache.lucene.util.JDKVectorUtilProvider");
+ // we use method handles, so we do not need to deal with setAccessible
as we have private
+ // access through the lookup:
+ final var constr = lookup.findConstructor(cls,
MethodType.methodType(void.class));
+ try {
+ return (VectorUtilProvider) constr.invoke();
+ } catch (RuntimeException | Error e) {
+ throw e;
+ } catch (Throwable th) {
+ throw new AssertionError(th);
+ }
+ } catch (NoSuchMethodException | IllegalAccessException e) {
+ throw new LinkageError("JDKVectorUtilProvider is missing correctly
typed constructor", e);
+ } catch (ClassNotFoundException cnfe) {
+ throw new LinkageError("JDKVectorUtilProvider is missing in Lucene JAR
file", cnfe);
+ }
+ } else if (runtimeVersion >= 21) {
+ LOG.warning(
+ "You are running with Java 21 or later. To make full use of the
Vector API, please update Apache Lucene.");
+ }
+ return new LuceneVectorUtilProvider();
+ }
+
+ // Extracted to a method to be able to apply the SuppressForbidden annotation
+ @SuppressWarnings("removal")
+ @SuppressForbidden(reason = "security manager")
+ private static <T> T doPrivileged(PrivilegedAction<T> action) {
+ return AccessController.doPrivileged(action);
+ }
+
+ static void ensureReadability() {
+ ModuleLayer.boot().modules().stream()
+ .filter(m -> m.getName().equals("jdk.incubator.vector"))
+ .findFirst()
+ .ifPresentOrElse(
+ vecMod -> VectorUtilProvider.class.getModule().addReads(vecMod),
+ () -> LOG.warning("vector incubator module not present"));
+ }
+
+ static {
+ PROVIDER =
Review Comment:
I’ll refactor to minimise the privileged block.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]