Package com.cedarsoftware.util.internal
Class VectorizedArrays
java.lang.Object
com.cedarsoftware.util.internal.VectorizedArrays
Internal API — not for external use.
Range-based array operations that dispatch to JDK 9+Arrays intrinsics
(SIMD-vectorized on supported HW) when the runtime JVM supports them, and fall
back to hand-rolled loops on JDK 8. java-util's source/target is JDK 1.8,
so the JDK 9+ Arrays.equals(arr, int, int, arr, int, int) / mismatch
/ compare signatures can't be referenced at compile time — but the same
library is overwhelmingly run on modern JVMs in production. This helper bridges
the gap with one-time reflective resolution at class load.
Dispatch mechanics
- At class load,
SystemUtilities.isJavaVersionAtLeast(9, 0)is queried once. On JDK 8 we short-circuit tonullhandles and skip the reflection cost entirely. - On JDK 9+, each operation is resolved via
MethodHandles.publicLookup()and cached in astatic finalMethodHandle.invokeExacton astatic final MHis recognised by HotSpot and inlined to direct intrinsic dispatch in steady state. - Per-call cost is one static-field read + null-check + (on JDK 9+)
MH.invokeExact. No per-call version checks.
Exposed operations
equalsRange— char and byte variantsmismatchRange— char and byte variantscompareRange— char and byte variants
System.arraycopy is already a HotSpot intrinsic on JDK 8+;
there's no slower portable fallback to dispatch to, so it's not exposed here.
Use System.arraycopy directly.)
Visibility
Exposed tocom.cedarsoftware:json-io via a qualified JPMS export
(exports com.cedarsoftware.util.internal to com.cedarsoftware.io).
Signatures and semantics may change without notice across minor releases.- Author:
- John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
License
-
Method Summary
Modifier and TypeMethodDescriptionstatic intcompareRange(byte[] a, int aFrom, int aTo, byte[] b, int bFrom, int bTo) Byte variant ofcompareRange(char[], int, int, char[], int, int).static intcompareRange(char[] a, int aFrom, int aTo, char[] b, int bFrom, int bTo) Equivalent to JDK 9+'sArrays.compare(a, aFrom, aTo, b, bFrom, bTo).static booleanequalsRange(byte[] a, int aFrom, int aTo, byte[] b, int bFrom, int bTo) Byte variant ofequalsRange(char[], int, int, char[], int, int).static booleanequalsRange(char[] a, int aFrom, int aTo, char[] b, int bFrom, int bTo) Equivalent to JDK 9+'sArrays.equals(a, aFrom, aTo, b, bFrom, bTo)— returnstrueiff the two ranges have the same length and contain element-wise equal chars.static intmismatchRange(byte[] a, int aFrom, int aTo, byte[] b, int bFrom, int bTo) Byte variant ofmismatchRange(char[], int, int, char[], int, int).static intmismatchRange(char[] a, int aFrom, int aTo, char[] b, int bFrom, int bTo) Equivalent to JDK 9+'sArrays.mismatch(a, aFrom, aTo, b, bFrom, bTo).
-
Method Details
-
equalsRange
public static boolean equalsRange(char[] a, int aFrom, int aTo, char[] b, int bFrom, int bTo) Equivalent to JDK 9+'sArrays.equals(a, aFrom, aTo, b, bFrom, bTo)— returnstrueiff the two ranges have the same length and contain element-wise equal chars. SIMD-vectorized on JDK 9+; manual loop on JDK 8. -
equalsRange
public static boolean equalsRange(byte[] a, int aFrom, int aTo, byte[] b, int bFrom, int bTo) Byte variant ofequalsRange(char[], int, int, char[], int, int). -
mismatchRange
public static int mismatchRange(char[] a, int aFrom, int aTo, char[] b, int bFrom, int bTo) Equivalent to JDK 9+'sArrays.mismatch(a, aFrom, aTo, b, bFrom, bTo). Returns the relative index of the first mismatching element (i.e.0for the first element in each range), or-1if the ranges are equal over their common prefix and have the same length. If the ranges have different lengths and are equal over the common prefix, returns the length of the shorter range. -
mismatchRange
public static int mismatchRange(byte[] a, int aFrom, int aTo, byte[] b, int bFrom, int bTo) Byte variant ofmismatchRange(char[], int, int, char[], int, int). -
compareRange
public static int compareRange(char[] a, int aFrom, int aTo, char[] b, int bFrom, int bTo) Equivalent to JDK 9+'sArrays.compare(a, aFrom, aTo, b, bFrom, bTo). Returns a negative integer, zero, or a positive integer as the first range is lexicographically less than, equal to, or greater than the second. If the ranges are equal over the common prefix, the shorter range compares less. -
compareRange
public static int compareRange(byte[] a, int aFrom, int aTo, byte[] b, int bFrom, int bTo) Byte variant ofcompareRange(char[], int, int, char[], int, int).
-