Skip to content

Inlining Record Selectors casues retainer thunks

I was spelunking around in ghc-debug and noticed a thunk arising from:

; let field_env = extendNameEnvList (tcg_field_env tcg_env) flds

In particular, the application of tcg_field_env tcg_env causes a thunk to retain the whole tcg_env.

@bgamari might ask, why is this not compiled to a selector thunk?

Well if you look at the STG you find that the tcg_field_env selector has been inlined which leaves the pleasing code:

let {
                                                    field_env_sti3
                                                      :: GHC.Types.Name.Env.NameEnv
                                                           [GHC.Types.FieldLabel.FieldLabel]
                                                    [LclId] =
                                                        {flds_sthV, tcg_env_sthT} \u []
                                                            case tcg_env_sthT of {
                                                            GHC.Tc.Types.TcGblEnv _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  ds98_stib [Occ=Once1]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]
                                                                                  _ [Occ=Dead]

Which seems that the selector has been inlined.

The conclusion seems to be that inlining a selector can regress memory usage due to creating an additional retainer, where the user might have expected the selector optimization to kick-in.

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information