[Git][ghc/ghc][master] compiler: avoid unused temporary `appendFS` operands
Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC Commits: 5574ee10 by Cheng Shao at 2026-04-24T08:24:30-04:00 compiler: avoid unused temporary `appendFS` operands This patch fixes unused temporary `appendFS` operands in the codebase that are retained in the `FastString` table after concatenation. Rewrite rules are added so that if an operand is `fsLit`/`mkFastString`, the `appendFS` application is rewritten to append the `ShortByteString` operands first. The patch also fixes `sconcat` behavior to align with `mconcat` for the same reason. Fixes #27205. - - - - - 1 changed file: - compiler/GHC/Data/FastString.hs Changes: ===================================== compiler/GHC/Data/FastString.hs ===================================== @@ -139,6 +139,7 @@ import Foreign.C import System.IO import Data.Data import Data.IORef +import qualified Data.List.NonEmpty as NE import Data.Semigroup as Semi import Foreign @@ -232,6 +233,7 @@ instance IsString FastString where instance Semi.Semigroup FastString where (<>) = appendFS + sconcat = concatFS . NE.toList instance Monoid FastString where mempty = nilFS @@ -619,6 +621,42 @@ unpackFS fs = utf8DecodeShortByteString $ fs_sbs fs zEncodeFS :: FastString -> FastZString zEncodeFS fs = fs_zenc fs +-- Sometimes an `appendFS` operand is temporarily constructed, and we +-- should avoid retaining the unused `FastString` operand in the +-- table. The RULES below mitigate the issue by concatenating the +-- `ShortByteString`s instead when an operand is `fsLit` or +-- `mkFastString`, which cover most such `appendFS` use cases. See +-- #27205. + +{-# RULES +"appendFS/fsLit y" forall x y. + appendFS x (fsLit y) = + mkFastStringShortByteString $ + fs_sbs x Semi.<> utf8EncodeShortByteString y + #-} + +{-# RULES +"appendFS/fsLit x" forall x y. + appendFS (fsLit x) y = + mkFastStringShortByteString $ + utf8EncodeShortByteString x Semi.<> fs_sbs y + #-} + +{-# RULES +"appendFS/mkFastString y" forall x y. + appendFS x (mkFastString y) = + mkFastStringShortByteString $ + fs_sbs x Semi.<> utf8EncodeShortByteString y + #-} + +{-# RULES +"appendFS/mkFastString x" forall x y. + appendFS (mkFastString x) y = + mkFastStringShortByteString $ + utf8EncodeShortByteString x Semi.<> fs_sbs y + #-} + +{-# INLINE[1] appendFS #-} appendFS :: FastString -> FastString -> FastString appendFS fs1 fs2 = mkFastStringShortByteString $ (Semi.<>) (fs_sbs fs1) (fs_sbs fs2) View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/5574ee101d22fa61d90f9d4d4a71be8c... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/5574ee101d22fa61d90f9d4d4a71be8c... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
Marge Bot (@marge-bot)