Skip to content

Commit

Permalink
Avoid buffer copies in RedisStateMachine #2173
Browse files Browse the repository at this point in the history
Original pull request: #2174
  • Loading branch information
jeffrey (Hongji Ye) authored and mp911de committed Nov 23, 2022
1 parent 803c682 commit 66506f9
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/lettuce/core/protocol/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
setState(LifecycleState.REGISTERED);

buffer = ctx.alloc().buffer(8192 * 8);
rsm = new RedisStateMachine(ctx.alloc());
rsm = new RedisStateMachine();
ctx.fireChannelRegistered();
}

Expand Down
23 changes: 7 additions & 16 deletions src/main/java/io/lettuce/core/protocol/RedisStateMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,6 @@ public String toString() {

private final boolean debugEnabled = logger.isDebugEnabled();

private final ByteBuf responseElementBuffer;

private final AtomicBoolean closed = new AtomicBoolean();

private final Resp2LongProcessor longProcessor = new Resp2LongProcessor();
Expand All @@ -244,8 +242,8 @@ public String toString() {
/**
* Initialize a new instance.
*/
public RedisStateMachine(ByteBufAllocator alloc) {
this.responseElementBuffer = alloc.buffer(1024);
public RedisStateMachine() {

}

public boolean isDiscoverProtocol() {
Expand Down Expand Up @@ -591,7 +589,7 @@ public void reset() {
*/
public void close() {
if (closed.compareAndSet(false, true)) {
responseElementBuffer.release();
// free resources if any
}
}

Expand Down Expand Up @@ -673,18 +671,11 @@ private ByteBuffer readBytes(ByteBuf buffer, int count) {
}

private ByteBuffer readBytes0(ByteBuf buffer, int count) {
final ByteBuffer byteBuffer = buffer.internalNioBuffer(buffer.readerIndex(), count);

ByteBuffer bytes;
responseElementBuffer.clear();

if (responseElementBuffer.capacity() < count) {
responseElementBuffer.capacity(count);
}

buffer.readBytes(responseElementBuffer, count);
bytes = responseElementBuffer.internalNioBuffer(0, count);

return bytes;
// advance reader index
buffer.readerIndex(buffer.readerIndex() + count);
return byteBuffer;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static void afterClass() {
@BeforeEach
final void createStateMachine() {
output = new StatusOutput<>(codec);
rsm = new RedisStateMachine(ByteBufAllocator.DEFAULT);
rsm = new RedisStateMachine();
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static void afterClass() {
@BeforeEach
final void createStateMachine() {
output = new StatusOutput<>(codec);
rsm = new RedisStateMachine(ByteBufAllocator.DEFAULT);
rsm = new RedisStateMachine();
rsm.setProtocolVersion(ProtocolVersion.RESP3);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static void afterClass() {
@BeforeEach
final void createStateMachine() {
output = new StatusOutput<>(codec);
rsm = new RedisStateMachine(ByteBufAllocator.DEFAULT);
rsm = new RedisStateMachine();
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void set(long integer) {

private ByteBuf masterBuffer;

private final RedisStateMachine stateMachine = new RedisStateMachine(ByteBufAllocator.DEFAULT);
private final RedisStateMachine stateMachine = new RedisStateMachine();
private final byte[] payload = ("*3\r\n" + //
"$4\r\n" + //
"LLEN\r\n" + //
Expand Down

0 comments on commit 66506f9

Please sign in to comment.