Skip to content

Commit

Permalink
Fix too slow "End", "Ctrl-V", etc. by omitting unnecessary lz-analyze
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiraoka committed Jun 5, 2018
1 parent e9ba00b commit 69dedf4
Showing 1 changed file with 49 additions and 5 deletions.
54 changes: 49 additions & 5 deletions src/main/java/featurecat/lizzie/analysis/Leelaz.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class Leelaz {
private long maxAnalyzeTimeMillis;//, maxThinkingTimeMillis;
private int cmdNumber;
private int currentCmdNum;
private ArrayDeque<String> cmdQueue;

private Process process;

Expand Down Expand Up @@ -71,7 +72,8 @@ public Leelaz() throws IOException, JSONException {
isPondering = false;
startPonderTime = System.currentTimeMillis();
cmdNumber = 1;
currentCmdNum = -1;
currentCmdNum = 0;
cmdQueue = new ArrayDeque<>();

JSONObject config = Lizzie.config.config.getJSONObject("leelaz");

Expand Down Expand Up @@ -188,7 +190,7 @@ private void parseLine(String line) {
// End of response
} else if (line.startsWith("info")) {
isLoaded = true;
if (currentCmdNum == cmdNumber - 1) {
if (isResponseUpToDate()) {
// This should not be stale data when the command number match
parseInfo(line.substring(5));
notifyBestMoveListeners();
Expand All @@ -205,14 +207,16 @@ private void parseLine(String line) {
}
isThinking = false;

} else if (Lizzie.frame != null && line.startsWith("=")) {
} else if (Lizzie.frame != null && (line.startsWith("=") || line.startsWith("?"))) {
if (printCommunication) {
System.out.print(line);
}
String[] params = line.trim().split(" ");
currentCmdNum = Integer.parseInt(params[0].substring(1).trim());

if (params.length == 1) return;
trySendCommandFromQueue();

if (line.startsWith("?") || params.length == 1) return;


if (isSettingHandicap) {
Expand Down Expand Up @@ -284,11 +288,43 @@ private void read() {
}

/**
* Sends a command for leelaz to execute
* Sends a command to command queue for leelaz to execute
*
* @param command a GTP command containing no newline characters
*/
public void sendCommand(String command) {
synchronized(cmdQueue) {
String lastCommand = cmdQueue.peekLast();
// For efficiency, delete unnecessary "lz-analyze" that will be stopped immediately
if (lastCommand != null && lastCommand.startsWith("lz-analyze")) {
cmdQueue.removeLast();
}
cmdQueue.addLast(command);
trySendCommandFromQueue();
}
}

/**
* Sends a command from command queue for leelaz to execute if it is ready
*/
private void trySendCommandFromQueue() {
if (!isResponseUpToDate()) {
return; // leelaz is not ready yet
}
synchronized(cmdQueue) {
String command = cmdQueue.pollFirst();
if (command != null) {
sendCommandToLeelaz(command);
}
}
}

/**
* Sends a command for leelaz to execute
*
* @param command a GTP command containing no newline characters
*/
private void sendCommandToLeelaz(String command) {
if (command.startsWith("fixed_handicap"))
isSettingHandicap = true;
command = cmdNumber + " " + command;
Expand All @@ -304,6 +340,14 @@ public void sendCommand(String command) {
}
}

/**
* Check whether leelaz is responding to the last command
*/
private boolean isResponseUpToDate() {
// Use >= instead of == for avoiding hang-up, though it cannot happen
return currentCmdNum >= cmdNumber - 1;
}

/**
* @param color color of stone to play
* @param move coordinate of the coordinate
Expand Down

0 comments on commit 69dedf4

Please sign in to comment.