GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Optimize wrap buffer cumulation in SslHandler and don't mutate input … · netty/netty@20a11b3

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handler/src
    • main/java/io/netty/handler/ssl
      • SslHandlerCoalescingBufferQueue.java
    • test/java/io/netty/handler/ssl
      • SslHandlerCoalescingBufferQueueTest.java
  • transport/src/main/java/io/netty/channel
    • AbstractCoalescingBufferQueue.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎handler/src/main/java/io/netty/handler/ssl/SslHandlerCoalescingBufferQueue.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/ssl/SslHandlerCoalescingBufferQueue.java
+16-27Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,38 +42,27 @@ abstract class SslHandlerCoalescingBufferQueue extends AbstractCoalescingBufferQ
4242

4343
@Override
4444
protected ByteBuf compose(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf next) {
45-
final int wrapDataSize = wrapDataSize();
46-
if (cumulation instanceof CompositeByteBuf) {
47-
CompositeByteBuf composite = (CompositeByteBuf) cumulation;
48-
int numComponents = composite.numComponents();
49-
if (numComponents == 0 ||
50-
!attemptCopyToCumulation(composite.internalComponent(numComponents - 1), next, wrapDataSize)) {
51-
composite.addComponent(true, next);
52-
}
53-
return composite;
54-
}
55-
return attemptCopyToCumulation(cumulation, next, wrapDataSize) ? cumulation :
45+
return attemptCopyToCumulation(cumulation, next, wrapDataSize()) ? cumulation :
5646
copyAndCompose(alloc, cumulation, next);
5747
}
5848

