Skip to content

Commit

Permalink
Polishing #2445
Browse files Browse the repository at this point in the history
Refine list preallocation size.
  • Loading branch information
mp911de committed Jul 17, 2023
1 parent 0009592 commit 170d966
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/main/java/io/lettuce/core/dynamic/SimpleBatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ protected BatchTasks flush(boolean forcedFlush) {

boolean defaultFlush = false;

List<RedisCommand<?, ?, ?>> commands = new ArrayList<>(Math.max(batchSize, 10));
List<RedisCommand<?, ?, ?>> commands = newDrainTarget();

while (flushing.compareAndSet(false, true)) {

Expand Down Expand Up @@ -147,7 +147,7 @@ protected BatchTasks flush(boolean forcedFlush) {

private List<RedisCommand<Object, Object, Object>> prepareForceFlush() {

List<RedisCommand<Object, Object, Object>> batch = new ArrayList<>(Math.max(batchSize, 10));
List<RedisCommand<Object, Object, Object>> batch = newDrainTarget();

while (!queue.isEmpty()) {

Expand All @@ -163,7 +163,7 @@ private List<RedisCommand<Object, Object, Object>> prepareForceFlush() {

private List<RedisCommand<Object, Object, Object>> prepareDefaultFlush(int consume) {

List<RedisCommand<Object, Object, Object>> batch = new ArrayList<>(Math.max(consume, 10));
List<RedisCommand<Object, Object, Object>> batch = newDrainTarget();

while ((batch.size() < consume || consume == -1) && !queue.isEmpty()) {

Expand All @@ -177,4 +177,7 @@ private List<RedisCommand<Object, Object, Object>> prepareDefaultFlush(int consu
return batch;
}

private <T> ArrayList<T> newDrainTarget() {
return new ArrayList<>(Math.max(0, Math.min(batchSize, queue.size())));
}
}

0 comments on commit 170d966

Please sign in to comment.