Skip to content
This repository
Browse code

Make use of assert conditional

This turns out to make about a 10% difference to performance, even
though the GHC docs claim it shouldn't :-(

--HG--
extra : convert_revision : 446f05e
  • Loading branch information...
commit a99b318f9a12f0d6bf73b0c4ce7bea1d9b3e4a08 1 parent eac9147
Bryan O'Sullivan authored August 15, 2010
8  Data/Text.hs
@@ -174,7 +174,9 @@ import Prelude (Char, Bool(..), Functor(..), Int, Maybe(..), String,
174 174
 #if defined(HAVE_DEEPSEQ)
175 175
 import Control.DeepSeq (NFData)
176 176
 #endif
  177
+#if defined(ASSERTS)
177 178
 import Control.Exception (assert)
  179
+#endif
178 180
 import Data.Char (isSpace)
179 181
 import Data.Data (Data(gfoldl, toConstr, gunfold, dataTypeOf))
180 182
 #if __GLASGOW_HASKELL__ >= 612
@@ -395,7 +397,11 @@ init (Text arr off len) | len <= 0                   = emptyError "init"
395 397
 -- | /O(1)/ Tests whether a 'Text' is empty or not.  Subject to
396 398
 -- fusion.
397 399
 null :: Text -> Bool
398  
-null (Text _arr _off len) = assert (len >= 0) $ len <= 0
  400
+null (Text _arr _off len) =
  401
+#if defined(ASSERTS)
  402
+    assert (len >= 0) $
  403
+#endif
  404
+    len <= 0
399 405
 {-# INLINE [1] null #-}
400 406
 
401 407
 {-# RULES
23  Data/Text/Array.hs
@@ -42,19 +42,20 @@ module Data.Text.Array
42 42
     , unsafeWrite
43 43
     ) where
44 44
 
45  
-#if 0
46  
-#define BOUNDS_CHECKING
  45
+#if defined(ASSERTS)
47 46
 -- This fugly hack is brought by GHC's apparent reluctance to deal
48 47
 -- with MagicHash and UnboxedTuples when inferring types. Eek!
49  
-#define CHECK_BOUNDS(_func_,_len_,_k_) \
  48
+# define CHECK_BOUNDS(_func_,_len_,_k_) \
50 49
 if (_k_) < 0 || (_k_) >= (_len_) then error ("Data.Text.Array." ++ (_func_) ++ ": bounds error, offset " ++ show (_k_) ++ ", length " ++ show (_len_)) else
51 50
 #else
52  
-#define CHECK_BOUNDS(_func_,_len_,_k_)
  51
+# define CHECK_BOUNDS(_func_,_len_,_k_)
53 52
 #endif
54 53
 
55 54
 #include "MachDeps.h"
56 55
 
  56
+#if defined(ASSERTS)
57 57
 import Control.Exception (assert)
  58
+#endif
58 59
 import Data.Text.UnsafeShift (shiftL)
59 60
 import GHC.Base (ByteArray#, MutableByteArray#, Int(..),
60 61
                  indexWord16Array#, newByteArray#,
@@ -92,10 +93,14 @@ unsafeFreeze :: MArray s -> ST s (Array)
92 93
 
93 94
 -- | Create an uninitialized mutable array.
94 95
 unsafeNew :: forall s. Int -> ST s (MArray s)
95  
-unsafeNew n = assert (n >= 0) . ST $ \s1# ->
96  
-   case bytesInArray n of
97  
-     len@(I# len#) ->
98  
-#if defined(BOUNDS_CHECKING)
  96
+unsafeNew n =
  97
+#if defined(ASSERTS)
  98
+    assert (n >= 0) .
  99
+#endif
  100
+    ST $ \s1# ->
  101
+    case bytesInArray n of
  102
+      len@(I# len#) ->
  103
+#if defined(ASSERTS)
99 104
          if len < 0 then error (show ("unsafeNew",len)) else
100 105
 #endif
101 106
          case newByteArray# len# s1# of
@@ -181,8 +186,10 @@ copy src dest
181 186
 -- | Unsafely copy the elements of an array.
182 187
 unsafeCopy :: MArray s -> Int -> MArray s -> Int -> Int -> ST s ()
183 188
 unsafeCopy src sidx dest didx count =
  189
+#if defined(ASSERTS)
184 190
     assert (sidx + count <= length src) .
185 191
     assert (didx + count <= length dest) $
  192
+#endif
186 193
     copy_loop sidx didx 0
187 194
     where
188 195
       copy_loop !i !j !c
10  Data/Text/Encoding/Fusion.hs
... ...
@@ -1,4 +1,4 @@
1  
-{-# LANGUAGE BangPatterns, Rank2Types #-}
  1
+{-# LANGUAGE BangPatterns, CPP, Rank2Types #-}
2 2
 
3 3
 -- |
4 4
 -- Module      : Data.Text.Encoding.Fusion
@@ -31,7 +31,9 @@ module Data.Text.Encoding.Fusion
31 31
     , module Data.Text.Encoding.Fusion.Common
32 32
     ) where
33 33
 
  34
+#if defined(ASSERTS)
34 35
 import Control.Exception (assert)
  36
+#endif
35 37
 import Data.ByteString.Internal (ByteString(..), mallocByteString, memcpy)
36 38
 import Data.Text.Fusion (Step(..), Stream(..))
37 39
 import Data.Text.Fusion.Size
@@ -182,7 +184,11 @@ unstream (Stream next s0 len) = unsafePerformIO $ do
182 184
       {-# NOINLINE trimUp #-}
183 185
       trimUp fp _ off = return $! PS fp 0 off
184 186
       copy0 :: ForeignPtr Word8 -> Int -> Int -> IO (ForeignPtr Word8)
185  
-      copy0 !src !srcLen !destLen = assert (srcLen <= destLen) $ do
  187
+      copy0 !src !srcLen !destLen =
  188
+#if defined(ASSERTS)
  189
+        assert (srcLen <= destLen) $
  190
+#endif
  191
+        do
186 192
           dest <- mallocByteString destLen
187 193
           withForeignPtr src  $ \src'  ->
188 194
               withForeignPtr dest $ \dest' ->
2  Data/Text/Encoding/Fusion/Common.hs
@@ -28,9 +28,9 @@ module Data.Text.Encoding.Fusion.Common
28 28
     ) where
29 29
 
30 30
 import Data.Bits ((.&.))
31  
-import Data.Char (ord)
32 31
 import Data.Text.Fusion (Step(..), Stream(..))
33 32
 import Data.Text.Fusion.Internal (M(..), S(..))
  33
+import Data.Text.UnsafeChar (ord)
34 34
 import Data.Text.UnsafeShift (shiftR)
35 35
 import Data.Word (Word8)
36 36
 import qualified Data.Text.Encoding.Utf8 as U8
24  Data/Text/Encoding/Utf8.hs
... ...
@@ -1,4 +1,4 @@
1  
-{-# LANGUAGE MagicHash #-}
  1
+{-# LANGUAGE CPP, MagicHash #-}
2 2
 
3 3
 -- |
4 4
 -- Module      : Data.Text.Encoding.Utf16
@@ -30,9 +30,11 @@ module Data.Text.Encoding.Utf8
30 30
     , validate4
31 31
     ) where
32 32
 
  33
+#if defined(ASSERTS)
33 34
 import Control.Exception (assert)
34  
-import Data.Char (ord)
  35
+#endif
35 36
 import Data.Bits ((.&.))
  37
+import Data.Text.UnsafeChar (ord)
36 38
 import Data.Text.UnsafeShift (shiftR)
37 39
 import GHC.Exts
38 40
 import GHC.Word (Word8(..))
@@ -47,14 +49,22 @@ between x y z = x >= y && x <= z
47 49
 {-# INLINE between #-}
48 50
 
49 51
 ord2   :: Char -> (Word8,Word8)
50  
-ord2 c = assert (n >= 0x80 && n <= 0x07ff) (x1,x2)
  52
+ord2 c =
  53
+#if defined(ASSERTS)
  54
+    assert (n >= 0x80 && n <= 0x07ff)
  55
+#endif
  56
+    (x1,x2)
51 57
     where
52 58
       n  = ord c
53 59
       x1 = fromIntegral $ (n `shiftR` 6) + 0xC0
54 60
       x2 = fromIntegral $ (n .&. 0x3F)   + 0x80
55 61
 
56 62
 ord3   :: Char -> (Word8,Word8,Word8)
57  
-ord3 c = assert (n >= 0x0800 && n <= 0xffff) (x1,x2,x3)
  63
+ord3 c =
  64
+#if defined(ASSERTS)
  65
+    assert (n >= 0x0800 && n <= 0xffff)
  66
+#endif
  67
+    (x1,x2,x3)
58 68
     where
59 69
       n  = ord c
60 70
       x1 = fromIntegral $ (n `shiftR` 12) + 0xE0
@@ -62,7 +72,11 @@ ord3 c = assert (n >= 0x0800 && n <= 0xffff) (x1,x2,x3)
62 72
       x3 = fromIntegral $ (n .&. 0x3F) + 0x80
63 73
 
64 74
 ord4   :: Char -> (Word8,Word8,Word8,Word8)
65  
-ord4 c = assert (n >= 0x10000) (x1,x2,x3,x4)
  75
+ord4 c =
  76
+#if defined(ASSERTS)
  77
+    assert (n >= 0x10000)
  78
+#endif
  79
+    (x1,x2,x3,x4)
66 80
     where
67 81
       n  = ord c
68 82
       x1 = fromIntegral $ (n `shiftR` 18) + 0xF0
11  Data/Text/Foreign.hs
... ...
@@ -1,4 +1,4 @@
1  
-{-# LANGUAGE BangPatterns #-}
  1
+{-# LANGUAGE BangPatterns, CPP #-}
2 2
 -- |
3 3
 -- Module      : Data.Text.Foreign
4 4
 -- Copyright   : (c) Bryan O'Sullivan 2009
@@ -25,7 +25,9 @@ module Data.Text.Foreign
25 25
     , unsafeCopyToPtr
26 26
     ) where
27 27
 
  28
+#if defined(ASSERTS)
28 29
 import Control.Exception (assert)
  30
+#endif
29 31
 import Control.Monad.ST (unsafeIOToST)
30 32
 import Data.Text.Internal (Text(..), empty)
31 33
 import qualified Data.Text.Array as A
@@ -53,7 +55,11 @@ fromPtr :: Ptr Word16           -- ^ source array
53 55
         -> Int                  -- ^ length of source array (in 'Word16' units)
54 56
         -> IO Text
55 57
 fromPtr _   0   = return empty
56  
-fromPtr ptr len = assert (len > 0) $ return (Text arr 0 len)
  58
+fromPtr ptr len =
  59
+#if defined(ASSERTS)
  60
+    assert (len > 0) $
  61
+#endif
  62
+    return $! Text arr 0 len
57 63
   where
58 64
     arr = A.run (A.unsafeNew len >>= copy)
59 65
     copy marr = loop ptr 0
@@ -68,6 +74,7 @@ fromPtr ptr len = assert (len > 0) $ return (Text arr 0 len)
68 74
 -- 'unsafeCopyToPtr'.
69 75
 lengthWord16 :: Text -> Int
70 76
 lengthWord16 (Text _arr _off len) = len
  77
+{-# INLINE lengthWord16 #-}
71 78
 
72 79
 -- | /O(n)/ Copy a 'Text' to an array.  The array is assumed to be big
73 80
 -- enough to hold the contents of the entire 'Text'.
3  Data/Text/Fusion.hs
@@ -47,9 +47,8 @@ import Prelude (Bool(..), Char, Maybe(..), Monad(..), Int,
47 47
                 Num(..), Ord(..), ($), (&&),
48 48
                 fromIntegral, otherwise)
49 49
 import Data.Bits ((.&.))
50  
-import Data.Char (ord)
51 50
 import Data.Text.Internal (Text(..))
52  
-import Data.Text.UnsafeChar (unsafeChr, unsafeWrite)
  51
+import Data.Text.UnsafeChar (ord, unsafeChr, unsafeWrite)
53 52
 import Data.Text.UnsafeShift (shiftL, shiftR)
54 53
 import qualified Data.Text.Array as A
55 54
 import qualified Data.Text.Fusion.Common as S
15  Data/Text/Fusion/Size.hs
... ...
@@ -1,3 +1,4 @@
  1
+{-# LANGUAGE CPP #-}
1 2
 {-# OPTIONS_GHC -fno-warn-missing-methods #-}
2 3
 -- |
3 4
 -- Module      : Data.Text.Fusion.Internal
@@ -26,7 +27,9 @@ module Data.Text.Fusion.Size
26 27
     , isEmpty
27 28
     ) where
28 29
 
  30
+#if defined(ASSERTS)
29 31
 import Control.Exception (assert)
  32
+#endif
30 33
 
31 34
 data Size = Exact {-# UNPACK #-} !Int -- ^ Exact size.
32 35
           | Max   {-# UNPACK #-} !Int -- ^ Upper bound on size.
@@ -34,11 +37,19 @@ data Size = Exact {-# UNPACK #-} !Int -- ^ Exact size.
34 37
             deriving (Eq, Show)
35 38
 
36 39
 exactSize :: Int -> Size
37  
-exactSize n = assert (n >= 0) Exact n
  40
+exactSize n =
  41
+#if defined(ASSERTS)
  42
+    assert (n >= 0)
  43
+#endif
  44
+    Exact n
38 45
 {-# INLINE exactSize #-}
39 46
 
40 47
 maxSize :: Int -> Size
41  
-maxSize n = assert (n >= 0) Max n
  48
+maxSize n =
  49
+#if defined(ASSERTS)
  50
+    assert (n >= 0)
  51
+#endif
  52
+    Max n
42 53
 {-# INLINE maxSize #-}
43 54
 
44 55
 unknownSize :: Size
20  Data/Text/Internal.hs
... ...
@@ -1,4 +1,4 @@
1  
-{-# LANGUAGE DeriveDataTypeable #-}
  1
+{-# LANGUAGE CPP, DeriveDataTypeable #-}
2 2
 
3 3
 -- |
4 4
 -- Module      : Data.Text.Internal
@@ -28,7 +28,9 @@ module Data.Text.Internal
28 28
     , showText
29 29
     ) where
30 30
 
  31
+#if defined(ASSERTS)
31 32
 import Control.Exception (assert)
  33
+#endif
32 34
 import qualified Data.Text.Array as A
33 35
 import Data.Typeable (Typeable)
34 36
 
@@ -42,13 +44,15 @@ data Text = Text
42 44
 -- | Smart constructor.
43 45
 text :: A.Array -> Int -> Int -> Text
44 46
 text arr off len =
45  
-    assert (len >= 0) .
46  
-    assert (off >= 0) .
47  
-    assert (alen == 0 || len == 0 || off < alen) .
48  
-    assert (len == 0 || c < 0xDC00 || c > 0xDFFF) $
49  
-    Text arr off len
50  
-  where c    = A.unsafeIndex arr off
51  
-        alen = A.length arr
  47
+#if defined(ASSERTS)
  48
+  let c    = A.unsafeIndex arr off
  49
+      alen = A.length arr
  50
+  in assert (len >= 0) .
  51
+     assert (off >= 0) .
  52
+     assert (alen == 0 || len == 0 || off < alen) .
  53
+     assert (len == 0 || c < 0xDC00 || c > 0xDFFF) $
  54
+#endif
  55
+     Text arr off len
52 56
 {-# INLINE text #-}
53 57
 
54 58
 -- | /O(1)/ The empty 'Text'.
18  Data/Text/Lazy/Builder.hs
... ...
@@ -1,4 +1,4 @@
1  
-{-# LANGUAGE BangPatterns, Rank2Types #-}
  1
+{-# LANGUAGE BangPatterns, CPP, Rank2Types #-}
2 2
 
3 3
 -----------------------------------------------------------------------------
4 4
 -- |
@@ -29,7 +29,9 @@ module Data.Text.Lazy.Builder
29 29
    , flush
30 30
    ) where
31 31
 
  32
+#if defined(ASSERTS)
32 33
 import Control.Exception (assert)
  34
+#endif
33 35
 import Control.Monad.ST (ST, runST)
34 36
 import Data.Bits ((.&.))
35 37
 import Data.Char (ord)
@@ -259,8 +261,10 @@ newBuffer size = do
259 261
 -- | Unsafely copy the elements of an array.
260 262
 unsafeCopy :: A.Array -> Int -> A.MArray s -> Int -> Int -> ST s ()
261 263
 unsafeCopy src sidx dest didx count =
  264
+#if defined(ASSERTS)
262 265
     assert (sidx + count <= A.length src) .
263 266
     assert (didx + count <= A.length dest) $
  267
+#endif
264 268
     copy_loop sidx didx 0
265 269
   where
266 270
     copy_loop !i !j !c
@@ -274,12 +278,16 @@ unsafeCopy src sidx dest didx count =
274 278
 unsafeWrite :: A.MArray s -> Int -> Char -> ST s Int
275 279
 unsafeWrite marr i c
276 280
     | n < 0x10000 = do
277  
-        assert (i >= 0) . assert (i < A.length marr) $
278  
-          A.unsafeWrite marr i (fromIntegral n)
  281
+#if defined(ASSERTS)
  282
+        assert (i >= 0) . assert (i < A.length marr) $ return ()
  283
+#endif
  284
+        A.unsafeWrite marr i (fromIntegral n)
279 285
         return 1
280 286
     | otherwise = do
281  
-        assert (i >= 0) . assert (i < A.length marr - 1) $
282  
-          A.unsafeWrite marr i lo
  287
+#if defined(ASSERTS)
  288
+        assert (i >= 0) . assert (i < A.length marr - 1) $ return ()
  289
+#endif
  290
+        A.unsafeWrite marr i lo
283 291
         A.unsafeWrite marr (i+1) hi
284 292
         return 2
285 293
     where n = ord c
10  Data/Text/Lazy/Encoding/Fusion.hs
... ...
@@ -1,4 +1,4 @@
1  
-{-# LANGUAGE BangPatterns, Rank2Types #-}
  1
+{-# LANGUAGE BangPatterns, CPP, Rank2Types #-}
2 2
 
3 3
 -- |
4 4
 -- Module      : Data.Text.Lazy.Encoding.Fusion
@@ -43,7 +43,9 @@ import System.IO.Unsafe (unsafePerformIO)
43 43
 import Foreign.ForeignPtr (withForeignPtr, ForeignPtr)
44 44
 import Foreign.Storable (pokeByteOff)
45 45
 import Data.ByteString.Internal (mallocByteString, memcpy)
  46
+#if defined(ASSERTS)
46 47
 import Control.Exception (assert)
  48
+#endif
47 49
 import qualified Data.ByteString.Internal as B
48 50
 
49 51
 data S = S0
@@ -123,7 +125,11 @@ unstreamChunks chunkSize (Stream next s0 len0) = chunk s0 (upperBound 4 len0)
123 125
               loop n' (off+1) s fp'
124 126
             trimUp fp off = B.PS fp 0 off
125 127
             copy0 :: ForeignPtr Word8 -> Int -> Int -> IO (ForeignPtr Word8)
126  
-            copy0 !src !srcLen !destLen = assert (srcLen <= destLen) $ do
  128
+            copy0 !src !srcLen !destLen =
  129
+#if defined(ASSERTS)
  130
+              assert (srcLen <= destLen) $
  131
+#endif
  132
+              do
127 133
                 dest <- mallocByteString destLen
128 134
                 withForeignPtr src  $ \src'  ->
129 135
                     withForeignPtr dest $ \dest' ->
45  Data/Text/Unsafe.hs
@@ -21,7 +21,9 @@ module Data.Text.Unsafe
21 21
     , unsafeTail
22 22
     ) where
23 23
      
  24
+#if defined(ASSERTS)
24 25
 import Control.Exception (assert)
  26
+#endif
25 27
 import Data.Text.Encoding.Utf16 (chr2)
26 28
 import Data.Text.Internal (Text(..))
27 29
 import Data.Text.UnsafeChar (unsafeChr)
@@ -40,11 +42,11 @@ import GHC.Base (realWorld#)
40 42
 -- omits the check for the empty case, so there is an obligation on
41 43
 -- the programmer to provide a proof that the 'Text' is non-empty.
42 44
 unsafeHead :: Text -> Char
43  
-unsafeHead (Text arr off len)
  45
+unsafeHead (Text arr off _len)
44 46
     | m < 0xD800 || m > 0xDBFF = unsafeChr m
45 47
     | otherwise                = chr2 m n
46  
-    where m = assert (len > 0) $ A.unsafeIndex arr off
47  
-          n = assert (len > 1) $ A.unsafeIndex arr (off+1)
  48
+    where m = A.unsafeIndex arr off
  49
+          n = A.unsafeIndex arr (off+1)
48 50
 {-# INLINE unsafeHead #-}
49 51
 
50 52
 -- | /O(1)/ A variant of 'tail' for non-empty 'Text'. 'unsafeHead'
@@ -52,42 +54,45 @@ unsafeHead (Text arr off len)
52 54
 -- the programmer to provide a proof that the 'Text' is non-empty.
53 55
 unsafeTail :: Text -> Text
54 56
 unsafeTail t@(Text arr off len) =
55  
-    assert (d <= len) $ Text arr (off+d) (len-d)
  57
+#if defined(ASSERTS)
  58
+    assert (d <= len) $
  59
+#endif
  60
+    Text arr (off+d) (len-d)
56 61
   where d = iter_ t 0
57 62
 {-# INLINE unsafeTail #-}
58 63
 
59  
--- | /O(1)/ Iterate one step forwards through a UTF-16 array,
60  
--- returning the current character and the delta to add to give the
61  
--- next offset to iterate at.
  64
+-- | /O(1)/ Iterate (unsafely) one step forwards through a UTF-16
  65
+-- array, returning the current character and the delta to add to give
  66
+-- the next offset to iterate at.
62 67
 iter :: Text -> Int -> (Char,Int)
63  
-iter (Text arr off len) i
  68
+iter (Text arr off _len) i
64 69
     | m < 0xD800 || m > 0xDBFF = (unsafeChr m, 1)
65 70
     | otherwise                = (chr2 m n,    2)
66  
-  where m = assert (i < len)     $ A.unsafeIndex arr j
67  
-        n = assert (i + 1 < len) $ A.unsafeIndex arr k
68  
-        j = assert (i >= 0)      $ off + i
69  
-        k =                        j + 1
  71
+  where m = A.unsafeIndex arr j
  72
+        n = A.unsafeIndex arr k
  73
+        j = off + i
  74
+        k = j + 1
70 75
 {-# INLINE iter #-}
71 76
 
72 77
 -- | /O(1)/ Iterate one step through a UTF-16 array, returning the
73 78
 -- delta to add to give the next offset to iterate at.
74 79
 iter_ :: Text -> Int -> Int
75  
-iter_ (Text arr off len) i | m < 0xD800 || m > 0xDBFF = 1
76  
-                           | otherwise                = 2
77  
-  where m = assert (i >= 0 && i < len) $ A.unsafeIndex arr (off+i)
  80
+iter_ (Text arr off _len) i | m < 0xD800 || m > 0xDBFF = 1
  81
+                            | otherwise                = 2
  82
+  where m = A.unsafeIndex arr (off+i)
78 83
 {-# INLINE iter_ #-}
79 84
 
80 85
 -- | /O(1)/ Iterate one step backwards through a UTF-16 array,
81 86
 -- returning the current character and the delta to add (i.e. a
82 87
 -- negative number) to give the next offset to iterate at.
83 88
 reverseIter :: Text -> Int -> (Char,Int)
84  
-reverseIter (Text arr off len) i
  89
+reverseIter (Text arr off _len) i
85 90
     | m < 0xDC00 || m > 0xDFFF = (unsafeChr m, -1)
86 91
     | otherwise                = (chr2 n m,    -2)
87  
-  where m = assert (i < len)    $ A.unsafeIndex arr j
88  
-        n = assert (i - 1 >= 0) $ A.unsafeIndex arr k
89  
-        j = assert (i >= 0)     $ off + i
90  
-        k =                       j - 1
  92
+  where m = A.unsafeIndex arr j
  93
+        n = A.unsafeIndex arr k
  94
+        j = off + i
  95
+        k = j - 1
91 96
 {-# INLINE reverseIter #-}
92 97
 
93 98
 -- | Just like unsafePerformIO, but we inline it. Big performance gains as
26  Data/Text/UnsafeChar.hs
... ...
@@ -1,4 +1,4 @@
1  
-{-# LANGUAGE MagicHash #-}
  1
+{-# LANGUAGE CPP, MagicHash #-}
2 2
 
3 3
 -- |
4 4
 -- Module      : Data.Text.UnsafeChar
@@ -15,22 +15,28 @@
15 15
 -- Fast character manipulation functions.
16 16
 module Data.Text.UnsafeChar
17 17
     (
18  
-      unsafeChr
  18
+      ord
  19
+    , unsafeChr
19 20
     , unsafeChr8
20 21
     , unsafeChr32
21 22
     , unsafeWrite
22 23
     -- , unsafeWriteRev
23 24
     ) where
24 25
 
  26
+#ifdef ASSERTS
25 27
 import Control.Exception (assert)
  28
+#endif
26 29
 import Control.Monad.ST (ST)
27 30
 import Data.Bits ((.&.))
28  
-import Data.Char (ord)
29 31
 import Data.Text.UnsafeShift (shiftR)
30  
-import GHC.Exts (Char(..), chr#, word2Int#)
  32
+import GHC.Exts (Char(..), Int(..), chr#, ord#, word2Int#)
31 33
 import GHC.Word (Word8(..), Word16(..), Word32(..))
32 34
 import qualified Data.Text.Array as A
33 35
 
  36
+ord :: Char -> Int
  37
+ord (C# c#) = I# (ord# c#)
  38
+{-# INLINE ord #-}
  39
+
34 40
 unsafeChr :: Word16 -> Char
35 41
 unsafeChr (W16# w#) = C# (chr# (word2Int# w#))
36 42
 {-# INLINE unsafeChr #-}
@@ -46,12 +52,16 @@ unsafeChr32 (W32# w#) = C# (chr# (word2Int# w#))
46 52
 unsafeWrite :: A.MArray s -> Int -> Char -> ST s Int
47 53
 unsafeWrite marr i c
48 54
     | n < 0x10000 = do
49  
-        assert (i >= 0) . assert (i < A.length marr) $
50  
-          A.unsafeWrite marr i (fromIntegral n)
  55
+#if defined(ASSERTS)
  56
+        assert (i >= 0) . assert (i < A.length marr) $ return ()
  57
+#endif
  58
+        A.unsafeWrite marr i (fromIntegral n)
51 59
         return $! i+1
52 60
     | otherwise = do
53  
-        assert (i >= 0) . assert (i < A.length marr - 1) $
54  
-          A.unsafeWrite marr i lo
  61
+#if defined(ASSERTS)
  62
+        assert (i >= 0) . assert (i < A.length marr - 1) $ return ()
  63
+#endif
  64
+        A.unsafeWrite marr i lo
55 65
         A.unsafeWrite marr (i+1) hi
56 66
         return $! i+2
57 67
     where n = ord c
1  text.cabal
@@ -85,3 +85,4 @@ library
85 85
     ghc-options: -fwarn-tabs
86 86
   if flag(developer)
87 87
     ghc-options: -Werror
  88
+    cpp-options: -DASSERTS

0 notes on commit a99b318

Please sign in to comment.
Something went wrong with that request. Please try again.