Skip to content

hashMap forces a memory leak #8

Description

@doyougnu

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions