airlift/slice

repository·master·Indexed 19 days ago

https://github.com/airlift/slice

A high-performance Java library for efficient byte array manipulation. Slice 2.0 provides fast access to primitives using little-endian byte order via VarHandles and MemorySegment, bulk data transfers, and specialized utilities for UTF-8 encoded data and IO streams.

Tokens
603
Snippets
0
Records
6
Agent score
18%

What's inside Slice

  1. Overview of Slice 2.0

    master
    Slice is a Java library designed for efficient manipulation of Java byte arrays. It provides high-performance methods for reading and writing primitive values (such as long) at specific indices without manual byte assembly. It also supports bulk data transfers between byte arrays and primitive arrays.
  2. Use Slice for IO Streams

    master
    Slice includes specialized classes for interacting with InputStream and OutputStream. These classes inherit the same performance characteristics as the core Slice class, allowing for fast single-primitive access and bulk data transfers directly through IO streams.
  3. Manipulate UTF-8 data with Slice

    master

    Slice provides a dedicated library for processing UTF-8 encoded data stored within byte arrays. You can use these utilities to perform common string-like operations directly on the bytes, including:

    • Counting code points
    • Substring operations
    • Trimming
    • Case changes
  4. Understand Byte Order and Platform Compatibility

    master

    Slice 2.0 uses little-endian byte order for storing multi-byte values.

    This design ensures data consistency regardless of the host CPU's endianness. While previous versions relied on sun.misc.Unsafe (which could cause corruption on big-endian CPUs), Slice 2.0 uses VarHandles and MemorySegment to explicitly specify little-endian order. This allows the library to work correctly on any CPU architecture by transparently handling byte swapping when necessary.

    Note that hash functions like XxHash64 and Murmur3 also follow this little-endian specification.

  5. Migrate from Legacy Slice

    master

    If your project requires support for off-heap memory or the ability to be backed by any Java primitive array type, you must use a version of Slice prior to 2.0. These features were removed in version 2.0 to simplify the codebase.

    Legacy versions are available on the release-0.x branch.

  6. Understand Memory Copy Microbenchmark results

    master

    The MemoryCopyBenchmark measures the throughput of different memory copy strategies used in Slice. Throughput is measured in ops/ms (operations per millisecond), where higher numbers indicate better performance.

    The benchmarks compare three primary methods across various payload sizes (from 0 to 128MB):

    1. slice: The standard Slice memory copy implementation.
    2. customLoop: A manual loop-based copy implementation.
    3. unsafe: A copy implementation using unsafe operations.

    Use these results to understand how Slice's performance scales with different data sizes and to compare its efficiency against manual or unsafe alternatives on specific hardware architectures.