[Haskell-cafe] Haskell Data.Vector, huge memory leak

Ertugrul Söylemez ertesx at gmx.de
Mon Apr 27 01:33:20 UTC 2015


> I am probably not using Data.Vector the way it's intended, but it's the
> simplest data structure I found with O(n) random access.

Remember that vectors from Data.Vector are lazy arrays.  Technically
speaking they are arrays of pointers to lazily evaluated values.  What
you really want for graphics is most likely Data.Vector.Storable.  A
vector of that type is always fully evaluated and dense:

    import qualified Data.Vector as V
    import qualified Data.Vector.Storable as Vs
    import qualified Data.Vector.Unboxed as Vu
    import Data.Word

    v :: V.Vector Word64
    v = V.fromList [1..1000]

    vs :: Vs.Vector Word64
    vs = Vs.fromList [1..1000]

    vu :: Vu.Vector Word64
    vu = Vu.fromList [1..1000]

You would expect that a 1000-element array of `Word64` values takes
exactly 8000 bytes of memory.  This is true for `vs` and `vu`, but not
for `v`, because it is a lazy array.

The difference between storable and unboxed vectors is that the former
has a certain address in memory that is not moved around.  This is
useful for example when you need to interface with OpenGL or SDL.
Unboxed vectors can be faster in certain cases, but the difference is
almost always negligible and would not be possible anyway if you were to
interface with a non-Haskell library.


Greets,
Ertugrul
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 472 bytes
Desc: not available
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150427/c21c6fc9/attachment.sig>


More information about the Haskell-Cafe mailing list