only propagate cloud eval when depth increases

this should reduce the number of redis messages greatly.

Before that, you could get cevals for pvs greater than yours,
even if your depth is already higher.
deepcrayonfish^2
Thibault Duplessis 2021-11-28 18:30:37 +01:00
parent ce6129dbd9
commit ece947fd20
1 changed files with 17 additions and 9 deletions

View File

@ -22,8 +22,8 @@ final private class EvalCacheUpgrade(setting: SettingStore[Boolean], scheduler:
import EvalCacheUpgrade._
private val members = mutable.AnyRefMap.empty[SriString, WatchingMember]
private val evals = mutable.AnyRefMap.empty[SetupId, Set[SriString]]
private val expirableSris = new ExpireCallbackMemo(20 minutes, sri => unregister(Socket.Sri(sri)))
private val evals = mutable.AnyRefMap.empty[SetupId, EvalState]
private val expirableSris = new ExpireCallbackMemo(10 minutes, sri => unregister(Socket.Sri(sri)))
private val upgradeMon = lila.mon.evalCache.upgrade
@ -34,15 +34,19 @@ final private class EvalCacheUpgrade(setting: SettingStore[Boolean], scheduler:
}
val setupId = makeSetupId(variant, fen, multiPv)
members += (sri.value -> WatchingMember(push, setupId, path))
evals += (setupId -> (~evals.get(setupId) + sri.value))
evals += (setupId -> evals.get(setupId).fold(EvalState(Set(sri.value), 0))(_ addSri sri))
expirableSris put sri.value
}
def onEval(input: EvalCacheEntry.Input, sri: Socket.Sri): Unit = if (setting.get()) {
(1 to input.eval.multiPv) flatMap { multiPv =>
evals get makeSetupId(input.id.variant, input.fen, multiPv)
} foreach { sris =>
val wms = sris.withFilter(sri.value !=) flatMap members.get
val setupId = makeSetupId(input.id.variant, input.fen, multiPv)
evals get setupId map (setupId -> _)
} filter {
_._2.depth < input.eval.depth
} foreach { case (setupId, eval) =>
evals += (setupId -> eval.copy(depth = input.eval.depth))
val wms = eval.sris.withFilter(sri.value !=) flatMap members.get
if (wms.nonEmpty) {
val json = JsonHandlers.writeEval(input.eval, input.fen)
wms foreach { wm =>
@ -61,10 +65,10 @@ final private class EvalCacheUpgrade(setting: SettingStore[Boolean], scheduler:
}
private def unregisterEval(setupId: SetupId, sri: Socket.Sri): Unit =
evals get setupId foreach { sris =>
val newSris = sris - sri.value
evals get setupId foreach { eval =>
val newSris = eval.sris - sri.value
if (newSris.isEmpty) evals -= setupId
else evals += (setupId -> newSris)
else evals += (setupId -> eval.copy(sris = newSris))
}
scheduler.scheduleWithFixedDelay(1 minute, 1 minute) { () =>
@ -80,6 +84,10 @@ private object EvalCacheUpgrade {
private type SetupId = String
private type Push = JsObject => Unit
private case class EvalState(sris: Set[SriString], depth: Int) {
def addSri(sri: Socket.Sri) = copy(sris = sris + sri.value)
}
private def makeSetupId(variant: Variant, fen: FEN, multiPv: Int): SetupId =
s"${variant.id}${EvalCacheEntry.SmallFen.make(variant, fen).value}^$multiPv"