@@ -47,7 +47,7 @@ loadAiCache scoreMove (AlphaBeta params rules eval) = do
4747 let getKey input = pmResult (smiMove input)
4848 aiCfg <- asks (gcAiConfig . csConfig)
4949 processor <- runProcessor (aiThreads aiCfg) getKey scoreMove
50- cache <- liftIO $ atomically $ newTVar $ AICache False processor emptyBoardMap
50+ cache <- liftIO newTBoardMap
5151 cachePath <- do
5252 home <- liftIO $ getEnv " HOME"
5353 let directory = home </> " .cache" </> " hcheckers" </> rulesName rules </> " ai.cache"
@@ -100,10 +100,11 @@ loadAiCache scoreMove (AlphaBeta params rules eval) = do
100100 fhHandle = fd
101101 }
102102 counts <- liftIO $ atomically $ newTVar $ BoardCounts 50 50 50 50
103- moves <- liftIO $ atomically $ newTVar emptyBoardMap
103+ moves <- liftIO newTBoardMap
104104 let handle = AICacheHandle {
105105 aichRules = rules,
106106 aichData = cache,
107+ aichProcessor = processor,
107108 aichPossibleMoves = moves,
108109 aichWriteQueue = writeQueue,
109110 aichCleanupQueue = cleanupQueue,
@@ -138,22 +139,6 @@ cacheDumper rules params handle = do
138139
139140cacheCleaner :: AICacheHandle rules eval -> Checkers ()
140141cacheCleaner handle = forever $ do
141- delta <- liftIO $ atomically $ do
142- aic <- readTVar (aichData handle)
143- currentCounts <- readTVar (aichCurrentCounts handle)
144- let cache = aicData aic
145- let oldSize = boardMapSize cache
146- let cache' = M. filterWithKey biggerCounts cache
147- biggerCounts bc _ =
148- (bcFirstMen bc + bcFirstKings bc + bcSecondMen bc + bcSecondKings bc) <=
149- (bcFirstMen currentCounts + bcFirstKings currentCounts + bcSecondMen currentCounts + bcSecondKings currentCounts)
150- aic' = aic {aicData = cache'}
151- newSize = boardMapSize cache'
152- delta = oldSize - newSize
153- writeTVar (aichData handle) aic'
154- return delta
155- $ info " cleanup: cleaned {} records" (Single delta)
156-
157142 liftIO $ threadDelay $ 30 * 1000 * 1000
158143
159144normalize :: BoardSize -> (BoardCounts ,BoardKey ,Side ) -> (BoardCounts ,BoardKey ,Side )
@@ -173,7 +158,7 @@ lookupAiCache params board depth side handle = do
173158 -- let fixSign = if side' == side then id else negate
174159 let bc = boardCounts board
175160 bk = boardKey board
176- (cachedScore, cachedStats) <- lookupMemory (bc, bk) side
161+ (cachedScore, cachedStats) <- lookupMemory board side
177162 case (cachedScore, cachedStats) of
178163 (Just result, Nothing ) -> do
179164 Monitoring. increment " cache.hit.memory"
@@ -225,11 +210,12 @@ lookupAiCache params board depth side handle = do
225210 | statsCount s < 10 = Nothing
226211 | otherwise = Just s
227212
228- lookupMemory :: ( BoardCounts , BoardKey ) -> Side -> Checkers (Maybe CacheItemSide , Maybe Stats )
229- lookupMemory (bc, bk) side = Monitoring. timed " cache.lookup.memory" $ do
213+ lookupMemory :: Board -> Side -> Checkers (Maybe CacheItemSide , Maybe Stats )
214+ lookupMemory board side = Monitoring. timed " cache.lookup.memory" $ do
230215 cfg <- asks (gcAiConfig . csConfig)
231- AICache _ _ cache <- liftIO $ atomically $ readTVar (aichData handle)
232- case lookupBoardMap (bc,bk) cache of
216+ let cache = aichData handle
217+ mbItem <- liftIO $ lookupBoardMap cache board
218+ case mbItem of
233219 Nothing -> return (Nothing , Nothing )
234220 Just (PerBoardData {.. }) -> do
235221 let depths = [dpLast depth .. dpLast depth + aiUseCacheMaxDepthPlus cfg] ++
@@ -269,20 +255,16 @@ putAiCache' params board depth side sideItem handle = do
269255 now <- liftIO $ getTime Monotonic
270256 Monitoring. increment " cache.records.put"
271257 fileCacheEnabled <- asks (aiStoreCache . gcAiConfig . csConfig)
272- liftIO $ atomically $ do
273- aic <- readTVar (aichData handle)
274- let item = case side of
275- First -> CacheItem {ciFirst = Just sideItem, ciSecond = Nothing }
276- Second -> CacheItem {ciFirst = Nothing , ciSecond = Just sideItem}
277-
278- init = PerBoardData (M. singleton (dpLast depth) item) Nothing
258+ let cache = aichData handle
259+ let item = case side of
260+ First -> CacheItem {ciFirst = Just sideItem, ciSecond = Nothing }
261+ Second -> CacheItem {ciFirst = Nothing , ciSecond = Just sideItem}
279262
280- newAicData = putBoardMapWith (<>) (bc,bk) init (aicData aic)
281- aic' = aic {aicDirty = True , aicData = newAicData}
263+ init = PerBoardData (M. singleton (dpLast depth) item) Nothing
282264
283- Just perBoard = lookupBoardMap (bc,bk) newAicData
265+ liftIO $ putBoardMapWith cache (<>) board init
284266
285- writeTVar (aichData handle) aic'
267+ liftIO $ atomically $ do
286268 when (fileCacheEnabled && needWriteFile) $
287269 putWriteQueue (aichWriteQueue handle) (board, depth, side, sideItem)
288270 putCleanupQueue (aichCleanupQueue handle) (bc, bk) now
@@ -292,3 +274,4 @@ putAiCache :: GameRules rules => AlphaBetaParams -> Board -> DepthParams -> Side
292274putAiCache params board depth side score moves handle = do
293275 let sideItem = CacheItemSide {cisScore = score}
294276 putAiCache' params board depth side sideItem handle
277+
0 commit comments