Skip to content

Instantly share code, notes, and snippets.

@m0ar
Created February 2, 2018 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m0ar/6feb721b545c2be9954efbd079a9d7ef to your computer and use it in GitHub Desktop.
Save m0ar/6feb721b545c2be9954efbd079a9d7ef to your computer and use it in GitHub Desktop.
Weird memory issue depending on type annotation
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
module HaxlLab (mainHaxl) where
import Data.Hashable (Hashable (..))
import Data.List (foldl')
import Haxl.Core
data Heavy a where
Mock :: Heavy Integer
instance DataSource () Heavy where
fetch _ _ _ reqs = SyncFetch $
mapM_ (\(BlockedFetch req var) -> runHeavy req var)
reqs
runHeavy :: Heavy a -> ResultVar a -> IO ()
runHeavy Mock var = do
-- This runs as expected:
let !n = foldl' (+) 0 [1..100000000] :: Integer
-- Without the type annotation it eats RAM like crazy
-- let !n = foldl' (+) 0 [1..100000000]
putSuccess var n
mainHaxl :: IO ()
mainHaxl = do
env <- initEnv initialState ()
summed <- runHaxl env $ dataFetch Mock
putStrLn $ "Result = " ++ show summed
initialState :: StateStore
initialState = stateSet NoState stateEmpty
deriving instance Show (Heavy a)
deriving instance Eq (Heavy a)
instance StateKey Heavy where
data State Heavy = NoState
instance Hashable (Heavy a) where
hashWithSalt salt _ = hashWithSalt salt ()
instance DataSourceName Heavy where
dataSourceName _ = "Heavy"
instance ShowP Heavy where
showp _ = "Heavy"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment