Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ High-performance Protocol Buffers code generator for Java, optimized for seriali
- **Zero-copy deserialization** using Netty `ByteBuf` (direct and heap memory)
- **Zero heap allocations** — reusable mutable objects, no Builder pattern overhead
- **Lazy string/bytes deserialization** — decoded only on access
- **Optimized string handling** — single-copy ASCII fast path via `sun.misc.Unsafe`
- **Optimized string handling** — single-copy ASCII fast path via `sun.misc.Unsafe`, used only when Netty uses it too
- **Protobuf-compatible JSON serialization and deserialization** — `toJson()` / `parseFromJson()` methods
- **Protobuf TextFormat (de)serialization** — opt-in `toTextFormat()` / `parseFromTextFormat()` for compatibility with `com.google.protobuf.TextFormat`
- **No runtime dependencies** — generated code is self-contained
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,38 +66,44 @@ class LightProtoCodec {
MethodHandle mhPutObject = null;
MethodHandle mhCopyMemory = null;
MethodHandle mhAllocateInstance = null;
try {
Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
Field f = unsafeClass.getDeclaredField("theUnsafe");
f.setAccessible(true);
Object unsafe = f.get(null);

// Use reflection for init-only operations
Method objectFieldOffset = unsafeClass.getMethod("objectFieldOffset", Field.class);
Method arrayBaseOffsetMethod = unsafeClass.getMethod("arrayBaseOffset", Class.class);
Method getObjectMethod = unsafeClass.getMethod("getObject", Object.class, long.class);

offset = (long) objectFieldOffset.invoke(unsafe, String.class.getDeclaredField("value"));
arrayBase = (int) arrayBaseOffsetMethod.invoke(unsafe, byte[].class);
// Detect compact strings: an ASCII string's internal byte[] length
// equals the string length when compact strings are enabled (LATIN1 coder),
// but is 2x the string length when disabled (UTF-16 coder).
byte[] testValue = (byte[]) getObjectMethod.invoke(unsafe, "a", offset);
compactStrings = (testValue.length == 1);

// Create MethodHandles for hot-path operations, bound to the Unsafe instance
MethodHandles.Lookup lookup = MethodHandles.lookup();
mhGetObject = lookup.unreflect(getObjectMethod).bindTo(unsafe);
mhPutObject = lookup.unreflect(
unsafeClass.getMethod("putObject", Object.class, long.class, Object.class)).bindTo(unsafe);
mhCopyMemory = lookup.unreflect(
unsafeClass.getMethod("copyMemory", Object.class, long.class, Object.class, long.class, long.class))
.bindTo(unsafe);
mhAllocateInstance = lookup.unreflect(
unsafeClass.getMethod("allocateInstance", Class.class)).bindTo(unsafe);
hasUnsafe = true;
} catch (Throwable ignore) {
// Fallback to non-Unsafe path
// Only where Netty uses sun.misc.Unsafe too: since JDK 24 the JVM prints a warning on
// the first call to one of its memory-access methods (JEP 498), once, and Netty's own
// calls have printed it already. Where Netty doesn't (by default from Netty 4.2 on
// Java 25), the paths without Unsafe below are taken, and no warning is printed.
if (PlatformDependent.hasUnsafe()) {
try {
Class<?> unsafeClass = Class.forName("sun.misc.Unsafe");
Field f = unsafeClass.getDeclaredField("theUnsafe");
f.setAccessible(true);
Object unsafe = f.get(null);

// Use reflection for init-only operations
Method objectFieldOffset = unsafeClass.getMethod("objectFieldOffset", Field.class);
Method arrayBaseOffsetMethod = unsafeClass.getMethod("arrayBaseOffset", Class.class);
Method getObjectMethod = unsafeClass.getMethod("getObject", Object.class, long.class);

offset = (long) objectFieldOffset.invoke(unsafe, String.class.getDeclaredField("value"));
arrayBase = (int) arrayBaseOffsetMethod.invoke(unsafe, byte[].class);
// Detect compact strings: an ASCII string's internal byte[] length
// equals the string length when compact strings are enabled (LATIN1 coder),
// but is 2x the string length when disabled (UTF-16 coder).
byte[] testValue = (byte[]) getObjectMethod.invoke(unsafe, "a", offset);
compactStrings = (testValue.length == 1);

// Create MethodHandles for hot-path operations, bound to the Unsafe instance
MethodHandles.Lookup lookup = MethodHandles.lookup();
mhGetObject = lookup.unreflect(getObjectMethod).bindTo(unsafe);
mhPutObject = lookup.unreflect(
unsafeClass.getMethod("putObject", Object.class, long.class, Object.class)).bindTo(unsafe);
mhCopyMemory = lookup.unreflect(
unsafeClass.getMethod("copyMemory", Object.class, long.class, Object.class, long.class, long.class))
.bindTo(unsafe);
mhAllocateInstance = lookup.unreflect(
unsafeClass.getMethod("allocateInstance", Class.class)).bindTo(unsafe);
hasUnsafe = true;
} catch (Throwable ignore) {
// Fallback to non-Unsafe path
}
}
HAS_UNSAFE = hasUnsafe;
STRING_VALUE_OFFSET = offset;
Expand Down Expand Up @@ -483,6 +489,7 @@ static int writeRawDouble(byte[] a, int i, double n) {
* at index i. ASCII strings are copied straight from the String's internal
* byte[]; other strings go through the JDK encoder. Returns the index after.
*/
@SuppressWarnings("deprecation")
static int writeRawString(byte[] a, int i, String s, int bytesCount) {
if (s.length() == bytesCount) {
// ASCII fast path: copy the String's internal LATIN1 byte[] directly
Expand All @@ -494,7 +501,9 @@ static int writeRawString(byte[] a, int i, String s, int bytesCount) {
throw new RuntimeException(t);
}
} else {
System.arraycopy(s.getBytes(StandardCharsets.ISO_8859_1), 0, a, i, bytesCount);
// Stores the low byte of each char: for a LATIN1 string, one arraycopy of its
// internal byte[], without the temporary array of getBytes(ISO_8859_1)
s.getBytes(0, bytesCount, a, i);
}
} else {
System.arraycopy(s.getBytes(StandardCharsets.UTF_8), 0, a, i, bytesCount);
Expand Down Expand Up @@ -584,7 +593,11 @@ static int writeRawString(java.nio.ByteBuffer nb, int i, String s, int bytesCoun
throw new RuntimeException(t);
}
} else {
nb.put(i, s.getBytes(StandardCharsets.ISO_8859_1), 0, bytesCount);
// One put per char, without a temporary array: faster than staging the bytes
// in one, at any length
for (int k = 0; k < bytesCount; k++) {
nb.put(i + k, (byte) s.charAt(k));
}
}
} else {
nb.put(i, s.getBytes(StandardCharsets.UTF_8), 0, bytesCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,6 @@ public void testAllocationFreeLargeWritesBytes() throws Exception {

@Test
public void testAllocationFreeLargeWritesTopicList() throws Exception {
// With -XX:-CompactStrings the ASCII fast path cannot bulk-put the String's
// internal byte[] and copies each string through a temporary array.
assumeTrue(!ManagementFactory.getRuntimeMXBean().getInputArguments().contains("-XX:-CompactStrings"));

List<String> topics = new ArrayList<>();
String base = "persistent://public/default/" + "t".repeat(520) + "-";
for (int i = 0; i < 8192; i++) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* Copyright 2026 StreamNative
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.streamnative.lightproto.tests;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.Unpooled;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import org.junit.jupiter.api.Test;

/**
* Generated code uses sun.misc.Unsafe only where Netty does. The first call to one of its
* memory-access methods prints a warning naming the caller (JEP 498, the default since
* JDK 24), which Netty's own calls have then printed already.
*/
public class NoUnsafeTest {

@Test
public void testNoUnsafeWarningWithoutNettyUnsafe() throws Exception {
assumeTrue(Runtime.version().feature() >= 23, "--sun-misc-unsafe-memory-access exists since JDK 23");
ProcessBuilder pb = new ProcessBuilder(
Paths.get(System.getProperty("java.home"), "bin", "java").toString(),
// The default from Netty 4.2 on Java 25
"-Dio.netty.noUnsafe=true",
"--sun-misc-unsafe-memory-access=warn",
"-cp", System.getProperty("java.class.path"),
NoUnsafeTest.class.getName());
pb.redirectErrorStream(true);
Process p = pb.start();
String output = new String(p.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
assertEquals(0, p.waitFor(), output);
assertFalse(output.contains("sun.misc.Unsafe"), output);
}

/** Writes and reads strings through every codec path, in the JVM that the test starts. */
public static void main(String[] args) {
String ascii = "persistent://tenant/namespace/topic-" + "t".repeat(2000);
String utf8 = "UTF16 Ελληνικά Русский 日本語";
S small = new S().setId("xyz");
small.addName(utf8);
S large = new S().setId(ascii);
large.addName(utf8);
for (S s : new S[]{small, large}) {
// Heap buffers are written in place and parsed from their array; direct ones are
// written through a scratch array or, above NIO_WRITE_MIN, their NIO view
for (ByteBuf b : new ByteBuf[]{Unpooled.buffer(), PooledByteBufAllocator.DEFAULT.directBuffer()}) {
s.writeTo(b);
S parsed = new S();
parsed.parseFrom(b, b.readableBytes());
check(s, parsed);
b.release();
}
// A gRPC message received in one direct buffer is parsed through a view
byte[] serialized = s.toByteArray();
ByteBuffer nio = ByteBuffer.allocateDirect(serialized.length).put(serialized).flip();
S parsed = new S();
parsed.parseFrom(LightProtoCodec.PARSE_VIEW.get().reset(nio), serialized.length);
check(s, parsed);
}
}

private static void check(S expected, S actual) {
if (!expected.getId().equals(actual.getId()) || !expected.getNameAt(0).equals(actual.getNameAt(0))) {
throw new AssertionError("strings differ after a round trip");
}
}
}
Loading