Skip to content

Commit 9c4df1a

Browse files
committed
On stats caching.
refs #6.
1 parent 48a50b5 commit 9c4df1a

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

src/AI/AlphaBeta/Cache.hs

+28-18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
{-# LANGUAGE StandaloneDeriving #-}
66
{-# LANGUAGE TemplateHaskell #-}
77
{-# LANGUAGE ScopedTypeVariables #-}
8+
{-# LANGUAGE RecordWildCards #-}
89

910
module AI.AlphaBeta.Cache where
1011

@@ -171,14 +172,19 @@ lookupAiCache params board depth side handle = do
171172
-- let fixSign = if side' == side then id else negate
172173
let bc = boardCounts board
173174
bk = boardKey board
174-
cached <- lookupMemory (bc, bk) side
175-
case cached of
176-
Just result -> do
175+
(cachedScore, cachedStats) <- lookupMemory (bc, bk) side
176+
case (cachedScore, cachedStats) of
177+
(Just result, Nothing) -> do
177178
Metrics.increment "cache.hit.memory"
178179
queueCleanup bc bk
179-
return cached
180-
-- return $ Just $ CacheItemSide $ fixSign $ cisScore result
181-
Nothing -> do
180+
return cachedScore
181+
(Nothing, Just stats) -> do
182+
Metrics.increment "stats.hit.memory"
183+
return $ Just $ CacheItemSide $ avg stats
184+
(Just _, Just stats) -> do
185+
Metrics.increment "stats.hit.memory"
186+
return $ Just $ CacheItemSide $ avg stats
187+
(Nothing, Nothing) -> do
182188
(mbCached, mbStats) <-
183189
(runStorage handle $ event "file lookup" $ lookupFile board depth side)
184190
`catch`
@@ -192,15 +198,19 @@ lookupAiCache params board depth side handle = do
192198
Metrics.increment "cache.miss"
193199
return Nothing
194200
(Nothing, Just stats) -> do
195-
Metrics.increment "cache.hit.stats"
196-
return $ Just $ CacheItemSide $ avg stats
201+
Metrics.increment "stats.hit.file"
202+
let score = avg stats
203+
putAiCache' params board depth side (CacheItemSide score) handle
204+
return $ Just $ CacheItemSide score
197205
(Just score, Nothing) -> do
198206
Metrics.increment "cache.hit.file"
199207
putAiCache' params board depth side (CacheItemSide score) handle
200208
return $ Just $ CacheItemSide score
201209
(Just _, Just stats) -> do
202-
Metrics.increment "cache.hit.stats"
203-
return $ Just $ CacheItemSide $ avg stats
210+
Metrics.increment "stats.hit.file"
211+
let score = avg stats
212+
putAiCache' params board depth side (CacheItemSide score) handle
213+
return $ Just $ CacheItemSide score
204214

205215
where
206216
queueCleanup bc bk = return ()
@@ -217,25 +227,25 @@ lookupAiCache params board depth side handle = do
217227
| statsCount s < 10 = Nothing
218228
| otherwise = Just s
219229

220-
lookupMemory :: (BoardCounts, BoardKey) -> Side -> Checkers (Maybe CacheItemSide)
230+
lookupMemory :: (BoardCounts, BoardKey) -> Side -> Checkers (Maybe CacheItemSide, Maybe Stats)
221231
lookupMemory (bc, bk) side = Metrics.timed "cache.lookup.memory" $ do
222232
let total = bcFirstMen bc + bcSecondMen bc + bcFirstKings bc + bcSecondKings bc
223233
cfg <- asks (gcAiConfig . csConfig)
224234
if total <= aiUseCacheMaxPieces cfg && dpTarget depth >= aiUseCacheMaxDepth cfg
225235
then do
226236
AICache _ _ cache <- liftIO $ atomically $ readTVar (aichData handle)
227237
case lookupBoardMap (bc,bk) cache of
228-
Nothing -> return Nothing
229-
Just (PerBoardData {boardScores = byDepth}) -> do
238+
Nothing -> return (Nothing, Nothing)
239+
Just (PerBoardData {..}) -> do
230240
let ds = [dpTarget depth .. dpTarget depth + aiUseCacheMaxDepthPlus cfg] ++
231241
[dpTarget depth - aiUseCacheMaxDepthMinus cfg .. dpTarget depth-1]
232242
depths = [depth {dpTarget = d} | d <- ds]
233-
case foldl mplus Nothing [M.lookup d byDepth | d <- depths ] of
234-
Nothing -> return Nothing
243+
case foldl mplus Nothing [M.lookup d boardScores | d <- depths ] of
244+
Nothing -> return (Nothing, boardStats)
235245
Just item -> case side of
236-
First -> return $ ciFirst item
237-
Second -> return $ ciSecond item
238-
else return Nothing
246+
First -> return (ciFirst item, boardStats)
247+
Second -> return (ciSecond item, boardStats)
248+
else return (Nothing, Nothing)
239249

240250
-- | Put an item to the cache.
241251
-- It is always writen to the memory,

0 commit comments

Comments
 (0)