Skip to content

Commit

Permalink
Solved the latency problem when reading PipedReader
Browse files Browse the repository at this point in the history
  • Loading branch information
luontola committed May 16, 2010
1 parent e8893e1 commit 49d0ff8
Showing 1 changed file with 2 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,10 @@

public class MulticastPipe extends Writer {

// TODO: PipedReader lags with the default 1024 size buffer, when seeing sbt's "actions" or "help"
// Would some other data structure have better latency?
// Profiling shows that java.io.PipedReader.read() waits randomly for one second (at other times it's quick).
// Based on PipedReader's source code it does that when the buffer is empty.
private static final int PIPE_BUFFER_SIZE = 8 * 1024;

private final List<PipedWriter> subscribers = new CopyOnWriteArrayList<PipedWriter>();

public Reader subscribe() throws IOException {
PipedReader r = new PipedReader(PIPE_BUFFER_SIZE);
PipedReader r = new PipedReader();
subscribers.add(new PipedWriter(r));
return r;
}
Expand All @@ -32,6 +26,7 @@ public void write(char[] cbuf, int off, int len) throws IOException {
for (PipedWriter w : subscribers) {
try {
w.write(cbuf, off, len);
w.flush();
} catch (IOException e) {
unsubscribe(w);
}
Expand Down

0 comments on commit 49d0ff8

Please sign in to comment.