From 774e7d41b5ab146f3316eab667b54efe9b16aa33 Mon Sep 17 00:00:00 2001 From: Andriy Redko Date: Thu, 18 Jan 2024 18:38:48 -0500 Subject: [PATCH] Only check LongVector.SPECIES_PREFERRED when jdk.incubator.vector is enabled (#11939) Signed-off-by: Andriy Redko --- .../common/round/RoundableFactory.java | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/libs/common/src/main/java20/org/opensearch/common/round/RoundableFactory.java b/libs/common/src/main/java20/org/opensearch/common/round/RoundableFactory.java index c5ec45bceb5bd..0709ed4374227 100644 --- a/libs/common/src/main/java20/org/opensearch/common/round/RoundableFactory.java +++ b/libs/common/src/main/java20/org/opensearch/common/round/RoundableFactory.java @@ -10,8 +10,6 @@ import org.opensearch.common.annotation.InternalApi; -import jdk.incubator.vector.LongVector; - /** * Factory class to create and return the fastest implementation of {@link Roundable}. * @@ -34,10 +32,30 @@ public final class RoundableFactory { */ private static final boolean USE_BTREE_SEARCHER; + /** + * This class is initialized only when: + * - JDK-20+ + * - jdk.incubator.vector.LongVector is available (--add-modules=jdk.incubator.vector is passed) + */ + private static final class VectorCheck { + final static int SPECIES_PREFERRED = jdk.incubator.vector.LongVector.SPECIES_PREFERRED.length(); + } + static { String simdRoundingFeatureFlag = System.getProperty("opensearch.experimental.feature.simd.rounding.enabled"); - USE_BTREE_SEARCHER = "forced".equalsIgnoreCase(simdRoundingFeatureFlag) - || (LongVector.SPECIES_PREFERRED.length() >= 4 && "true".equalsIgnoreCase(simdRoundingFeatureFlag)); + boolean useBtreeSearcher = false; + + try { + final Class incubator = Class.forName("jdk.incubator.vector.LongVector"); + + useBtreeSearcher = "forced".equalsIgnoreCase(simdRoundingFeatureFlag) + || (VectorCheck.SPECIES_PREFERRED >= 4 && "true".equalsIgnoreCase(simdRoundingFeatureFlag)); + + } catch (final ClassNotFoundException ex) { + /* do not use BtreeSearcher */ + } + + USE_BTREE_SEARCHER = useBtreeSearcher; } private RoundableFactory() {}