Commit 881065a
committed
SwingConsolePane: first cut at batching the output
We want to avoid an excessive amount of queuing to the EDT via
EventQueue.invokeLater calls. This change puts all output events into a
queue, which is fully drained by each Runnable queued up on the EDT.
Concurrent appends push the new output events into the queue, but only
queue up a new Runnable on the EDT if there isn't one already running.
Concurrency issues are always tricky, but I _think_ this logic is
thread-safe. It is OK that an extra Runnable or two might get queued up
on the EDT. The danger is the inverse situation: that the append method
might opt not to queue up a Runnable, even though the currently active
Runnable on the EDT is already winding down. If that happened, the
appearance of some output might be delayed by an arbitrarily lengthy
period, until the next bit of output rolls around...
In the case of SwingConsolePaneBenchmark, this change reduces the
number of threadService.queue calls from >2000 to a mere _2_ calls!
However, the improvement in real time is not what you might expect.
There are three scenarios:
* BATCHED console updates (i.e., with this commit applied).
* THOROUGH console updates, queuing to the EDT on every output event.
(i.e., without this commit applied).
* NAIVE console updates, without queuing anything to the EDT at all.
(i.e., w/o this commit or d86b605).
I ran three trials of SwingConsolePaneBenchmark for each of the
scenarios above. Here are the results (in ms) on my machine:
* BATCHED = 158 159 154
* THOROUGH = 169 176 164
* NAIVE = 999 716 735 (!!)
In all three cases, all output does eventually make it to the console,
and all the assertions in SwingConsolePaneBenchmark actually pass.
So at least on my system (OS X with Apple Java 1.6.0_65), it is not a
matter of correctness, only performance.
There is a downside to behavior with the BATCHED console updates though:
if the number of output events flood the queue faster than it can be
drained on the EDT, then the EDT will lock up indefinitely -- whereas
with the THOROUGH approach, at least you'll continue to see output on
the console, since the EDT gets freed up afterwards every time.1 parent f2ae3fb commit 881065a
File tree
1 file changed
+26
-8
lines changed- src/main/java/org/scijava/ui/swing/console
1 file changed
+26
-8
lines changedLines changed: 26 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| |||
81 | 82 | | |
82 | 83 | | |
83 | 84 | | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
84 | 92 | | |
85 | 93 | | |
86 | 94 | | |
| |||
107 | 115 | | |
108 | 116 | | |
109 | 117 | | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
110 | 122 | | |
111 | 123 | | |
112 | 124 | | |
113 | 125 | | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
121 | 140 | | |
122 | | - | |
123 | 141 | | |
124 | 142 | | |
125 | 143 | | |
| |||
0 commit comments