From 69dedf48bd3857a9b21ba97266718356f9f762bd Mon Sep 17 00:00:00 2001 From: Hiraoka Date: Tue, 5 Jun 2018 21:46:26 +0900 Subject: [PATCH] Fix too slow "End", "Ctrl-V", etc. by omitting unnecessary lz-analyze --- .../featurecat/lizzie/analysis/Leelaz.java | 54 +++++++++++++++++-- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/src/main/java/featurecat/lizzie/analysis/Leelaz.java b/src/main/java/featurecat/lizzie/analysis/Leelaz.java index 507b6d78e..27eb8ec1c 100644 --- a/src/main/java/featurecat/lizzie/analysis/Leelaz.java +++ b/src/main/java/featurecat/lizzie/analysis/Leelaz.java @@ -33,6 +33,7 @@ public class Leelaz { private long maxAnalyzeTimeMillis;//, maxThinkingTimeMillis; private int cmdNumber; private int currentCmdNum; + private ArrayDeque cmdQueue; private Process process; @@ -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"); @@ -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(); @@ -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) { @@ -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; @@ -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