Skip to content

Commit

Permalink
Merge pull request #9789 from galderz/t_empty_buffer_netty_reset_and_…
Browse files Browse the repository at this point in the history
…substitute_9750

Move empty byte buffer creation direct buffer to runtime
  • Loading branch information
gsmet authored Jun 8, 2020
2 parents b0a0639 + 23e8e8f commit e98b2ff
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import io.quarkus.deployment.builditem.nativeimage.NativeImageConfigBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageSystemPropertyBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.RuntimeReinitializedClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.UnsafeAccessedFieldBuildItem;
import io.quarkus.netty.BossEventLoopGroup;
import io.quarkus.netty.MainEventLoopGroup;
import io.quarkus.netty.runtime.EmptyByteBufStub;
import io.quarkus.netty.runtime.NettyRecorder;

class NettyProcessor {
Expand Down Expand Up @@ -218,4 +220,10 @@ public List<UnsafeAccessedFieldBuildItem> unsafeAccessedFields() {
new UnsafeAccessedFieldBuildItem("sun.nio.ch.SelectorImpl", "selectedKeys"),
new UnsafeAccessedFieldBuildItem("sun.nio.ch.SelectorImpl", "publicSelectedKeys"));
}

@BuildStep
RuntimeInitializedClassBuildItem runtimeInitBcryptUtil() {
// this holds a direct allocated byte buffer that needs to be initialised at run time
return new RuntimeInitializedClassBuildItem(EmptyByteBufStub.class.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.quarkus.netty.runtime;

import java.nio.ByteBuffer;

import io.netty.util.internal.PlatformDependent;

public final class EmptyByteBufStub {
private static final ByteBuffer EMPTY_BYTE_BUFFER = ByteBuffer.allocateDirect(0);
private static final long EMPTY_BYTE_BUFFER_ADDRESS;

static {
long emptyByteBufferAddress = 0;
try {
if (PlatformDependent.hasUnsafe()) {
emptyByteBufferAddress = PlatformDependent.directBufferAddress(EMPTY_BYTE_BUFFER);
}
} catch (Throwable t) {
// Ignore
}
EMPTY_BYTE_BUFFER_ADDRESS = emptyByteBufferAddress;
}

public static ByteBuffer emptyByteBuffer() {
return EMPTY_BYTE_BUFFER;
}

public static long emptyByteBufferAddress() {
return EMPTY_BYTE_BUFFER_ADDRESS;
}

private EmptyByteBufStub() {
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.netty.runtime.graal;

import java.nio.ByteBuffer;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.cert.X509Certificate;
Expand Down Expand Up @@ -35,6 +36,7 @@
import io.netty.util.concurrent.GlobalEventExecutor;
import io.netty.util.internal.logging.InternalLoggerFactory;
import io.netty.util.internal.logging.JdkLoggerFactory;
import io.quarkus.netty.runtime.EmptyByteBufStub;

/**
* This substitution avoid having loggers added to the build
Expand Down Expand Up @@ -389,6 +391,48 @@ static Class<?> tryToLoadClass(final ClassLoader loader, final Class<?> helper)

}

@TargetClass(className = "io.netty.buffer.EmptyByteBuf")
final class Target_io_netty_buffer_EmptyByteBuf {

@Alias
@RecomputeFieldValue(kind = RecomputeFieldValue.Kind.Reset)
private static ByteBuffer EMPTY_BYTE_BUFFER;

@Alias
@RecomputeFieldValue(kind = RecomputeFieldValue.Kind.Reset)
private static long EMPTY_BYTE_BUFFER_ADDRESS;

@Substitute
public ByteBuffer nioBuffer() {
return EmptyByteBufStub.emptyByteBuffer();
}

@Substitute
public ByteBuffer[] nioBuffers() {
return new ByteBuffer[] { EmptyByteBufStub.emptyByteBuffer() };
}

@Substitute
public ByteBuffer internalNioBuffer(int index, int length) {
return EmptyByteBufStub.emptyByteBuffer();
}

@Substitute
public boolean hasMemoryAddress() {
return EmptyByteBufStub.emptyByteBufferAddress() != 0;
}

@Substitute
public long memoryAddress() {
if (hasMemoryAddress()) {
return EmptyByteBufStub.emptyByteBufferAddress();
} else {
throw new UnsupportedOperationException();
}
}

}

class NettySubstitutions {

}

0 comments on commit e98b2ff

Please sign in to comment.