tibbe avatar tibbe committed 2d7a605

Even cheaper IO

Comments (0)

Files changed (1)


 {-# LANGUAGE BangPatterns, CPP, Rank2Types, OverloadedStrings,
-    RecordWildCards #-}
+    RecordWildCards, MagicHash, UnboxedTuples #-}
 -- |
 -- Module      :  Data.Attoparsec.ByteString.Internal
 -- Copyright   :  Bryan O'Sullivan 2007-2011
 import Foreign.Ptr (castPtr, minusPtr, plusPtr)
 import Foreign.Storable (Storable(peek, sizeOf))
 import Prelude hiding (getChar, take, takeWhile)
-import System.IO.Unsafe (unsafeDupablePerformIO)
 import qualified Data.Attoparsec.Internal.Types as T
 import qualified Data.ByteString as B8
 import qualified Data.ByteString.Char8 as B
 import qualified Data.ByteString.Unsafe as B
 #if defined(__GLASGOW_HASKELL__)
+import GHC.Base (realWorld#)
 import GHC.Exts (inline)
+import GHC.IO (IO(IO))
+import System.IO.Unsafe (unsafePerformIO)
 inline :: a -> a
 inline x = x
                 done !i !s = return (T i s)
             inner start s1
     bs <- get
-    let T i s' = unsafeDupablePerformIO $ scanner bs
+    let T i s' = inlinePerformIO $ scanner bs
         !h = B.unsafeTake i bs
         !t = B.unsafeDrop i bs
     put t
                   Done _ a     -> Right a
                   _            -> error "parseOnly: impossible error!"
 {-# INLINE parseOnly #-}
+-- | Just like unsafePerformIO, but we inline it. Big performance gains as
+-- it exposes lots of things to further inlining. /Very unsafe/. In
+-- particular, you should do no memory allocation inside an
+-- 'inlinePerformIO' block. On Hugs this is just @unsafePerformIO@.
+inlinePerformIO :: IO a -> a
+#if defined(__GLASGOW_HASKELL__)
+inlinePerformIO (IO m) = case m realWorld# of (# _, r #) -> r
+inlinePerformIO = unsafePerformIO
+{-# INLINE inlinePerformIO #-}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.