on master help sign in
Describe performance characteristics of Env
commite190279e5f3460459f98e82fa8e0b22172bd0f8d authorAndrzej Rybczak <andrzej@rybczak.net> dateMon 14 Jun 2021, 23:21 (5 years ago) parent6a84a0f
1 file changed+16−4
src/Effectful/Internal/Env.hs+16−4
src/Effectful/Internal/Env.hs+16−4
@@ -35,7 +35,19 @@ import Effectful.Internal.Has
3535
3636type role Env nominal
3737
38−-- | A mutable, extensible record indexed by effect data types.
38+-- | A mutable, strict (elements are kept in weak head normal form), extensible
39+-- record indexed by effect data types.
40+--
41+-- Offers pretty much perfect performance characteristics:
42+--
43+-- - Extending: /O(1)/ (amortized).
44+--
45+-- - Shrinking: /O(1)/.
46+--
47+-- - Indexing via '(:>)': /O(1)/ (amortized).
48+--
49+-- - Modification of a specific element: /O(1)/.
50+--
3951newtype Env (es :: [Effect]) = Env (IORef EnvRef)
4052
4153data EnvRef = EnvRef Int (SmallMutableArray RealWorld Any)
@@ -90,6 +102,9 @@ unsafeConsEnv e (Env ref) = do
90102 e `seq` writeSmallArray es n (toAny e)
91103 writeIORef ref $! EnvRef (n + 1) es
92104 pure $ Env ref
105+ where
106+ doubleCapacity :: Int -> Int
107+ doubleCapacity n = max 1 n * 2
93108{-# NOINLINE unsafeConsEnv #-}
94109
95110-- | Shrink the environment by one data type (in place). Makes sure the size of
@@ -145,9 +160,6 @@ unsafeStateEnv f (Env ref) = do
145160----------------------------------------
146161-- Internal helpers
147162
148−doubleCapacity :: Int -> Int
149−doubleCapacity n = max 1 n * 2
150−
151163toAny :: a -> Any
152164toAny = unsafeCoerce
153165