[Haskell-cafe] return with mutable object

Baojun Wang wangbj at gmail.com
Wed Jun 18 00:53:24 UTC 2014


To make my question more clearer, will test1/test2 have noticeable
performance difference?

-- mutable1.hs

import qualified Data.Vector.Mutable as MV

import Control.Monad

import Control.Monad.Primitive

a1 :: (PrimMonad m) => MV.MVector (PrimState m) a -> m ()

a1 v = do

 -- do something

 return ()

a2 :: (PrimMonad m) => MV.MVector (PrimState m) a -> m ()

a2 v = do

 -- do something else

 return ()

b1 :: (PrimMonad m) => MV.MVector (PrimState m) a -> m (MV.MVector
(PrimState m) a)

b2 :: (PrimMonad m) => MV.MVector (PrimState m) a -> m (MV.MVector
(PrimState m) a)

b1 v = do

 -- do something different

 return v

b2 v = do

 -- do something else different

 return v

test1 :: IO ()

test1 = do

 v1 <- MV.replicate 1000 0

 a1 v1

 a2 v1

 return ()

test2 :: IO ()

test2 =

 MV.replicate 1000 0 >>= b1 >>= b2 >> return () -- I'd prefer this way
cause it's more haskell.




On Tue, Jun 17, 2014 at 5:50 PM, Baojun Wang <wangbj at gmail.com> wrote:

> Hi List,
>
> Per my understanding, return x would make a new copy of the object. What
> if the returned object is mutable? Will this make a new (mutable) object?
>
> My concern is if I created a very large mutable object, does return
> mutable make a full copy of the original mutable data, or just copy a
> reference (pointer?)?
>
> Thanks
> baojun
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140617/bd56f18a/attachment.html>


More information about the Haskell-Cafe mailing list