5949
@Override
60-
protected ByteBuf composeFirst(ByteBufAllocator allocator, ByteBuf first) {
61-
if (first instanceof CompositeByteBuf) {
62-
CompositeByteBuf composite = (CompositeByteBuf) first;
63-
if (wantsDirectBuffer) {
64-
first = allocator.directBuffer(composite.readableBytes());
65-
} else {
66-
first = allocator.heapBuffer(composite.readableBytes());
67-
}
68-
try {
69-
first.writeBytes(composite);
70-
} catch (Throwable cause) {
71-
first.release();
72-
PlatformDependent.throwException(cause);
73-
}
74-
composite.release();
50+
protected ByteBuf composeFirst(ByteBufAllocator allocator, ByteBuf first, int bufferSize) {
51+
final ByteBuf newFirst;
52+
if (wantsDirectBuffer) {
53+
newFirst = allocator.directBuffer(bufferSize);
54+
} else {
55+
newFirst = allocator.heapBuffer(bufferSize);
56+
}
57+
try {
58+
newFirst.writeBytes(first);
59+
} catch (Throwable cause) {
60+
newFirst.release();
61+
PlatformDependent.throwException(cause);
7562
}
76-
return first;
63+
assert !first.isReadable();
64+
first.release();
65+
return newFirst;
7766
}
7867

7968
@Override
Collapse file

‎handler/src/test/java/io/netty/handler/ssl/SslHandlerCoalescingBufferQueueTest.java‎

Copy file name to clipboardExpand all lines: handler/src/test/java/io/netty/handler/ssl/SslHandlerCoalescingBufferQueueTest.java
+76-18Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,82 @@
2121
import io.netty.channel.ChannelPromise;
2222
import io.netty.channel.embedded.EmbeddedChannel;
2323
import org.junit.jupiter.params.ParameterizedTest;
24-
import org.junit.jupiter.params.provider.ValueSource;
24+
import org.junit.jupiter.params.provider.EnumSource;
2525

2626
import java.nio.ByteBuffer;
27+
import java.util.function.Function;
28+
import java.util.function.Supplier;
2729

2830
import static org.junit.jupiter.api.Assertions.assertEquals;
2931
import static org.junit.jupiter.api.Assertions.assertFalse;
3032
import static org.junit.jupiter.api.Assertions.assertTrue;
3133

3234
public class SslHandlerCoalescingBufferQueueTest {
35+
static final Supplier<ByteBuf> BYTEBUF_SUPPLIER = new Supplier<ByteBuf>() {
36+
@Override
37+
public ByteBuf get() {
38+
ByteBuf buf = Unpooled.directBuffer(128);
39+
buf.writeLong(1);
40+
buf.writeLong(2);
41+
return buf;
42+
}
43+
};
44+
static final Function<ByteBuf, ByteBuf> NO_WRAPPER = new Function<ByteBuf, ByteBuf>() {
45+
@Override
46+
public ByteBuf apply(ByteBuf byteBuf) {
47+
return byteBuf;
48+
}
49+
};
50+
static final Function<ByteBuf, ByteBuf> DUPLICATE_WRAPPER = new Function<ByteBuf, ByteBuf>() {
51+
@Override
52+
public ByteBuf apply(ByteBuf byteBuf) {
53+
return byteBuf.duplicate();
54+
}
55+
};
56+
static final Function<ByteBuf, ByteBuf> SLICE_WRAPPER = new Function<ByteBuf, ByteBuf>() {
57+
@Override
58+
public ByteBuf apply(ByteBuf byteBuf) {
59+
return byteBuf.slice();
60+
}
61+
};
62+
static ByteBuffer createNioBuffer() {
63+
ByteBuffer nioBuffer = ByteBuffer.allocateDirect(128);
64+
nioBuffer.putLong(1);
65+
nioBuffer.putLong(2);
66+
nioBuffer.position(nioBuffer.limit());
67+
nioBuffer.flip();
68+
return nioBuffer;
69+
}
70+
71+
enum CumulationTestScenario {
72+
BASIC_NIO_BUFFER(new Supplier() {
73+
@Override
74+
public ByteBuf get() {
75+
return Unpooled.wrappedBuffer(createNioBuffer());
76+
}
77+
}, NO_WRAPPER),
78+
READ_ONLY_AND_DUPLICATE_NIO_BUFFER(new Supplier() {
79+
@Override
80+
public ByteBuf get() {
81+
return Unpooled.wrappedBuffer(createNioBuffer().asReadOnlyBuffer());
82+
}
83+
}, DUPLICATE_WRAPPER),
84+
BASIC_DIRECT_BUFFER(BYTEBUF_SUPPLIER, NO_WRAPPER),
85+
DUPLICATE_DIRECT_BUFFER(BYTEBUF_SUPPLIER, DUPLICATE_WRAPPER),
86+
SLICED_DIRECT_BUFFER(BYTEBUF_SUPPLIER, SLICE_WRAPPER);
87+
88+
final Supplier<ByteBuf> bufferSupplier;
89+
final Function<ByteBuf, ByteBuf> bufferWrapper;
90+
91+
CumulationTestScenario(Supplier<ByteBuf> bufferSupplier, Function<ByteBuf, ByteBuf> bufferWrapper) {
92+
this.bufferSupplier = bufferSupplier;
93+
this.bufferWrapper = bufferWrapper;
94+
}
95+
}
3396

3497
@ParameterizedTest
35-
@ValueSource(booleans = { true, false })
36-
public void testCumulation(boolean readOnlyAndDuplicate) {
98+
@EnumSource(CumulationTestScenario.class)
99+
public void testCumulation(CumulationTestScenario testScenario) {
37100
EmbeddedChannel channel = new EmbeddedChannel();
38101
SslHandlerCoalescingBufferQueue queue = new SslHandlerCoalescingBufferQueue(channel, 16, false) {
39102
@Override
@@ -42,34 +105,29 @@ protected int wrapDataSize() {
42105
}
43106
};
44107

45-
ByteBuffer nioBuffer = ByteBuffer.allocateDirect(128);
46-
nioBuffer.putLong(0);
47-
nioBuffer.putLong(0);
48-
nioBuffer.putLong(0);
49-
nioBuffer.flip();
50-
ByteBuf first;
51-
if (readOnlyAndDuplicate) {
52-
first = Unpooled.wrappedBuffer(nioBuffer.asReadOnlyBuffer()).duplicate();
53-
first.writerIndex(8);
54-
} else {
55-
first = Unpooled.wrappedBuffer(nioBuffer);
56-
first.writerIndex(8);
57-
}
108+
ByteBuf original = testScenario.bufferSupplier.get();
109+
original.writerIndex(8);
110+
ByteBuf first = testScenario.bufferWrapper.apply(original);
111+
first.retain();
58112
queue.add(first);
59-
ByteBuf second = Unpooled.copyLong(1);
113+
ByteBuf second = Unpooled.copyLong(3);
60114
queue.add(second);
61115

62116
ChannelPromise promise = channel.newPromise();
63117
assertFalse(queue.isEmpty());
64118
ByteBuf buffer = queue.remove(UnpooledByteBufAllocator.DEFAULT, 128, promise);
65119
try {
66120
assertEquals(16, buffer.readableBytes());
67-
assertEquals(0, buffer.readLong());
68121
assertEquals(1, buffer.readLong());
122+
assertEquals(3, buffer.readLong());
69123
} finally {
70124
buffer.release();
71125
}
72126
assertTrue(queue.isEmpty());
127+
assertEquals(8, original.writerIndex());
128+
original.writerIndex(original.capacity());
129+
assertEquals(2, original.getLong(8));
130+
first.release();
73131
assertEquals(0, first.refCnt());
74132
assertEquals(0, second.refCnt());
75133
}
Collapse file

‎transport/src/main/java/io/netty/channel/AbstractCoalescingBufferQueue.java‎

Copy file name to clipboardExpand all lines: transport/src/main/java/io/netty/channel/AbstractCoalescingBufferQueue.java
+17-3Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,10 @@ public final ByteBuf remove(ByteBufAllocator alloc, int bytes, ChannelPromise ag
181181

182182
bytes -= bufferBytes;
183183
if (toReturn == null) {
184-
// if there are no more bytes in the queue after this, there's no reason to compose
185-
toReturn = bufferBytes == readableBytes
184+
// if there are no more bytes to read, there's no reason to compose
185+
toReturn = bytes == 0
186186
? entryBuffer
187-
: composeFirst(alloc, entryBuffer);
187+
: composeFirst(alloc, entryBuffer, bufferBytes + bytes);
188188
} else {
189189
toReturn = compose(alloc, toReturn, entryBuffer);
190190
}
@@ -333,7 +333,21 @@ protected final ByteBuf copyAndCompose(ByteBufAllocator alloc, ByteBuf cumulatio
333333
/**
334334
* Calculate the first {@link ByteBuf} which will be used in subsequent calls to
335335
* {@link #compose(ByteBufAllocator, ByteBuf, ByteBuf)}.
336+
* @param bufferSize the optimal size of the buffer needed for cumulation
337+
* @return the first buffer
336338
*/
339+
protected ByteBuf composeFirst(ByteBufAllocator allocator, ByteBuf first, int bufferSize) {
340+
return composeFirst(allocator, first);
341+
}
342+
343+
/**
344+
* Calculate the first {@link ByteBuf} which will be used in subsequent calls to
345+
* {@link #compose(ByteBufAllocator, ByteBuf, ByteBuf)}.
346+
* This method is deprecated and will be removed in the future. Implementing classes should
347+
* override {@link #composeFirst(ByteBufAllocator, ByteBuf, int)} instead.
348+
* @deprecated Use {AbstractCoalescingBufferQueue#composeFirst(ByteBufAllocator, ByteBuf, int)}
349+
*/
350+
@Deprecated
337351
protected ByteBuf composeFirst(ByteBufAllocator allocator, ByteBuf first) {
338352
return first;
339353
}

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.