Skip to content

Commit 4386150

Browse files
committed
perf(jsonrpc): buffer output writes in StreamMessageConsumer
Wrap provided OutputStream in BufferedOutputStream to amortize small writes; improves throughput by reducing syscalls. Centralize wrapping via setOutput to avoid double wrapping.
1 parent 79e2cf7 commit 4386150

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

org.eclipse.lsp4j.jsonrpc/src/main/java/org/eclipse/lsp4j/jsonrpc/json/StreamMessageConsumer.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
******************************************************************************/
1212
package org.eclipse.lsp4j.jsonrpc.json;
1313

14+
import java.io.BufferedOutputStream;
1415
import java.io.IOException;
1516
import java.io.OutputStream;
1617
import java.nio.charset.StandardCharsets;
@@ -28,8 +29,7 @@ public class StreamMessageConsumer implements MessageConsumer, MessageConstants
2829
private final MessageJsonHandler jsonHandler;
2930

3031
private final Object outputLock = new Object();
31-
32-
private OutputStream output;
32+
private BufferedOutputStream output;
3333

3434
public StreamMessageConsumer(MessageJsonHandler jsonHandler) {
3535
this(null, StandardCharsets.UTF_8.name(), jsonHandler);
@@ -40,7 +40,7 @@ public StreamMessageConsumer(OutputStream output, MessageJsonHandler jsonHandler
4040
}
4141

4242
public StreamMessageConsumer(OutputStream output, String encoding, MessageJsonHandler jsonHandler) {
43-
this.output = output;
43+
setOutput(output);
4444
this.encoding = encoding;
4545
this.jsonHandler = jsonHandler;
4646
}
@@ -50,7 +50,13 @@ public OutputStream getOutput() {
5050
}
5151

5252
public void setOutput(OutputStream output) {
53-
this.output = output;
53+
synchronized (outputLock) {
54+
this.output = output == null //
55+
? null
56+
: output instanceof BufferedOutputStream //
57+
? (BufferedOutputStream) output
58+
: new BufferedOutputStream(output);
59+
}
5460
}
5561

5662
@Override

0 commit comments

Comments
 (0)