doyougnu / VSmt

Variational Satisfiability Solver
1 stars 0 forks source link

hashMap forces a memory leak #8

Open doyougnu opened 3 years ago

doyougnu commented 3 years ago

For some reason using a hashmap instead of an intmap causes a memory leak that slows down the solver, consider:

type Cache a b = Map.HashMap (StableName a) b
type ACache = Cache IL (V :/\ IL)

instance (Monad m, MonadIO m, MonadLogger m) =>
  Cacheable (SolverT m) IL (V :/\ IL) where
  memo !a go = do acc  <- readCache accCache
                  !i   <- liftIO $ makeStableName a
                  case Map.lookup i acc of
                    Just b -> do logInProducerWith "Acc Cache Hit on " a
                                 succAccCacheHits
                                 return b
                    Nothing -> do !b <- go
                                  logInProducerWith "Acc Cache miss on " a
                                  updateCache accCache $! Map.insert i b
                                  return b

just this causes a memory leak and I cannot find out why or where. Using an IntMap this leak goes away suggesting it is in the keys:

type Cache a = IMap.IntMap a
type ACache = Cache (V :/\ IL)

instance (Monad m, MonadIO m, MonadLogger m) =>
  Cacheable (SolverT m) IL (V :/\ IL) where
  memo !a go = do acc  <- readCache accCache
                  !i   <- liftIO $ hashStableName <$> makeStableName a
                  case IMap.lookup i acc of
                    Just b -> do logInProducerWith "Acc Cache Hit on " a
                                 succAccCacheHits
                                 return b
                    Nothing -> do !b <- go
                                  logInProducerWith "Acc Cache miss on " a
                                  updateCache accCache $! IMap.insert i b
                                  return b

This is especially weird because the hashable instance is essentially the exact same thing! (hashing just calls out the hashStableName:

instance Hashable (StableName a) where
    hash = hashStableName
    hashWithSalt = defaultHashWithSalt