Coding

Library for fast mapping of Java records to native memory

A new Java library, TypedMemory, enables developers to efficiently map Java records to native memory using a novel combination of Java's record types and the Unsafe API, promising significant performance gains for applications reliant on low-level memory management. By leveraging the compiler's record type optimization, TypedMemory eliminates the need for manual memory layout specification, streamlining the development process. Early benchmarks indicate a 2x to 5x speedup over traditional approaches.

TypedMemory is a Java library that enables strongly typed access to off-heap memory by mapping Java record types onto native memory. Built on the Java Foreign Function & Memory (FFM) API, it simplifies low-level memory management for performance-critical applications such as simulation, graphics, native interop, and data-oriented programming. The library targets Java 25 and later, leveraging modern Java features including records and the ClassFile API.

Overview

TypedMemory provides a type-safe abstraction over contiguous off-heap memory regions allocated via FFM’s Arena API. Instead of manually defining memory layouts, offsets, and access patterns, developers declare data structures using Java records. The library automatically derives the corresponding MemoryLayout, enabling direct get/set operations on structured memory with compile-time type safety.

The library is experimental and may introduce breaking changes. It is available from Maven Central under version 0.1.0 and is licensed under Apache 2.0.

What it does

Key capabilities include:

  • Mapping Java record types to off-heap memory using Mem.of(Class<T>, Arena, count)
  • Reading and writing elements via get(index) and set(index, value)
  • Preserving exact memory layout for native interoperation
  • Supporting nested records and fixed-size arrays via @size(n) annotation
  • Wrapping existing MemorySegment instances with Mem.wrap(Class<T>, segment)
  • Performing bulk operations: fill, init, copyTo, copyFrom, swap
  • Exposing layout introspection through layout() for debugging and alignment verification

Example usage:

record Color(float r, float g, float b, float a) {}

try (Arena arena = Arena.ofConfined()) {
    Mem<Color> colors = Mem.of(Color.class, arena, 3);
    colors.set(0, new Color(1f, 0f, 0f));
    Color c = colors.get(0);
}

Structured records with arrays are supported:

record Pixel(int i, int j) {}
record Point(byte x, @size(3) Pixel[] y, @size(3) int[] z) {}

Tradeoffs

TypedMemory requires Java 25 or greater due to its reliance on the ClassFile API. When using reinterpret operations, applications must be launched with --enable-native-access=ALL-UNNAMED for unnamed modules or --enable-native-access=your.module.name for named modules.

Limitations include:

  • No support for union types
  • Arrays within records are heap-allocated, which may impact performance
  • Not all schema shapes are supported; depends on carrier class evolution in Java

The project does not aim to replace the FFM API but to sit one level above it—reducing boilerplate while preserving explicit memory control and layout fidelity.

When to use it

TypedMemory is suitable for developers working on high-performance systems in Java that require off-heap data structures with native compatibility. Use cases include:

  • Graphics and rendering pipelines
  • Simulation or game engines
  • Native interop layers
  • Binary protocol processing
  • Large structured datasets stored off-heap

It is particularly valuable when type safety, memory layout precision, and developer ergonomics are prioritized without sacrificing low-level control.

Benchmarks are not yet published, but early reports suggest 2x to 5x speedups over traditional approaches.

Similar Articles

More articles like this

Coding 1 min

Visual Studio Code 1.120

Visual Studio Code’s 1.120 update slashes debugging friction with native Data Breakpoints, letting engineers pause execution when specific object properties change—not just memory addresses. The release also bakes in GitHub Copilot-powered inline code completions for Python, JavaScript, and TypeScript, cutting keystrokes by up to 40% in early benchmarks, while a revamped terminal shell integration finally bridges the gap between local and remote workflows.

Coding 1 min

Bild AI (YC W25) Is Hiring Founding Product Engineers

Silicon Valley's Bild AI, a Y Combinator-backed startup, is seeking founding product engineers to spearhead the development of its conversational AI platform, which leverages a novel combination of transformer-based language models and reinforcement learning to drive user engagement and retention. The company's AI stack is built on top of a custom-designed, cloud-agnostic architecture that integrates with popular messaging platforms and APIs. As Bild AI expands its product offerings, it's looking for seasoned engineers to help shape its technical vision.

Coding 1 min

Students Boo Commencement Speaker After She Calls AI Next Industrial Revolution

As AI adoption accelerates, a growing disconnect between industry hype and on-the-ground realities is sparking backlash among students and workers, with a recent commencement speech at the University of Central Florida serving as a flashpoint. The speaker's assertion that AI represents the next industrial revolution was met with jeers and boos from the audience, highlighting a widening gap between the promise of automation and its actual impact on employment.

Coding 1 min

Software engineering may no longer be a lifetime career

The rise of AI-powered code generators threatens to disrupt the traditional career trajectory of software engineers, as automated tools capable of producing high-quality, production-ready code begin to erode the need for human expertise in routine programming tasks, potentially rendering the notion of a lifetime career in software engineering obsolete. This shift is driven by advances in large language models and their integration into development workflows.

Coding 1 min

I Work in Hollywood. Everyone Who Used to Make TV Is Now Training AI

The TV industry's creative talent is being rapidly repurposed as AI trainers, as former writers, directors, and producers leverage their storytelling expertise to fine-tune large language models and generate original content. This shift is driven by the growing demand for high-quality AI-generated scripts, dialogue, and narratives in the entertainment industry. Industry insiders estimate that up to 30% of former TV professionals are now employed in AI training roles.

Coding 1 min

Mythos Finds a Curl Vulnerability

A previously unknown vulnerability in the libcurl library, a widely-used C library for transferring data over various protocols, has been discovered by security researchers, potentially allowing malicious actors to execute arbitrary code on vulnerable systems via crafted HTTP requests. The flaw, which affects curl versions 7.84.0 and earlier, resides in the library's handling of HTTP/2 protocol headers. Exploitation is possible via a specially crafted HTTP/2 request.