Skip to content

Commit

Permalink
pack multiple eval hits into a single response message
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Dec 27, 2023
1 parent 28c86f5 commit 3313b10
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
22 changes: 12 additions & 10 deletions src/main/scala/evalCache/EvalCacheApi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@ final class EvalCacheApi(mongo: Mongo)(using
if e.up then upgrade.register(sri, e)

def getMulti(sri: Sri, e: EvalGetMulti, emit: Emit[ClientIn]): Unit =
e.fens.foreach: fen =>
getEntry(Id.make(e.variant, fen))
.map:
_.flatMap(_.makeBestSinglePvEval)
.map(monitorRequest(fen, Monitor.evalCache.multi))
.foreach:
_.foreach: eval =>
emit:
ClientIn.EvalHitMulti:
EvalCacheJsonHandlers.writeMultiHit(eval, fen)
e.fens
.traverse: fen =>
getEntry(Id.make(e.variant, fen))
.map:
_.flatMap(_.makeBestSinglePvEval).map(fen -> _)
.map(monitorRequest(fen, Monitor.evalCache.multi))
.map(_.flatten)
.foreach: evals =>
if evals.nonEmpty then
emit:
ClientIn.EvalHitMulti:
EvalCacheJsonHandlers.writeMultiHit(evals)
multi.register(sri, e)

def put(sri: Sri, user: User.Id, e: EvalPut): Unit =
Expand Down
6 changes: 5 additions & 1 deletion src/main/scala/evalCache/EvalCacheJsonHandlers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ object EvalCacheJsonHandlers:
"pvs" -> JsArray(e.pvs.toList.map(writePv))
)

def writeMultiHit(e: Eval, fen: Fen.Epd) = Json
def writeMultiHit(fen: Fen.Epd, e: Eval): JsObject = Json
.obj("fen" -> fen, "depth" -> e.depth)
.add("cp" -> e.bestPv.score.cp)
.add("mate" -> e.bestPv.score.mate)

def writeMultiHit(evals: List[(Fen.Epd, Eval)]): JsObject = evals match
case List(single) => writeMultiHit.tupled(single)
case many => Json.obj("multi" -> many.map(writeMultiHit.tupled))

private def writePv(pv: Pv) = Json
.obj("moves" -> pv.moves.value.toList.map(_.uci).mkString(" "))
.add("cp" -> pv.score.cp)
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/evalCache/EvalCacheMulti.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ final private class EvalCacheMulti(using
val sris = oldEval.sris.filter(_ != fromSri)
if sris.nonEmpty then
val hit = EvalHitMulti:
EvalCacheJsonHandlers.writeMultiHit(input.eval, input.fen)
EvalCacheJsonHandlers.writeMultiHit(input.fen, input.eval)
sris.foreach: sri =>
Bus.publish(_ sri sri, hit)
upgradeMon.count.increment(sris.size)
Expand Down

0 comments on commit 3313b10

Please sign in to comment.