Class SWARUtil$

java.lang.Object
org.apache.pekko.util.SWARUtil$

public class SWARUtil$ extends Object
SWAR (SIMD Within A Register) utility class. Internal Use Only.

Copied from the Netty Project. https://github.com/netty/netty/blob/d28a0fc6598b50fbe8f296831777cf4b653a475f/common/src/main/java/io/netty/util/internal/SWARUtil.java

Multi-byte reads use MethodHandles.byteArrayViewVarHandle(java.lang.Class<?>, java.nio.ByteOrder), which allows reading several bytes from a byte array as a single typed value (e.g. short, int, or long) in one operation rather than reading and shifting each byte individually.

The JDK itself uses the same technique. Since Java 17, jdk.internal.util.ByteArray (big endian) and jdk.internal.util.ByteArrayLittleEndian (little endian) use MethodHandles.byteArrayViewVarHandle for every primitive type, and those helpers back the public APIs of java.io.DataInputStream (readShort, readInt, readLong, etc.) and java.util.UUID construction from bytes.

Why this is faster than byte-by-byte shifts

  • Single native load instruction – on x86/x64 and AArch64 the HotSpot JIT intrinsifies the VarHandle access into a single MOVZX, MOV, or LDR instruction that reads the full value directly from memory, whereas manual byte-shift code requires multiple load-and-shift-and-or sequences that are harder for the JIT to collapse.
  • Consolidated bounds check – a single range check covers the entire multi-byte read; individual array(i) accesses each carry their own implicit bounds check.
  • No alignment requirement – unlike sun.misc.Unsafe the VarHandle variant works correctly on unaligned offsets, so callers do not need to pad or copy data to satisfy alignment constraints.
  • SWAR arithmetic – reading a full long with a single VarHandle call means eight bytes arrive in one register, enabling SWAR patterns that test all eight bytes in parallel (see applyPattern(long,long)).

A runtime try/catch guards each VarHandle creation; if the JVM does not support the API (e.g. older Android runtimes) the code falls back to explicit byte-by-byte shift implementations (getLongBEWithoutMethodHandle, etc.) so behaviour is always correct.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final SWARUtil$
    Static reference to the singleton instance of this Scala object.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    applyPattern(long word, long pattern)
    Applies a compiled pattern to given word.
    long
    compilePattern(byte byteToFind)
    Compiles given byte into a long pattern suitable for SWAR operations.
    int
    getIndex(long word)
    Returns the index of the first occurrence of byte specified in the pattern.
    int
    getInt(byte[] array, int index, ByteOrder byteOrder)
    Returns the int value at the specified index in the given byte array.
    int
    getLastIndex(long word)
    Returns the index of the last occurrence of a byte specified in the pattern within a word.
    long
    getLong(byte[] array, int index, ByteOrder byteOrder)
    Returns the long value at the specified index in the given byte array.
    short
    getShort(byte[] array, int index, ByteOrder byteOrder)
    Returns the short value at the specified index in the given byte array.
    void
    putInt(byte[] array, int index, int value, ByteOrder byteOrder)
    Writes an int value at the specified index in the given byte array.
    void
    putLong(byte[] array, int index, long value, ByteOrder byteOrder)
    Writes a long value at the specified index in the given byte array.
    void
    putShort(byte[] array, int index, int value, ByteOrder byteOrder)
    Writes the low 16 bits of value at the specified index in the given byte array.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MODULE$

      public static final SWARUtil$ MODULE$
      Static reference to the singleton instance of this Scala object.
  • Constructor Details

    • SWARUtil$

      public SWARUtil$()
  • Method Details

    • compilePattern

      public long compilePattern(byte byteToFind)
      Compiles given byte into a long pattern suitable for SWAR operations.
    • applyPattern

      public long applyPattern(long word, long pattern)
      Applies a compiled pattern to given word. Returns a word where each byte that matches the pattern has the highest bit set.

      Parameters:
      word - the word to apply the pattern to
      pattern - the pattern to apply
      Returns:
      a word where each byte that matches the pattern has the highest bit set
    • getIndex

      public int getIndex(long word)
      Returns the index of the first occurrence of byte specified in the pattern. If no pattern is found, returns 8. Currently only supports big endian.

      Parameters:
      word - the return value of applyPattern(long, long)
      Returns:
      the index of the first occurrence of the specified pattern in the specified word. If no pattern is found, returns 8.
    • getLastIndex

      public int getLastIndex(long word)
      Returns the index of the last occurrence of a byte specified in the pattern within a word. If no pattern is found, the result is -1. Currently only supports big endian.

      Parameters:
      word - the return value of applyPattern(long,long)
      Returns:
      the index of the last occurrence of the specified pattern in the specified word.
    • getLong

      public long getLong(byte[] array, int index, ByteOrder byteOrder)
      Returns the long value at the specified index in the given byte array. Uses big-endian byte order. Uses a VarHandle byte array view if supported. Does not range check - assumes caller has checked bounds.

      Parameters:
      array - the byte array to read from
      index - the index to read from
      byteOrder - the byte order to use (big-endian or little-endian)
      Returns:
      the long value at the specified index
    • getInt

      public int getInt(byte[] array, int index, ByteOrder byteOrder)
      Returns the int value at the specified index in the given byte array. Uses big-endian byte order. Uses a VarHandle byte array view if supported. Does not range check - assumes caller has checked bounds.

      Parameters:
      array - the byte array to read from
      index - the index to read from
      byteOrder - the byte order to use (big-endian or little-endian)
      Returns:
      the int value at the specified index
    • getShort

      public short getShort(byte[] array, int index, ByteOrder byteOrder)
      Returns the short value at the specified index in the given byte array. Uses big-endian byte order. Uses a VarHandle byte array view if supported. Does not range check - assumes caller has checked bounds.

      Parameters:
      array - the byte array to read from
      index - the index to read from
      byteOrder - the byte order to use (big-endian or little-endian)
      Returns:
      the short value at the specified index
    • putInt

      public void putInt(byte[] array, int index, int value, ByteOrder byteOrder)
      Writes an int value at the specified index in the given byte array. Uses a VarHandle byte array view if supported, otherwise falls back to byte-by-byte writes. Does not range check - assumes caller has checked bounds.

      Parameters:
      array - the byte array to write to
      index - the index to write at
      value - the int value to write
      byteOrder - the byte order to use (big-endian or little-endian)
    • putShort

      public void putShort(byte[] array, int index, int value, ByteOrder byteOrder)
      Writes the low 16 bits of value at the specified index in the given byte array. Uses a VarHandle byte array view if supported, otherwise falls back to byte-by-byte writes. Does not range check - assumes caller has checked bounds.

      Parameters:
      array - the byte array to write to
      index - the index to write at
      value - the value whose low 16 bits are written
      byteOrder - the byte order to use (big-endian or little-endian)
    • putLong

      public void putLong(byte[] array, int index, long value, ByteOrder byteOrder)
      Writes a long value at the specified index in the given byte array. Uses a VarHandle byte array view if supported, otherwise falls back to byte-by-byte writes. Does not range check - assumes caller has checked bounds.

      Parameters:
      array - the byte array to write to
      index - the index to write at
      value - the long value to write
      byteOrder - the byte order to use (big-endian or little-endian)