5
5
{-# LANGUAGE StandaloneDeriving #-}
6
6
{-# LANGUAGE TemplateHaskell #-}
7
7
{-# LANGUAGE ScopedTypeVariables #-}
8
+ {-# LANGUAGE RecordWildCards #-}
8
9
9
10
module AI.AlphaBeta.Cache where
10
11
@@ -171,14 +172,19 @@ lookupAiCache params board depth side handle = do
171
172
-- let fixSign = if side' == side then id else negate
172
173
let bc = boardCounts board
173
174
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
177
178
Metrics. increment " cache.hit.memory"
178
179
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
182
188
(mbCached, mbStats) <-
183
189
(runStorage handle $ event " file lookup" $ lookupFile board depth side)
184
190
`catch`
@@ -192,15 +198,19 @@ lookupAiCache params board depth side handle = do
192
198
Metrics. increment " cache.miss"
193
199
return Nothing
194
200
(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
197
205
(Just score, Nothing ) -> do
198
206
Metrics. increment " cache.hit.file"
199
207
putAiCache' params board depth side (CacheItemSide score) handle
200
208
return $ Just $ CacheItemSide score
201
209
(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
204
214
205
215
where
206
216
queueCleanup bc bk = return ()
@@ -217,25 +227,25 @@ lookupAiCache params board depth side handle = do
217
227
| statsCount s < 10 = Nothing
218
228
| otherwise = Just s
219
229
220
- lookupMemory :: (BoardCounts , BoardKey ) -> Side -> Checkers (Maybe CacheItemSide )
230
+ lookupMemory :: (BoardCounts , BoardKey ) -> Side -> Checkers (Maybe CacheItemSide , Maybe Stats )
221
231
lookupMemory (bc, bk) side = Metrics. timed " cache.lookup.memory" $ do
222
232
let total = bcFirstMen bc + bcSecondMen bc + bcFirstKings bc + bcSecondKings bc
223
233
cfg <- asks (gcAiConfig . csConfig)
224
234
if total <= aiUseCacheMaxPieces cfg && dpTarget depth >= aiUseCacheMaxDepth cfg
225
235
then do
226
236
AICache _ _ cache <- liftIO $ atomically $ readTVar (aichData handle)
227
237
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
230
240
let ds = [dpTarget depth .. dpTarget depth + aiUseCacheMaxDepthPlus cfg] ++
231
241
[dpTarget depth - aiUseCacheMaxDepthMinus cfg .. dpTarget depth- 1 ]
232
242
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)
235
245
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 )
239
249
240
250
-- | Put an item to the cache.
241
251
-- It is always writen to the memory,
0 commit comments