Skip to content

Commit

Permalink
Add a flush for the GUI print cache.
Browse files Browse the repository at this point in the history
I tested the performance impact of this and for the GUI it seems to
be neutral, but the CLI takes a small performance hit.
  • Loading branch information
TheThirdOne committed Sep 28, 2020
1 parent 6ed3068 commit 97fe0d0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/rars/simulator/Simulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ private void startExecution() {
private void stopExecution(boolean done, Reason reason) {
this.done = done;
this.constructReturnReason = reason;
SystemIO.flush(true);
if (done) SystemIO.resetFiles(); // close any files opened in the process of simulating
Simulator.getInstance().notifyObserversOfExecution(new SimulatorNotice(SimulatorNotice.SIMULATOR_STOP,
maxSteps, (Globals.getGui() != null || Globals.runSpeedPanelExists)?RunSpeedPanel.getInstance().getRunSpeed():RunSpeedPanel.UNLIMITED_SPEED,
Expand Down Expand Up @@ -386,6 +387,7 @@ public void run() {
// Volatile variable initialized false but can be set true by the main thread.
// Used to stop or pause a running program. See stopSimulation() above.
while (!stop) {
SystemIO.flush(false);
// Perform the RISCV instruction in synchronized block. If external threads agree
// to access memory and registers only through synchronized blocks on same
// lock variable, then full (albeit heavy-handed) protection of memory and
Expand Down
15 changes: 15 additions & 0 deletions src/rars/util/SystemIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ private static BufferedReader getInputReader() {
return inputReader;
}

// The GUI doesn't handle lots of small messages well so I added this hacky way of buffering
// Currently it checks to flush every instruction run
private static String buffer = "";
private static long lasttime = 0;
private static void print2Gui(String output){
Expand All @@ -453,6 +455,19 @@ private static void print2Gui(String output){
buffer += output;
}
}
/**
* Flush stdout cache
* Makes sure that messages don't get stuck in the print2Gui buffer for too long.
*/
public static void flush(boolean force) {
long time = System.currentTimeMillis();
if (buffer != "" && (force || time > lasttime)){
Globals.getGui().getMessagesPane().postRunMessage(buffer);
buffer = "";
lasttime = time + 100;
}
}


public static Data swapData(Data in){
Data temp = new Data(false);
Expand Down

0 comments on commit 97fe0d0

Please sign in to comment.