diff options
author | Sylvain Henry <sylvain@haskus.fr> | 2021-03-11 17:41:51 +0100 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2021-04-29 17:29:44 -0400 |
commit | 1d03d8bef962e6789db44e8b6f2cfd9e34f3f5ad (patch) | |
tree | d77ec6ba70bc70e87e954ecb2f56cfa39d12159e /compiler/GHC/Data | |
parent | c2541c49f162f1d03b0ae55f47b9c76cc96df76f (diff) | |
download | haskell-1d03d8bef962e6789db44e8b6f2cfd9e34f3f5ad.tar.gz |
Replace (ptext .. sLit) with `text`
1. `text` is as efficient as `ptext . sLit` thanks to the rewrite rules
2. `text` is visually nicer than `ptext . sLit`
3. `ptext . sLit` encourages using one `ptext` for several `sLit` as in:
ptext $ case xy of
... -> sLit ...
... -> sLit ...
which may allocate SDoc's TextBeside constructors at runtime instead
of sharing them into CAFs.
Diffstat (limited to 'compiler/GHC/Data')
-rw-r--r-- | compiler/GHC/Data/FastString.hs | 15 |
1 files changed, 6 insertions, 9 deletions
diff --git a/compiler/GHC/Data/FastString.hs b/compiler/GHC/Data/FastString.hs index 9ba933f336..590f043d4b 100644 --- a/compiler/GHC/Data/FastString.hs +++ b/compiler/GHC/Data/FastString.hs @@ -31,7 +31,8 @@ -- * Pointer and size of a Latin-1 encoded string. -- * Practically no operations. -- * Outputting them is fast. --- * Generated by 'sLit'. +-- * Generated by 'mkPtrString'. +-- * Length of string literals (mkPtrString "abc") is computed statically -- * Turn into 'GHC.Utils.Outputable.SDoc' with 'GHC.Utils.Outputable.ptext' -- * Requires manual memory management. -- Improper use may lead to memory leaks or dangling pointers. @@ -101,7 +102,6 @@ module GHC.Data.FastString PtrString (..), -- ** Construction - sLit, mkPtrString#, mkPtrString, @@ -675,7 +675,7 @@ mkPtrString# a# = PtrString (Ptr a#) (ptrStrLength (Ptr a#)) -- | Encode a 'String' into a newly allocated 'PtrString' using Latin-1 -- encoding. The original string must not contain non-Latin-1 characters -- (above codepoint @0xff@). -{-# INLINE mkPtrString #-} +{-# NOINLINE[0] mkPtrString #-} -- see rules below mkPtrString :: String -> PtrString mkPtrString s = -- we don't use `unsafeDupablePerformIO` here to avoid potential memory leaks @@ -693,6 +693,9 @@ mkPtrString s = return (PtrString p len) ) +{-# RULES "mkPtrString" + forall x . mkPtrString (unpackCString# x) = mkPtrString# x #-} + -- | Decode a 'PtrString' back into a 'String' using Latin-1 encoding. -- This does not free the memory associated with 'PtrString'. unpackPtrString :: PtrString -> String @@ -714,15 +717,9 @@ ptrStrLength :: Ptr Word8 -> Int {-# INLINE ptrStrLength #-} ptrStrLength (Ptr a) = I# (cstringLength# a) -{-# NOINLINE sLit #-} -sLit :: String -> PtrString -sLit x = mkPtrString x - {-# NOINLINE fsLit #-} fsLit :: String -> FastString fsLit x = mkFastString x -{-# RULES "slit" - forall x . sLit (unpackCString# x) = mkPtrString# x #-} {-# RULES "fslit" forall x . fsLit (unpackCString# x) = mkFastString# x #-} |