From ac79f2335f0fde3a37a4905ae05fa81baddc779c Mon Sep 17 00:00:00 2001 From: H.G.Muller Date: Sat, 3 Dec 2016 11:42:41 +0100 Subject: [PATCH] Improve efficiency The commands usermove, time and otim are now matched first, because these are the commands used during game play, when response time and CPU usage are relevant concerns. --- UCI2WB.c | 71 +++++++++++++++++++++++++++++++++---------------------------- 1 files changed, 38 insertions(+), 33 deletions(-) diff --git a/UCI2WB.c b/UCI2WB.c index 997423f..9b5e70c 100644 --- a/UCI2WB.c +++ b/UCI2WB.c @@ -441,16 +441,25 @@ GUI2Engine() line[++i] = 0; if(x == EOF) { printf("# EOF\n"); sprintf(line, "quit -1\n"); } if(think && !pause) Sync(PAUSE), think = 0, Release(); // if no longer thinking, take dummy pause sscanf(line, "%s", command); DPRINT("# '%s' think=%d pause=%d log=%d sent=%d\n", command, think, pause, logLen, sentLen); - if(!strcmp(command, "easy")) { - if(*canPonder) ponder = 0, sprintf(command, "option"), sprintf(line, "option %s=0\n", canPonder); else continue; - } - else if(!strcmp(command, "hard")) { - if(*canPonder) ponder = 1, sprintf(command, "option"), sprintf(line, "option %s=1\n", canPonder); else continue; - } - if(!strcmp(command, "offer")) drawOffer = 1; // backlogged anyway, so this can be done instantly + if(!strcmp(command, "usermove")) { difficult--; break; } // for efficiency during game play, moves, time & otim are tried first + else if(!strcmp(command, "time")) sscanf(line+4, "%d", &myTime), myTime = (10*myTime)/unit; + else if(!strcmp(command, "otim")) sscanf(line+4, "%d", &hisTime), hisTime = (10*hisTime)/unit; + else if(!strcmp(command, "offer")) drawOffer = 1; // backlogged anyway, so this can be done instantly else if(!strcmp(command, "post")) post = 1; else if(!strcmp(command, "nopost"))post = 0; - else if(!strcmp(command, "option")) { + else if(!strcmp(command, "pause")) { + if(computer == stm) myTime -= GetTickCount() - startTime; + suspended = 1 + pondering; // remember if we were pondering, and stop search ignoring bestmove + StopPonder(pondering || computer == stm); + } + else { //convert easy & hard to "option" after treating their effect on the adapter + if(!strcmp(command, "easy")) { + if(*canPonder) ponder = 0, sprintf(command, "option"), sprintf(line, "option %s=0\n", canPonder); else continue; + } + else if(!strcmp(command, "hard")) { + if(*canPonder) ponder = 1, sprintf(command, "option"), sprintf(line, "option %s=1\n", canPonder); else continue; + } + if(!strcmp(command, "option")) { char *p; if(logLen == sentLen) logLen = 0, sentLen = 0; // engine is up to date; reset buffer if(sscanf(line+7, "UCI2WB debug output=%d", &debug) == 1) ; else @@ -463,15 +472,30 @@ GUI2Engine() } else snprintf(backLog+logLen, 9999-logLen, "setoption %s%s\n", nameWord, line+7); DPRINT("# backlog: %s", backLog+logLen); logLen += strlen(backLog+logLen); if(!think && Release()) break; // break will restart analysis; pondering is restarted by Release itself + } + else difficult = 1; // difficult command; terminate loop for easy ones } - else if(!strcmp(command, "pause")) { - if(computer == stm) myTime -= GetTickCount() - startTime; - suspended = 1 + pondering; // remember if we were pondering, and stop search ignoring bestmove - StopPonder(pondering || computer == stm); - } - else difficult = 1; // difficult command; terminate loop for easy ones } // next command + // some commands that should never come during thinking can be safely processed here + if(difficult < 0) { // used as kludge to signal "usermove" was already matched + sscanf(line, "usermove %s", command); // strips off linefeed + Move4Engine(command); + stm = WHITE+BLACK - stm; collect = (computer == ANALYZE); sm = 0; + // when pondering we either continue the ponder search as normal search, or abort it + if(pondering || computer == ANALYZE) { + if(pondering && !strcmp(command, move[moveNr])) { // ponder hit + char *draw = drawOffer ? " draw" : ""; drawOffer = 0; + pondering = 0; pause = 2; moveNr++; startTime = GetTickCount(); // clock starts running now + EPRINT((f, "# ponderhit%s\n", draw)) fflush(toE); fflush(stdout); + think = 2; // request blocking input during thinking + goto nomove; + } + StopPonder(1); searching = 0; + } + strcpy(move[moveNr++], command); // possibly overwrites ponder move + continue; + } if(!strcmp(command, "resume")) { if(suspended == 2) StartPonder(); // restart interrupted ponder search suspended = think = 0; continue; // causes thinking to start in normal way if on move or analyzing @@ -494,23 +518,6 @@ GUI2Engine() Sync(PAUSE); // wait for readyok EPRINT((f, "# u%cinewgame\n", sc)) fflush(toE); } - else if(!strcmp(command, "usermove")) { - sscanf(line, "usermove %s", command); // strips off linefeed - Move4Engine(command); - stm = WHITE+BLACK - stm; collect = (computer == ANALYZE); sm = 0; - // when pondering we either continue the ponder search as normal search, or abort it - if(pondering || computer == ANALYZE) { - if(pondering && !strcmp(command, move[moveNr])) { // ponder hit - char *draw = drawOffer ? " draw" : ""; drawOffer = 0; - pondering = 0; pause = 2; moveNr++; startTime = GetTickCount(); // clock starts running now - EPRINT((f, "# ponderhit%s\n", draw)) fflush(toE); fflush(stdout); - think = 2; // request blocking input during thinking - goto nomove; - } - StopPonder(1); searching = 0; - } - strcpy(move[moveNr++], command); // possibly overwrites ponder move - } else if(!strcmp(command, "level")) { int sec = 0; sscanf(line, "level %d %d:%d %d", &mps, &tc, &sec, &inc) == 4 || @@ -567,8 +574,6 @@ GUI2Engine() else if(!strcmp(command, "exit")) computer = NONE, StopPonder(1), searching = 0, Analyze("false"); else if(!strcmp(command, "force")) computer = NONE, StopPonder(pondering); else if(!strcmp(command, "go")) computer = stm; - else if(!strcmp(command, "time")) sscanf(line+4, "%d", &myTime), myTime = (10*myTime)/unit; - else if(!strcmp(command, "otim")) sscanf(line+4, "%d", &hisTime), hisTime = (10*hisTime)/unit; else if(!strcmp(command, "ping")) { /* static int done; if(!done) pause = 1, fprintf(toE, "isready\n"), fflush(toE), printf("# send isready\n"), fflush(stdout), Sync(PAUSE); done = 1;*/ printf("po%s", line+2); } else if(!strcmp(command, "memory")) sscanf(line, "memory %d", &memory); else if(!strcmp(command, "cores")&& !!*threadOpt) { sscanf(line, "cores %d", &cores); EPRINT((f, "# setoption %s%s %s%d\n", nameWord, threadOpt, valueWord, cores)) } -- 1.7.0.4