From 7e720d3cbf37d9e4b5218b1ec921e42f37c26a9a Mon Sep 17 00:00:00 2001 From: Heyuan Liu Date: Sat, 14 Jan 2023 20:41:59 +0800 Subject: [PATCH 1/2] WFSSL-112 Use PooledByteBuffer to fix the performance issue caused by ByteBuffer allocation --- .../org/wildfly/openssl/OpenSSLEngine.java | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/java/src/main/java/org/wildfly/openssl/OpenSSLEngine.java b/java/src/main/java/org/wildfly/openssl/OpenSSLEngine.java index 2c30bee..74a4ef7 100644 --- a/java/src/main/java/org/wildfly/openssl/OpenSSLEngine.java +++ b/java/src/main/java/org/wildfly/openssl/OpenSSLEngine.java @@ -254,7 +254,9 @@ private int writePlaintextData(final ByteBuffer src) { return sslWrote; } } else { - ByteBuffer buf = ByteBuffer.allocateDirect(len); + DefaultByteBufferPool.PooledByteBuffer pooledByteBuffer = DefaultByteBufferPool.DIRECT_POOL.allocate(); + ByteBuffer buf = pooledByteBuffer.getBuffer(); + buf.limit(len); try { final long addr = memoryAddress(buf); @@ -271,8 +273,7 @@ private int writePlaintextData(final ByteBuffer src) { src.position(pos); } } finally { - buf.clear(); - ByteBufferUtils.cleanDirectBuffer(buf); + pooledByteBuffer.close(); } } @@ -293,7 +294,9 @@ private int writeEncryptedData(final ByteBuffer src) { return netWrote; } } else { - ByteBuffer buf = ByteBuffer.allocateDirect(len); + DefaultByteBufferPool.PooledByteBuffer pooledByteBuffer = DefaultByteBufferPool.DIRECT_POOL.allocate(); + ByteBuffer buf = pooledByteBuffer.getBuffer(); + buf.limit(len); try { final long addr = memoryAddress(buf); @@ -307,8 +310,7 @@ private int writeEncryptedData(final ByteBuffer src) { src.position(pos); } } finally { - buf.clear(); - ByteBufferUtils.cleanDirectBuffer(buf); + pooledByteBuffer.close(); } } @@ -344,7 +346,9 @@ private int readPlaintextData(final ByteBuffer dst) throws SSLException { final int pos = dst.position(); final int limit = dst.limit(); final int len = Math.min(MAX_ENCRYPTED_PACKET_LENGTH, limit - pos); - final ByteBuffer buf = ByteBuffer.allocateDirect(len); + DefaultByteBufferPool.PooledByteBuffer pooledByteBuffer = DefaultByteBufferPool.DIRECT_POOL.allocate(); + final ByteBuffer buf = pooledByteBuffer.getBuffer(); + buf.limit(len); try { final long addr = memoryAddress(buf); @@ -368,8 +372,7 @@ private int readPlaintextData(final ByteBuffer dst) throws SSLException { } } } finally { - buf.clear(); - ByteBufferUtils.cleanDirectBuffer(buf); + pooledByteBuffer.close(); } } @@ -389,7 +392,9 @@ private int readEncryptedData(final ByteBuffer dst, final int pending) { return bioRead; } } else { - final ByteBuffer buf = ByteBuffer.allocateDirect(pending); + DefaultByteBufferPool.PooledByteBuffer pooledByteBuffer = DefaultByteBufferPool.DIRECT_POOL.allocate(); + final ByteBuffer buf = pooledByteBuffer.getBuffer(); + buf.limit(pending); try { final long addr = memoryAddress(buf); @@ -403,8 +408,7 @@ private int readEncryptedData(final ByteBuffer dst, final int pending) { return bioRead; } } finally { - buf.clear(); - ByteBufferUtils.cleanDirectBuffer(buf); + pooledByteBuffer.close(); } } From f4bfa6f362c4a5ab7fda9a0f013f84eb64ac7f30 Mon Sep 17 00:00:00 2001 From: Heyuan Liu Date: Sun, 29 Jan 2023 13:17:59 +0800 Subject: [PATCH 2/2] WFSSL-112 Add system property to use pooled byte buffer or allocated directly --- .../org/wildfly/openssl/OpenSSLEngine.java | 72 ++++++++++++++----- 1 file changed, 56 insertions(+), 16 deletions(-) diff --git a/java/src/main/java/org/wildfly/openssl/OpenSSLEngine.java b/java/src/main/java/org/wildfly/openssl/OpenSSLEngine.java index 74a4ef7..40fb8dd 100644 --- a/java/src/main/java/org/wildfly/openssl/OpenSSLEngine.java +++ b/java/src/main/java/org/wildfly/openssl/OpenSSLEngine.java @@ -61,6 +61,7 @@ public final class OpenSSLEngine extends SSLEngine { } static final int MAX_PLAINTEXT_LENGTH = 16 * 1024; // 2^14 + static final boolean USE_POOLED_BYTEBUFFER = Boolean.getBoolean("org.wildfly.openssl.use-pooled-buffer"); private static final int MAX_COMPRESSED_LENGTH = MAX_PLAINTEXT_LENGTH + 1024; private static final int MAX_CIPHERTEXT_LENGTH = MAX_COMPRESSED_LENGTH + 1024; @@ -254,9 +255,8 @@ private int writePlaintextData(final ByteBuffer src) { return sslWrote; } } else { - DefaultByteBufferPool.PooledByteBuffer pooledByteBuffer = DefaultByteBufferPool.DIRECT_POOL.allocate(); - ByteBuffer buf = pooledByteBuffer.getBuffer(); - buf.limit(len); + final ByteBufferAllocator allocator = getByteBufferAllocator(); + final ByteBuffer buf = allocator.allocate(len); try { final long addr = memoryAddress(buf); @@ -273,7 +273,7 @@ private int writePlaintextData(final ByteBuffer src) { src.position(pos); } } finally { - pooledByteBuffer.close(); + allocator.close(); } } @@ -294,9 +294,8 @@ private int writeEncryptedData(final ByteBuffer src) { return netWrote; } } else { - DefaultByteBufferPool.PooledByteBuffer pooledByteBuffer = DefaultByteBufferPool.DIRECT_POOL.allocate(); - ByteBuffer buf = pooledByteBuffer.getBuffer(); - buf.limit(len); + final ByteBufferAllocator allocator = getByteBufferAllocator(); + final ByteBuffer buf = allocator.allocate(len); try { final long addr = memoryAddress(buf); @@ -310,7 +309,7 @@ private int writeEncryptedData(final ByteBuffer src) { src.position(pos); } } finally { - pooledByteBuffer.close(); + allocator.close(); } } @@ -346,9 +345,8 @@ private int readPlaintextData(final ByteBuffer dst) throws SSLException { final int pos = dst.position(); final int limit = dst.limit(); final int len = Math.min(MAX_ENCRYPTED_PACKET_LENGTH, limit - pos); - DefaultByteBufferPool.PooledByteBuffer pooledByteBuffer = DefaultByteBufferPool.DIRECT_POOL.allocate(); - final ByteBuffer buf = pooledByteBuffer.getBuffer(); - buf.limit(len); + final ByteBufferAllocator allocator = getByteBufferAllocator(); + final ByteBuffer buf = allocator.allocate(len); try { final long addr = memoryAddress(buf); @@ -372,7 +370,7 @@ private int readPlaintextData(final ByteBuffer dst) throws SSLException { } } } finally { - pooledByteBuffer.close(); + allocator.close(); } } @@ -392,9 +390,8 @@ private int readEncryptedData(final ByteBuffer dst, final int pending) { return bioRead; } } else { - DefaultByteBufferPool.PooledByteBuffer pooledByteBuffer = DefaultByteBufferPool.DIRECT_POOL.allocate(); - final ByteBuffer buf = pooledByteBuffer.getBuffer(); - buf.limit(pending); + final ByteBufferAllocator allocator = getByteBufferAllocator(); + final ByteBuffer buf = allocator.allocate(pending); try { final long addr = memoryAddress(buf); @@ -408,7 +405,7 @@ private int readEncryptedData(final ByteBuffer dst, final int pending) { return bioRead; } } finally { - pooledByteBuffer.close(); + allocator.close(); } } @@ -1610,4 +1607,47 @@ private Set getSupportedProtocolsSet() { } } + private interface ByteBufferAllocator { + ByteBuffer allocate(final int length); + + void close(); + } + + private ByteBufferAllocator getByteBufferAllocator() { + if (USE_POOLED_BYTEBUFFER) { + return new ByteBufferAllocator() { + final DefaultByteBufferPool.PooledByteBuffer pooledByteBuffer = DefaultByteBufferPool.DIRECT_POOL.allocate(); + + @Override + public ByteBuffer allocate(int length) { + ByteBuffer buf = pooledByteBuffer.getBuffer(); + buf.limit(length); + return buf; + } + + @Override + public void close() { + pooledByteBuffer.close(); + } + }; + } else { + return new ByteBufferAllocator() { + ByteBuffer buf; + + @Override + public ByteBuffer allocate(int length) { + buf = ByteBuffer.allocateDirect(length); + return buf; + } + + @Override + public void close() { + if (buf != null) { + buf.clear(); + ByteBufferUtils.cleanDirectBuffer(buf); + } + } + }; + } + } }