From 3ba8eb10b948bbffed96c06f7c7bdede56e819b1 Mon Sep 17 00:00:00 2001 From: skr Date: Fri, 23 Feb 2024 09:41:33 +0530 Subject: [PATCH] Use pre-allocated State's in `RedisStateMachine`, avoiding need for State object allocs #2610 * adds gc and thrpt profiling in RedisStateMachine benchmark * fixes a stale benchmark which caused compilation errors ClusterDistributionChannelWriterBenchmark Original pull request: #2768 --- .../core/protocol/RedisStateMachine.java | 53 ++++++++++--------- ...terDistributionChannelWriterBenchmark.java | 3 +- .../jmh/io/lettuce/core/protocol/JmhMain.java | 17 ++++-- 3 files changed, 42 insertions(+), 31 deletions(-) diff --git a/src/main/java/io/lettuce/core/protocol/RedisStateMachine.java b/src/main/java/io/lettuce/core/protocol/RedisStateMachine.java index 1d446b8117..ac292f0c49 100644 --- a/src/main/java/io/lettuce/core/protocol/RedisStateMachine.java +++ b/src/main/java/io/lettuce/core/protocol/RedisStateMachine.java @@ -37,6 +37,7 @@ * @author Will Glozer * @author Mark Paluch * @author Helly Guo + * @author shikharid */ public class RedisStateMachine { @@ -225,9 +226,27 @@ public String toString() { return sb.toString(); } + void reset() { + this.type = null; + this.count = NOT_FOUND; + } + + /** + * Pre-allocates a State array of given len + * + * @param len len of the states array to be created + * @return array of State's of len size + */ + private static State[] createStates(int len) { + final State[] stack = new State[len]; + for (int i = 0;i < len; ++i) { + stack[i] = new State(); + } + return stack; + } } - private final State[] stack = new State[32]; + private final State[] stack = State.createStates(32); private final boolean debugEnabled = logger.isDebugEnabled(); @@ -290,7 +309,7 @@ public boolean decode(ByteBuf buffer, CommandOutput output, Consumer