@@ -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