mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-24 07:52:14 +03:00
4d2dbe68fa
Writing to a mutable var is a particularly potent source of leaks since it mostly defeats GHC's analysis. Here we add assertions to all mutable writes, and fix a couple spots where we wrote some thunks to a mutable var (compiled with -O2). Some of these thunks were probably benign, but others looked liked they might be retaining big args. Didn't do much analysis, just fixed. Actually pretty happy with how easy this was to use and as a diagnostic, once I sorted out some issues. We should consider using it elsewhere, and maybe extending so that we can use it with tests, enable when `-fenable-assertsions` etc. Relates #3388 Also simplified codepaths that use `AcceptWith`, which has unnecessary `Maybe` fields.
43 lines
1.2 KiB
Haskell
43 lines
1.2 KiB
Haskell
module Hasura.GraphQL.Execute.LiveQuery.TMap
|
|
( TMap
|
|
, new
|
|
, reset
|
|
, null
|
|
, lookup
|
|
, insert
|
|
, delete
|
|
, toList
|
|
) where
|
|
|
|
import Hasura.Prelude hiding (lookup, null, toList)
|
|
|
|
import qualified Data.HashMap.Strict as Map
|
|
|
|
import Control.Concurrent.STM
|
|
|
|
-- | A coarse-grained transactional map implemented by simply wrapping a 'Map.HashMap' in a 'TVar'.
|
|
-- Compared to "StmContainers.Map", this provides much faster iteration over the elements at the
|
|
-- cost of significantly increased contention on writes.
|
|
newtype TMap k v = TMap { unTMap :: TVar (Map.HashMap k v) }
|
|
|
|
new :: STM (TMap k v)
|
|
new = TMap <$> newTVar Map.empty
|
|
|
|
reset :: TMap k v -> STM ()
|
|
reset = flip writeTVar Map.empty . unTMap
|
|
|
|
null :: TMap k v -> STM Bool
|
|
null = fmap Map.null . readTVar . unTMap
|
|
|
|
lookup :: (Eq k, Hashable k) => k -> TMap k v -> STM (Maybe v)
|
|
lookup k = fmap (Map.lookup k) . readTVar . unTMap
|
|
|
|
insert :: (Eq k, Hashable k) => v -> k -> TMap k v -> STM ()
|
|
insert !v k mapTv = modifyTVar' (unTMap mapTv) $ Map.insert k v
|
|
|
|
delete :: (Eq k, Hashable k) => k -> TMap k v -> STM ()
|
|
delete k mapTv = modifyTVar' (unTMap mapTv) $ Map.delete k
|
|
|
|
toList :: TMap k v -> STM [(k, v)]
|
|
toList = fmap Map.toList . readTVar . unTMap
|