Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid buffer copies in RedisStateMachine #2174

Closed
wants to merge 3 commits into from
Closed
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 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