Skip to content

Commit

Permalink
Merge pull request #88 from fohristiwhirl/infoval_optimise
Browse files Browse the repository at this point in the history
Replace InfoVal() function
  • Loading branch information
fohristiwhirl authored Apr 29, 2020
2 parents 9e05d69 + 7f1eac5 commit cf0f04f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
29 changes: 29 additions & 0 deletions 20_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,35 @@ function InfoVal(s, key) {
return "";
}

function InfoValMany(s, keys) {

// Optimised version of InfoVal for when many values can be pulled out of the same string.

let ret = Object.create(null);

let tokens = s.split(" ").filter(s => s !== "");

for (let key of keys) {
let ok = false;
for (let i = 0; i < tokens.length - 1; i++) {
if (tokens[i] === key) {
if (tokens[i + 1].endsWith(")")) {
ret[key] = tokens[i + 1].slice(0, -1);
} else {
ret[key] = tokens[i + 1];
}
ok = true;
break;
}
}
if (!ok) {
ret[key] = "";
}
}

return ret;
}

function InfoPV(s) {

// Pull the PV out, assuming it's at the end of the string.
Expand Down
38 changes: 21 additions & 17 deletions 80_info.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ function NewInfoHandler() {
this.ever_received_info = true;
this.version++;

let infovals = InfoValMany(s, ["pv", "cp", "mate", "multipv", "nodes", "nps", "time"]);

let move_info;
let move = InfoVal(s, "pv");
let move = infovals["pv"];
move = board.c960_castling_converter(move);

if (this.table[move]) { // We already have move info for this move.
Expand All @@ -113,7 +115,7 @@ function NewInfoHandler() {

let tmp;

tmp = parseInt(InfoVal(s, "cp"), 10); // Score in centipawns
tmp = parseInt(infovals["cp"], 10); // Score in centipawns
if (Number.isNaN(tmp) === false) {
move_info.cp = tmp;
if (this.ever_received_q === false) {
Expand All @@ -122,7 +124,7 @@ function NewInfoHandler() {
move_info.mate = 0; // Engines will send one of cp or mate, so mate gets reset when receiving cp
}

tmp = parseInt(InfoVal(s, "mate"), 10);
tmp = parseInt(infovals["mate"], 10);
if (Number.isNaN(tmp) === false) {
move_info.mate = tmp;
if (tmp !== 0) {
Expand All @@ -131,26 +133,26 @@ function NewInfoHandler() {
}
}

tmp = parseInt(InfoVal(s, "multipv"), 10); // Leela's ranking of the move, starting at 1
tmp = parseInt(infovals["multipv"], 10); // Leela's ranking of the move, starting at 1
if (Number.isNaN(tmp) === false) {
move_info.multipv = tmp;
if (tmp > 1) {
this.ever_received_multipv_2 = true;
}
}

tmp = parseInt(InfoVal(s, "nodes"), 10);
tmp = parseInt(infovals["nodes"], 10);
if (Number.isNaN(tmp) === false) {
move_info.total_nodes = tmp;
this.nodes = tmp;
}

tmp = parseInt(InfoVal(s, "nps"), 10);
tmp = parseInt(infovals["nps"], 10);
if (Number.isNaN(tmp) === false) {
this.nps = tmp;
}

tmp = parseInt(InfoVal(s, "time"), 10);
tmp = parseInt(infovals["time"], 10);
if (Number.isNaN(tmp) === false) {
this.time = tmp;
}
Expand Down Expand Up @@ -186,8 +188,10 @@ function NewInfoHandler() {
this.ever_received_info = true;
this.version++;

let infovals = InfoValMany(s, ["string", "N:", "(D:", "(U:", "(Q+U:", "(S:", "(P:", "(Q:", "(V:", "(M:"]);

let move_info;
let move = InfoVal(s, "string");
let move = infovals["string"];
move = board.c960_castling_converter(move);

if (this.table[move]) { // We already have move info for this move.
Expand All @@ -205,48 +209,48 @@ function NewInfoHandler() {

let tmp;

tmp = parseInt(InfoVal(s, "N:"), 10);
tmp = parseInt(infovals["N:"], 10);
if (Number.isNaN(tmp) === false) {
move_info.n = tmp;
}

tmp = parseFloat(InfoVal(s, "(D:"));
tmp = parseFloat(infovals["(D:"]);
if (Number.isNaN(tmp) === false) {
move_info.d = tmp;
}

tmp = parseFloat(InfoVal(s, "(U:"));
tmp = parseFloat(infovals["(U:"]);
if (Number.isNaN(tmp) === false) {
move_info.u = tmp;
}

tmp = parseFloat(InfoVal(s, "(Q+U:")); // Old name for S
tmp = parseFloat(infovals["(Q+U:"]); // Q+U, old name for S
if (Number.isNaN(tmp) === false) {
move_info.s = tmp;
}

tmp = parseFloat(InfoVal(s, "(S:"));
tmp = parseFloat(infovals["(S:"]);
if (Number.isNaN(tmp) === false) {
move_info.s = tmp;
}

tmp = parseFloat(InfoVal(s, "(P:")); // parseFloat will ignore the trailing %
tmp = parseFloat(infovals["(P:"]); // P, parseFloat will ignore the trailing %
if (Number.isNaN(tmp) === false) {
move_info.p = tmp;
}

tmp = parseFloat(InfoVal(s, "(Q:"));
tmp = parseFloat(infovals["(Q:"]);
if (Number.isNaN(tmp) === false) {
this.ever_received_q = true;
move_info.q = tmp;
}

tmp = parseFloat(InfoVal(s, "(V:"));
tmp = parseFloat(infovals["(V:"]);
if (Number.isNaN(tmp) === false) {
move_info.v = tmp;
}

tmp = parseFloat(InfoVal(s, "(M:"));
tmp = parseFloat(infovals["(M:"]);
if (Number.isNaN(tmp) === false) {
move_info.m = tmp;
}
Expand Down

0 comments on commit cf0f04f

Please sign in to comment.