diff options
author | michaelt <what_is_it_to_do_anything@yahoo.com> | 2013-11-26 00:55:41 -0500 |
---|---|---|
committer | michaelt <what_is_it_to_do_anything@yahoo.com> | 2013-11-26 00:55:41 -0500 |
commit | 5e3f5409333cf06f79489169195e5cd7031ac4bd (patch) | |
tree | 873ed1b89e4874f6a719d79842450046f04e71f3 | |
parent | ff38b9f029818b81f8e1c309f7eccc979ba5c346 (diff) | |
download | text-pipes-5e3f5409333cf06f79489169195e5cd7031ac4bd.tar.gz text-pipes-5e3f5409333cf06f79489169195e5cd7031ac4bd.tar.zst text-pipes-5e3f5409333cf06f79489169195e5cd7031ac4bd.zip |
scrap Text.hGetChunk for bytestring + streamDecodeUtf8, which is three times as fast
-rw-r--r-- | Pipes/Text.hs | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/Pipes/Text.hs b/Pipes/Text.hs index 4fc6c4a..3d119fe 100644 --- a/Pipes/Text.hs +++ b/Pipes/Text.hs | |||
@@ -165,6 +165,7 @@ import qualified Data.List as List | |||
165 | import Foreign.C.Error (Errno(Errno), ePIPE) | 165 | import Foreign.C.Error (Errno(Errno), ePIPE) |
166 | import qualified GHC.IO.Exception as G | 166 | import qualified GHC.IO.Exception as G |
167 | import Pipes | 167 | import Pipes |
168 | import qualified Pipes.ByteString as PB | ||
168 | import qualified Pipes.ByteString.Parse as PBP | 169 | import qualified Pipes.ByteString.Parse as PBP |
169 | import Pipes.Text.Parse ( | 170 | import Pipes.Text.Parse ( |
170 | nextChar, drawChar, unDrawChar, peekChar, isEndOfChars ) | 171 | nextChar, drawChar, unDrawChar, peekChar, isEndOfChars ) |
@@ -223,12 +224,22 @@ stdin = fromHandle IO.stdin | |||
223 | -} | 224 | -} |
224 | 225 | ||
225 | fromHandle :: MonadIO m => IO.Handle -> Producer' Text m () | 226 | fromHandle :: MonadIO m => IO.Handle -> Producer' Text m () |
227 | #if MIN_VERSION_text(0,11,4) | ||
228 | fromHandle h = PB.fromHandle h >-> pipeDecodeUtf8 | ||
229 | {-# INLINABLE fromHandle#-} | ||
230 | -- bytestring fromHandle + streamDecodeUtf8 is 3 times as fast as | ||
231 | -- the dedicated Text IO function 'hGetChunk' ; | ||
232 | -- this way "runEffect $ PT.fromHandle hIn >-> PT.toHandle hOut" | ||
233 | -- runs the same as the conduit equivalent, only slightly slower | ||
234 | -- than "runEffect $ PB.fromHandle hIn >-> PB.toHandle hOut" | ||
235 | |||
236 | #else | ||
226 | fromHandle h = go where | 237 | fromHandle h = go where |
227 | go = do txt <- liftIO (T.hGetChunk h) | 238 | go = do txt <- liftIO (T.hGetChunk h) |
228 | unless (T.null txt) $ do yield txt | 239 | unless (T.null txt) $ do yield txt |
229 | go | 240 | go |
230 | {-# INLINABLE fromHandle#-} | 241 | {-# INLINABLE fromHandle#-} |
231 | 242 | #endif | |
232 | {-| Stream text from a file using Pipes.Safe | 243 | {-| Stream text from a file using Pipes.Safe |
233 | 244 | ||
234 | >>> runSafeT $ runEffect $ Text.readFile "hello.hs" >-> Text.map toUpper >-> hoist lift Text.stdout | 245 | >>> runSafeT $ runEffect $ Text.readFile "hello.hs" >-> Text.map toUpper >-> hoist lift Text.stdout |