Skip to content

Commit d9538ea

Browse files
authored
[improve][broker] Improve performance of StatsOutputStream by using ArrayDeque instead of Stack (apache#20808)
1 parent e41883e commit d9538ea

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

pulsar-broker/src/main/java/org/apache/pulsar/utils/StatsOutputStream.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,52 +19,53 @@
1919
package org.apache.pulsar.utils;
2020

2121
import io.netty.buffer.ByteBuf;
22-
import java.util.Stack;
22+
import java.util.ArrayDeque;
23+
import java.util.Deque;
2324
import org.apache.pulsar.common.util.SimpleTextOutputStream;
2425

2526
public class StatsOutputStream extends SimpleTextOutputStream {
26-
private final Stack<Boolean> separators = new Stack<>();
27+
private final Deque<Boolean> separators = new ArrayDeque<>();
2728

2829
public StatsOutputStream(ByteBuf buffer) {
2930
super(buffer);
3031
}
3132

3233
public StatsOutputStream startObject() {
3334
checkSeparator();
34-
separators.push(Boolean.FALSE);
35+
separators.addLast(Boolean.FALSE);
3536
write('{');
3637
return this;
3738
}
3839

3940
public StatsOutputStream startObject(String key) {
4041
checkSeparator();
4142
write('"').writeEncoded(key).write("\":{");
42-
separators.push(Boolean.FALSE);
43+
separators.addLast(Boolean.FALSE);
4344
return this;
4445
}
4546

4647
public StatsOutputStream endObject() {
47-
separators.pop();
48+
separators.removeLast();
4849
write('}');
4950
return this;
5051
}
5152

5253
public StatsOutputStream startList() {
5354
checkSeparator();
54-
separators.push(Boolean.FALSE);
55+
separators.addLast(Boolean.FALSE);
5556
write('[');
5657
return this;
5758
}
5859

5960
public StatsOutputStream startList(String key) {
6061
checkSeparator();
6162
write('"').writeEncoded(key).write("\":[");
62-
separators.push(Boolean.FALSE);
63+
separators.addLast(Boolean.FALSE);
6364
return this;
6465
}
6566

6667
public StatsOutputStream endList() {
67-
separators.pop();
68+
separators.removeLast();
6869
write(']');
6970
return this;
7071
}
@@ -121,10 +122,11 @@ StatsOutputStream writeItem(String s) {
121122
private void checkSeparator() {
122123
if (separators.isEmpty()) {
123124
return;
124-
} else if (separators.peek() == Boolean.TRUE) {
125+
} else if (separators.peekLast() == Boolean.TRUE) {
125126
write(",");
126127
} else {
127-
separators.set(separators.size() - 1, Boolean.TRUE);
128+
separators.pollLast();
129+
separators.addLast(Boolean.TRUE);
128130
}
129131
}
130132
}

pulsar-broker/src/test/java/org/apache/pulsar/utils/StatsOutputStreamTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,26 @@ public String str() {
150150
reset();
151151
return s;
152152
}
153+
154+
@Test
155+
public void testBehaviorOfStatsOutputStreamWithDeque() {
156+
// Create a byte buffer for collecting output
157+
ByteBuf buffer = Unpooled.buffer();
158+
159+
// Create an instance of StatsOutputStream using Deque
160+
StatsOutputStream output = new StatsOutputStream(buffer);
161+
output.startObject()
162+
.writePair("name", "test")
163+
.startList("items")
164+
.writeItem(true)
165+
.writeItem(123L)
166+
.writeItem("sample")
167+
.endList()
168+
.endObject();
169+
170+
// Assert
171+
assertEquals(buffer.toString(java.nio.charset.StandardCharsets.UTF_8),
172+
"{\"name\":\"test\",\"items\":[true,123,\"sample\"]}");
173+
}
174+
153175
}

0 commit comments

Comments
 (0)