Class UninitializedArrays

java.lang.Object
org.elasticsearch.compute.data.UninitializedArrays

public final class UninitializedArrays extends Object
Allocates uninitialized primitive arrays via jdk.internal.misc.Unsafe#allocateUninitializedArray.

Unlike new T[length], the returned arrays are not zeroed by the JVM. Callers must overwrite every element they use before reading it, otherwise they will observe stale memory contents. Use only on the hot path where the zeroing cost is measurable and the array is guaranteed to be fully written before being read.

Requires the JVM to be started with --add-opens=java.base/jdk.internal.misc=ALL-UNNAMED so that reflective access to the singleton Unsafe instance is permitted. When the reflective bootstrap fails (e.g. the flag is missing or a future JDK changes the internals), this class transparently falls back to zero-initialized new T[length] allocation after logging a warning.

TLAB-size limit: the HotSpot _allocateUninitializedArray intrinsic only skips zeroing on the JIT-inlined TLAB fast path. Allocations that don't fit in the current thread's TLAB (typically arrays larger than a few hundred KB, depending on -XX:TLABSize and occupancy) fall back to OptoRuntime::new_array_CMemAllocator::allocate, which always zeros the array body before returning it. The intrinsic cannot override that — large heap allocations are zeroed by the GC's allocator regardless of which API requested them. As a result this helper is only a win for small scratch arrays (per-batch decode buffers, tens of KB). For multi-MB buffers, the only way to avoid the zeroing cost is to pool and reuse the buffer instead of allocating a fresh one.

  • Method Details

    • isUnsafeEnabled

      public static boolean isUnsafeEnabled()
      Returns true when the reflective bootstrap of jdk.internal.misc.Unsafe#allocateUninitializedArray succeeded and the newXxxArray methods will return uninitialized arrays. Returns false when the JVM is missing --add-opens=java.base/jdk.internal.misc=ALL-UNNAMED (or the JDK internals changed), in which case the newXxxArray methods silently fall back to zero-initialized new T[length].
    • ensureUnsafeEnabled

      public static void ensureUnsafeEnabled()
      Throws IllegalStateException if isUnsafeEnabled() is false. Use from production code paths or tests that require the uninitialized fast path and must fail loudly rather than silently fall back to zero-initialized allocation.
    • newBooleanArray

      public static boolean[] newBooleanArray(int length)
    • newByteArray

      public static byte[] newByteArray(int length)
    • newShortArray

      public static short[] newShortArray(int length)
    • newCharArray

      public static char[] newCharArray(int length)
    • newIntArray

      public static int[] newIntArray(int length)
    • newLongArray

      public static long[] newLongArray(int length)
    • newFloatArray

      public static float[] newFloatArray(int length)
    • newDoubleArray

      public static double[] newDoubleArray(int length)