on master help sign in
Move inlineInterleaveST and document it.
9ea5d99Bryan O'Sullivan
Data/Text/Lazy/Builder.hs+3−10
@@ -29,15 +29,15 @@ module Data.Text.Lazy.Builder
29 ) where
29 ) where
30
30
31import Control.Exception (assert)
31import Control.Exception (assert)
32−import Control.Monad.ST
32+import Control.Monad.ST (ST, runST)
33import Data.Bits ((.&.))
33import Data.Bits ((.&.))
34import Data.Char (ord)
34import Data.Char (ord)
35import Data.Monoid (Monoid(..))
35import Data.Monoid (Monoid(..))
36import Data.Text.Internal (Text(..))
36import Data.Text.Internal (Text(..))
37import Data.Text.Lazy.Internal (defaultChunkSize)
37import Data.Text.Lazy.Internal (defaultChunkSize)
38+import Data.Text.Unsafe (inlineInterleaveST)
38import Data.Text.UnsafeShift (shiftR)
39import Data.Text.UnsafeShift (shiftR)
39−import Data.Word
40+import Data.Word (Word16)
40−import GHC.ST (ST(..))
41import Prelude hiding (map, putChar)
41import Prelude hiding (map, putChar)
42
42
43import qualified Data.Text as S
43import qualified Data.Text as S
@@ -215,13 +215,6 @@ newBuffer size = do
215 return $! Buffer arr 0 0 size
215 return $! Buffer arr 0 0 size
216{-# INLINE newBuffer #-}
216{-# INLINE newBuffer #-}
217
217
218−inlineInterleaveST :: ST s a -> ST s a
219−inlineInterleaveST (ST m) = ST $ \ s ->
220− let r = case m s of (# _, res #) -> res in (# s, r #)
221−{-# INLINE inlineInterleaveST #-}
222−
223−------------------------------------------------------------------------
224−
225-- | Unsafely copy the elements of an array.
218-- | Unsafely copy the elements of an array.
226unsafeCopy :: A.Elt e =>
219unsafeCopy :: A.Elt e =>
227 A.Array e -> Int -> A.MArray s e -> Int -> Int -> ST s ()
220 A.Array e -> Int -> A.MArray s e -> Int -> Int -> ST s ()
Data/Text/Unsafe.hs+18−2
@@ -12,7 +12,8 @@
12-- use in heavily tested code.
12-- use in heavily tested code.
13module Data.Text.Unsafe
13module Data.Text.Unsafe
14 (
14 (
15+ inlineInterleaveST
15− inlinePerformIO
16+ , inlinePerformIO
16 , iter
17 , iter
17 , iter_
18 , iter_
18 , reverseIter
19 , reverseIter
@@ -21,9 +22,10 @@ module Data.Text.Unsafe
21 ) where
22 ) where
22
23
23import Control.Exception (assert)
24import Control.Exception (assert)
25+import Data.Text.Encoding.Utf16 (chr2)
24import Data.Text.Internal (Text(..))
26import Data.Text.Internal (Text(..))
25import Data.Text.UnsafeChar (unsafeChr)
27import Data.Text.UnsafeChar (unsafeChr)
26−import Data.Text.Encoding.Utf16 (chr2)
28+import GHC.ST (ST(..))
27import qualified Data.Text.Array as A
29import qualified Data.Text.Array as A
28#if defined(__GLASGOW_HASKELL__)
30#if defined(__GLASGOW_HASKELL__)
29# if __GLASGOW_HASKELL__ >= 611
31# if __GLASGOW_HASKELL__ >= 611
@@ -100,3 +102,17 @@ inlinePerformIO (IO m) = case m realWorld# of (# _, r #) -> r
100#else
102#else
101inlinePerformIO = unsafePerformIO
103inlinePerformIO = unsafePerformIO
102#endif
104#endif
105+
106+-- | Allow an 'ST' computation to be deferred lazily. When passed an
107+-- action of type 'ST' @s@ @a@, the action will only be performed when
108+-- the value of @a@ is demanded.
109+--
110+-- This function is identical to the normal unsafeInterleaveST, but is
111+-- inlined and hence faster.
112+--
113+-- /Note/: This operation is highly unsafe, as it can introduce
114+-- externally visible non-determinism into an 'ST' action.
115+inlineInterleaveST :: ST s a -> ST s a
116+inlineInterleaveST (ST m) = ST $ \ s ->
117+ let r = case m s of (# _, res #) -> res in (# s, r #)
118+{-# INLINE inlineInterleaveST #-}