mutable-containers 0.1.1.0 → 0.1.2.0
raw patch · 5 files changed
+69/−1 lines, 5 filesdep +ghc-primPVP ok
version bump matches the API change (PVP)
Dependencies added: ghc-prim
API changes (from Hackage documentation)
+ Data.Mutable.PRef: asPRef :: PRef s a -> PRef s a
+ Data.Mutable.PRef: class MutableContainer c => MutableRef c where type family RefElement c
+ Data.Mutable.PRef: data PRef s a
+ Data.Mutable.PRef: instance MutableContainer (PRef s a)
+ Data.Mutable.PRef: instance Prim a => MutableRef (PRef s a)
+ Data.Mutable.PRef: modifyRef :: (MutableRef c, PrimMonad m, PrimState m ~ MCState c) => c -> (RefElement c -> RefElement c) -> m ()
+ Data.Mutable.PRef: modifyRef' :: (MutableRef c, PrimMonad m, PrimState m ~ MCState c) => c -> (RefElement c -> RefElement c) -> m ()
+ Data.Mutable.PRef: newRef :: (MutableRef c, PrimMonad m, PrimState m ~ MCState c) => RefElement c -> m c
+ Data.Mutable.PRef: readRef :: (MutableRef c, PrimMonad m, PrimState m ~ MCState c) => c -> m (RefElement c)
+ Data.Mutable.PRef: type IOPRef = PRef (PrimState IO)
+ Data.Mutable.PRef: writeRef :: (MutableRef c, PrimMonad m, PrimState m ~ MCState c) => c -> RefElement c -> m ()
Files
- ChangeLog.md +4/−0
- Data/Mutable/PRef.hs +58/−0
- bench/ref.hs +2/−0
- mutable-containers.cabal +3/−1
- test/Spec.hs +2/−0
ChangeLog.md view
@@ -1,3 +1,7 @@+## 0.1.2.0++* Added PRef+ ## 0.1.1.0 * Added reference benchmark.
+ Data/Mutable/PRef.hs view
@@ -0,0 +1,58 @@+{-# LANGUAGE MagicHash #-}+{-# LANGUAGE TypeFamilies #-}+-- | Use @ByteArray@s containing one element for mutable references.+--+-- This is similar to @URef@s, but avoids the overhead of storing the length of+-- the @Vector@, which we statically know will always be 1. This allows it to+-- be a bit faster.+--+-- Motivated by: <http://stackoverflow.com/questions/27261813/why-is-my-little-stref-int-require-allocating-gigabytes> and ArrayRef.+module Data.Mutable.PRef+ ( -- * Types+ PRef+ , IOPRef+ -- * Functions+ , asPRef+ , MutableRef (..)+ ) where++import Control.Monad (liftM)+import Data.Mutable.Class+import Data.Primitive.ByteArray+import Data.Primitive.Types+import GHC.Types (Int (..))++-- | A primitive ByteArray reference, supporting any monad.+newtype PRef s a = PRef (MutableByteArray s)++asPRef :: PRef s a -> PRef s a+asPRef x = x+{-# INLINE asPRef #-}++-- | A primitive ByteArray IO reference.+type IOPRef = PRef (PrimState IO)++instance MutableContainer (PRef s a) where+ type MCState (PRef s a) = s+instance Prim a => MutableRef (PRef s a) where+ type RefElement (PRef s a) = a++ newRef x = do+ ba <- newByteArray (I# (sizeOf# x))+ writeByteArray ba 0 x+ return $! PRef ba+ {-# INLINE newRef #-}++ readRef (PRef ba) = readByteArray ba 0+ {-# INLINE readRef #-}++ writeRef (PRef ba) = writeByteArray ba 0+ {-# INLINE writeRef #-}++ modifyRef (PRef ba) f = do+ x <- readByteArray ba 0+ writeByteArray ba 0 $! f x+ {-# INLINE modifyRef #-}++ modifyRef' = modifyRef+ {-# INLINE modifyRef' #-}
bench/ref.hs view
@@ -4,6 +4,7 @@ import Data.Mutable.Class import Data.Mutable.SRef import Data.Mutable.URef+import Data.Mutable.PRef import Data.Mutable.VRef test :: (MCState c ~ PrimState IO, RefElement c ~ Int, MutableRef c)@@ -27,6 +28,7 @@ , test "STRef" asSTRef , test "MutVar" asMutVar , test "URef" asURef+ , test "PRef" asPRef , test "SRef" asSRef , test "VRef" asVRef ]
mutable-containers.cabal view
@@ -1,5 +1,5 @@ name: mutable-containers-version: 0.1.1.0+version: 0.1.2.0 synopsis: Abstactions and concrete implementations of mutable containers description: See docs and README at <http://www.stackage.org/package/mutable-containers> homepage: https://github.com/fpco/mutable-containers@@ -16,6 +16,7 @@ exposed-modules: Data.Mutable.SRef Data.Mutable.Class Data.Mutable.URef+ Data.Mutable.PRef Data.Mutable.VRef Data.Mutable.DList Data.Mutable.Deque@@ -24,6 +25,7 @@ , containers , vector , mono-traversable+ , ghc-prim default-language: Haskell2010 test-suite test
test/Spec.hs view
@@ -4,6 +4,7 @@ import Data.Mutable.DList import Data.Mutable.SRef import Data.Mutable.URef+import Data.Mutable.PRef import Data.Mutable.VRef import Data.Sequence (Seq) import Data.Vector (Vector)@@ -111,6 +112,7 @@ _ <- atomic' tested $ \x -> (x - i, ()) check test "URef" asURef modifyRefHelper modifyRefHelper'+ test "PRef" asPRef modifyRefHelper modifyRefHelper' test "SRef" asSRef modifyRefHelper modifyRefHelper' test "VRef" asVRef modifyRefHelper modifyRefHelper' test "STRef" asSTRef modifyRefHelper modifyRefHelper'