[Haskell-cafe] ST not strict enough?
Jason Dusek
jason.dusek at gmail.com
Wed Nov 16 20:58:51 CET 2011
2011/11/15 Johan Tibell <johan.tibell at gmail.com>:
> On Tue, Nov 15, 2011 at 12:08 PM, Jason Dusek <jason.dusek at gmail.com> wrote:
>> Should I be annotating my functions with strictness, for the
>> vector reference, for example? Should I be using STUArrays,
>> instead?
>
> From
> http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.4.1.0/Control-Monad-ST-Safe.html
>
> "The >>= and >> operations are strict in the state (though not in values
> stored in the state)."
>
> which implies that
>
> modifySTRef counter (+1)
>
> is too lazy.
As a first cut at strictifying the ST operations, I introduced a
strict plus and strict vector write operation, strictifying
every parameter that admitted it.
(+!) a b = ((+) $!! a) $!! b
w v n b = (Vector.unsafeWrite v $!! n) $!! b
This did not alter memory usage in any noticeable way. (Tried it
with strict and lazy ByteStrings and both had the same memory
usage as they did without the extra strictness.)
It does seem off odd that building a vector byte by byte is so
hard to do performantly. Maybe the memory usage ends up being
okay when working with larger structures, though.
--
Jason Dusek
() ascii ribbon campaign - against html e-mail
/\ www.asciiribbon.org - against proprietary attachments
diff --git a/Rebuild.hs b/Rebuild.hs
@@ -15,6 +15,7 @@ import Data.STRef
import Data.String
import Data.Word
+import Control.DeepSeq
import Data.Vector.Unboxed (Vector)
import qualified Data.Vector.Unboxed as Vector (create, length)
import qualified Data.Vector.Unboxed.Mutable as Vector hiding (length)
@@ -46,8 +47,8 @@ rebuildAsVector bytes = byteVector
n <- readSTRef counter
return (Vector.unsafeSlice 0 n v)
writeOneByte v counter b = do n <- readSTRef counter
- Vector.unsafeWrite v n b
+ w v n b
modifySTRef counter (+!1)
+ (+!) a b = ((+) $!! a) $!! b
+ w v n b = (Vector.unsafeWrite v $!! n) $!! b
More information about the Haskell-Cafe
mailing list