on master help sign in
Reduce inlining bloat from use of singleton
commit74e4526972d19b58831c815998fa0ab79e9aba28 authorBryan O'Sullivan <bos@serpentine.com> dateFri 15 Aug 2014, 10:08 (12 years ago) parentcfd6520
3 files changed+19−3
Data/Text.hs+17−1 Data/Text/Internal.hs+1−1 Data/Text/Internal/Fusion/Common.hs+1−1
Data/Text.hs+17−1
@@ -221,7 +221,7 @@ import Data.Text.Internal (Text(..), empty, firstf, mul, safe, text)
221221import qualified Prelude as P
222222import Data.Text.Unsafe (Iter(..), iter, iter_, lengthWord16, reverseIter,
223223 reverseIter_, unsafeHead, unsafeTail)
224−import Data.Text.Internal.Unsafe.Char (unsafeChr)
224+import Data.Text.Internal.Unsafe.Char (unsafeChr, unsafeWrite)
225225import qualified Data.Text.Internal.Functions as F
226226import qualified Data.Text.Internal.Encoding.Utf16 as U16
227227import Data.Text.Internal.Search (indices)
@@ -425,6 +425,22 @@ singleton :: Char -> Text
425425singleton = unstream . S.singleton . safe
426426{-# INLINE [1] singleton #-}
427427
428+{-# RULES "TEXT singleton" forall a.
429+ unstream (S.singleton (safe a))
430+ = singleton_ a #-}
431+
432+-- This is intended to reduce inlining bloat.
433+singleton_ :: Char -> Text
434+singleton_ c = Text (A.run x) 0 len
435+ where x :: ST s (A.MArray s)
436+ x = do arr <- A.new len
437+ _ <- unsafeWrite arr 0 d
438+ return arr
439+ len | d < '\x10000' = 1
440+ | otherwise = 2
441+ d = safe c
442+{-# NOINLINE singleton_ #-}
443+
428444-- -----------------------------------------------------------------------------
429445-- * Basic functions
430446
Data/Text/Internal.hs+1−1
@@ -110,7 +110,7 @@ safe :: Char -> Char
110110safe c
111111 | ord c .&. 0x1ff800 /= 0xd800 = c
112112 | otherwise = '\xfffd'
113−{-# INLINE safe #-}
113+{-# INLINE [0] safe #-}
114114
115115-- | Apply a function to the first element of an optional pair.
116116firstf :: (a -> c) -> Maybe (a,b) -> Maybe (c,b)
Data/Text/Internal/Fusion/Common.hs+1−1
@@ -123,7 +123,7 @@ singleton :: Char -> Stream Char
123123singleton c = Stream next False 1
124124 where next False = Yield c True
125125 next True = Done
126−{-# INLINE singleton #-}
126+{-# INLINE [0] singleton #-}
127127
128128streamList :: [a] -> Stream a
129129{-# INLINE [0] streamList #-}