Skip to content

Commit

Permalink
Better growth factor for arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
arybczak committed Dec 22, 2024
1 parent 54236f4 commit 2d54743
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
6 changes: 1 addition & 5 deletions effectful-core/src/Effectful/Internal/Env.hs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ insertEffect storage e f = do
Storage (bumpVersion version) (StorageData (size + 1) vs0 es0 fs0)
pure $ Ref size version
EQ -> do
let len = doubleCapacity len0
let len = growCapacity len0
vs <- newPrimArray len
es <- newSmallArray len undefinedEffect
fs <- newSmallArray len undefinedRelinker
Expand Down Expand Up @@ -511,10 +511,6 @@ deleteEffect storage (Ref ref version) = do
relinkEnv :: IORef' Storage -> Env es -> IO (Env es)
relinkEnv storage (Env offset refs _) = pure $ Env offset refs storage

-- | Double the capacity of an array.
doubleCapacity :: Int -> Int
doubleCapacity n = max 1 n * 2

undefinedVersion :: Version
undefinedVersion = Version 0

Expand Down
13 changes: 12 additions & 1 deletion effectful-core/src/Effectful/Internal/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ module Effectful.Internal.Utils
, Unique
, newUnique

-- * CallStack
-- * CallStack
, thawCallStack

-- * Array capacity
, growCapacity
) where

import Control.Exception
Expand Down Expand Up @@ -129,3 +132,11 @@ thawCallStack :: CallStack -> CallStack
thawCallStack = \case
FreezeCallStack cs -> cs
cs -> cs

----------------------------------------

-- | Grow capacity of an array.
--
-- See https://archive.ph/Z2R8w.
growCapacity :: Int -> Int
growCapacity n = 1 + quot (n * 3) 2

0 comments on commit 2d54743

Please sign in to comment.