Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow list fusion for Text and Text.Lazy unpack #629

Merged
merged 1 commit into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions benchmarks/haskell/Benchmarks/Pure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ benchmark kind ~Env{..} =
[ benchT $ nf (T.zipWith min tb) ta
, benchTL $ nf (TL.zipWith min tlb) tla
]
, bgroup "length . unpack" -- length should fuse with unpack
[ benchT $ nf (L.length . T.unpack) ta
, benchTL $ nf (L.length . TL.unpack) tla
]
, bgroup "length . drop 1 . unpack" -- no list fusion because of drop 1
[ benchT $ nf (L.length . L.drop 1 . T.unpack) ta
, benchTL $ nf (L.length . L.drop 1 . TL.unpack) tla
]
, bgroup "length"
[ bgroup "cons"
[ benchT $ nf (T.length . T.cons c) ta
Expand Down
18 changes: 17 additions & 1 deletion src/Data/Text/Lazy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,23 @@ unpack ::
#endif
Text -> String
unpack t = S.unstreamList (stream t)
{-# INLINE [1] unpack #-}
{-# NOINLINE unpack #-}

foldrFB :: (Char -> b -> b) -> b -> Text -> b
foldrFB = foldr
{-# INLINE [0] foldrFB #-}

-- List fusion rules for `unpack`:
-- * `unpack` rewrites to `build` up till (but not including) phase 1. `build`
-- fuses if `foldr` is applied to it.
-- * If it doesn't fuse: In phase 1, `build` inlines to give us `foldrFB (:) []`
-- and we rewrite that back to `unpack`.
-- * If it fuses: In phase 0, `foldrFB` inlines and `foldr` inlines. GHC
-- optimizes the fused code.
{-# RULES
"Text.Lazy.unpack" [~1] forall t. unpack t = Exts.build (\lcons lnil -> foldrFB lcons lnil t)
"Text.Lazy.unpackBack" [1] foldrFB (:) [] = unpack
#-}

-- | /O(n)/ Convert a literal string into a Text.
unpackCString# :: Addr# -> Text
Expand Down
29 changes: 25 additions & 4 deletions src/Data/Text/Show.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import Data.Text.Internal.Encoding.Utf8 (utf8Length)
import Data.Text.Internal.Unsafe.Char (unsafeWrite)
import Data.Text.Unsafe (Iter(..), iterArray)
import GHC.Exts (Ptr(..), Int(..), Addr#, indexWord8OffAddr#)
import qualified GHC.Exts as Exts
import GHC.Word (Word8(..))
import qualified Data.Text.Array as A
#if !MIN_VERSION_ghc_prim(0,7,0)
Expand All @@ -53,12 +54,32 @@ unpack ::
HasCallStack =>
#endif
Text -> String
unpack (Text arr off len) = go off
unpack t = foldrText (:) [] t
{-# NOINLINE unpack #-}

foldrText :: (Char -> b -> b) -> b -> Text -> b
foldrText f z (Text arr off len) = go off
where
go !i
| i >= off + len = []
| otherwise = let !(Iter c l) = iterArray arr i in c : go (i + l)
{-# INLINE [1] unpack #-}
| i >= off + len = z
| otherwise = let !(Iter c l) = iterArray arr i in f c (go (i + l))
{-# INLINE foldrText #-}
Comment on lines +60 to +66
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This can, and maybe should, use Data.Text.foldr. But it was already here, so I didn't change that.


foldrTextFB :: (Char -> b -> b) -> b -> Text -> b
foldrTextFB = foldrText
{-# INLINE [0] foldrTextFB #-}

-- List fusion rules for `unpack`:
-- * `unpack` rewrites to `build` up till (but not including) phase 1. `build`
-- fuses if `foldr` is applied to it.
-- * If it doesn't fuse: In phase 1, `build` inlines to give us
-- `foldrTextFB (:) []` and we rewrite that back to `unpack`.
-- * If it fuses: In phase 0, `foldrTextFB` inlines and `foldrText` inlines. GHC
-- optimizes the fused code.
{-# RULES
"Text.unpack" [~1] forall t. unpack t = Exts.build (\lcons lnil -> foldrTextFB lcons lnil t)
"Text.unpackBack" [1] foldrTextFB (:) [] = unpack
#-}

-- | /O(n)/ Convert a null-terminated
-- <https://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8 modified UTF-8>
Expand Down
Loading