diff options
author | Sylvain Henry <hsyl20@gmail.com> | 2018-05-13 11:36:28 -0400 |
---|---|---|
committer | Ben Gamari <ben@smart-cactus.org> | 2018-05-13 18:32:20 -0400 |
commit | 7c665f9ce0980ee7c81a44c8f861686395637453 (patch) | |
tree | b8a90c2da2d364bdd7703c0f265d1f7adf4d0c28 /compiler/utils/BufWrite.hs | |
parent | 2188427015e384410fcb7ec9114f5e7f0e2ad6f0 (diff) | |
download | haskell-7c665f9ce0980ee7c81a44c8f861686395637453.tar.gz |
Refactor LitString
Refactor LitString so that the string length is computed at most once
and then stored.
Also remove strlen and memcmp wrappers (it seems like they were a
workaround for a very old GCC when using -fvia-C).
Bumps haddock submodule.
Reviewers: bgamari, dfeuer, nickkuk
Reviewed By: bgamari, nickkuk
Subscribers: nickkuk, dfeuer, thomie, carter
Differential Revision: https://phabricator.haskell.org/D4630
Diffstat (limited to 'compiler/utils/BufWrite.hs')
-rw-r--r-- | compiler/utils/BufWrite.hs | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/compiler/utils/BufWrite.hs b/compiler/utils/BufWrite.hs index e25bf06c4c..99c043ce41 100644 --- a/compiler/utils/BufWrite.hs +++ b/compiler/utils/BufWrite.hs @@ -20,6 +20,7 @@ module BufWrite ( bPutFS, bPutFZS, bPutLitString, + bPutReplicate, bFlush, ) where @@ -97,19 +98,45 @@ bPutCStringLen b@(BufHandle buf r hdl) cstr@(ptr, len) = do copyBytes (buf `plusPtr` i) ptr len writeFastMutInt r (i + len) -bPutLitString :: BufHandle -> LitString -> Int -> IO () -bPutLitString b@(BufHandle buf r hdl) a len = a `seq` do +bPutLitString :: BufHandle -> LitString -> IO () +bPutLitString b@(BufHandle buf r hdl) l@(LitString a len) = l `seq` do i <- readFastMutInt r if (i+len) >= buf_size then do hPutBuf hdl buf i writeFastMutInt r 0 if (len >= buf_size) then hPutBuf hdl a len - else bPutLitString b a len + else bPutLitString b l else do copyBytes (buf `plusPtr` i) a len writeFastMutInt r (i+len) +-- | Replicate an 8-bit character +bPutReplicate :: BufHandle -> Int -> Char -> IO () +bPutReplicate (BufHandle buf r hdl) len c = do + i <- readFastMutInt r + let oc = fromIntegral (ord c) + if (i+len) < buf_size + then do + fillBytes (buf `plusPtr` i) oc len + writeFastMutInt r (i+len) + else do + -- flush the current buffer + when (i /= 0) $ hPutBuf hdl buf i + if (len < buf_size) + then do + fillBytes buf oc len + writeFastMutInt r len + else do + -- fill a full buffer + fillBytes buf oc buf_size + -- flush it as many times as necessary + let go n | n >= buf_size = do + hPutBuf hdl buf buf_size + go (n-buf_size) + | otherwise = writeFastMutInt r n + go len + bFlush :: BufHandle -> IO () bFlush (BufHandle buf r hdl) = do i <- readFastMutInt r |