Skip to content

Instantly share code, notes, and snippets.

@ndmitchell
Last active June 20, 2018 19:09
Show Gist options
  • Save ndmitchell/54e58f23613bc074b79e9e915dec03bf to your computer and use it in GitHub Desktop.
Save ndmitchell/54e58f23613bc074b79e9e915dec03bf to your computer and use it in GitHub Desktop.
GHC optimisation differences
Output from GhcApi.hs
s1 :: Addr#
[GblId, Caf=NoCafRefs]
s1 = "test"#
s2 :: [Char]
[GblId]
s2 = unpackCString# s1
s3 :: Record
[GblId]
s3 = Record s2
pick :: String
[GblId]
pick = case s3 of { Record ds -> ds }
Output from ghc -ddump-simpl
-- RHS size: {terms: 1, types: 0, coercions: 0, joins: 0/0}
LensOpt.pick1 :: GHC.Prim.Addr#
[GblId,
Caf=NoCafRefs,
Unf=Unf{Src=<vanilla>, TopLvl=True, Value=True, ConLike=True,
WorkFree=True, Expandable=True, Guidance=IF_ARGS [] 20 0}]
LensOpt.pick1 = "test"#
-- RHS size: {terms: 2, types: 0, coercions: 0, joins: 0/0}
pick :: String
[GblId,
Unf=Unf{Src=<vanilla>, TopLvl=True, Value=False, ConLike=True,
WorkFree=False, Expandable=True, Guidance=IF_ARGS [] 20 0}]
pick = GHC.CString.unpackCString# LensOpt.pick1
-- Program used to compile LensOpt.hs using the GHC API
import GHC
import GhcPlugins
import GHC.Paths
main = runGhc (Just libdir) $ do
setTargets []
dflags <- getSessionDynFlags
setSessionDynFlags dflags{hscTarget = HscNothing}
res <- compileToCoreSimplified "LensOpt.hs"
liftIO $ writeFile "lens_api.txt" $ showSDoc dflags $ ppr res
-- Program under test that I would like optimising
{-# OPTIONS_GHC -O2 #-}
module LensOpt(pick) where
import Control.Lens
data Record = Record {_field :: String}
field = lens _field $ \r x -> r{_field=x}
pick = Record "test" ^. field
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment