on master help sign in
Move inlineInterleaveST and document it.

--HG-- extra : convert_revision : 321c2edabb2b8d9ddfd283506594c6e76bbdf11b

commit9ea5d9987f4aded71430c991715ff394a31be704 authorBryan O'Sullivan <bos@serpentine.com> dateThu 29 Apr 2010, 17:32 (16 years ago) parent01bb6aa
2 files changed+21−12
Data/Text/Lazy/Builder.hs+3−10 Data/Text/Unsafe.hs+18−2
Data/Text/Lazy/Builder.hs+3−10
@@ -29,15 +29,15 @@ module Data.Text.Lazy.Builder
2929 ) where
3030
3131import Control.Exception (assert)
32−import Control.Monad.ST
32+import Control.Monad.ST (ST, runST)
3333import Data.Bits ((.&.))
3434import Data.Char (ord)
3535import Data.Monoid (Monoid(..))
3636import Data.Text.Internal (Text(..))
3737import Data.Text.Lazy.Internal (defaultChunkSize)
38+import Data.Text.Unsafe (inlineInterleaveST)
3839import Data.Text.UnsafeShift (shiftR)
39−import Data.Word
40−import GHC.ST (ST(..))
40+import Data.Word (Word16)
4141import Prelude hiding (map, putChar)
4242
4343import qualified Data.Text as S
@@ -215,13 +215,6 @@ newBuffer size = do
215215 return $! Buffer arr 0 0 size
216216{-# INLINE newBuffer #-}
217217
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−
225218-- | Unsafely copy the elements of an array.
226219unsafeCopy :: A.Elt e =>
227220 A.Array e -> Int -> A.MArray s e -> Int -> Int -> ST s ()
Data/Text/Unsafe.hs+18−2
@@ -12,7 +12,8 @@
1212-- use in heavily tested code.
1313module Data.Text.Unsafe
1414 (
15− inlinePerformIO
15+ inlineInterleaveST
16+ , inlinePerformIO
1617 , iter
1718 , iter_
1819 , reverseIter
@@ -21,9 +22,10 @@ module Data.Text.Unsafe
2122 ) where
2223
2324import Control.Exception (assert)
25+import Data.Text.Encoding.Utf16 (chr2)
2426import Data.Text.Internal (Text(..))
2527import Data.Text.UnsafeChar (unsafeChr)
26−import Data.Text.Encoding.Utf16 (chr2)
28+import GHC.ST (ST(..))
2729import qualified Data.Text.Array as A
2830#if defined(__GLASGOW_HASKELL__)
2931# if __GLASGOW_HASKELL__ >= 611
@@ -100,3 +102,17 @@ inlinePerformIO (IO m) = case m realWorld# of (# _, r #) -> r
100102#else
101103inlinePerformIO = unsafePerformIO
102104#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 #-}