Note [Deterministic UniqFM]
A @UniqDFM@ is just like @UniqFM@ with the following additional property: the function `udfmToList` returns the elements in some deterministic order not depending on the Unique key for those elements. If the client of the map performs operations on the map in deterministic order then `udfmToList` returns them in deterministic order. There is an implementation cost: each element is given a serial number as it is added, and `udfmToList` sorts its result by this serial number. So you should only use `UniqDFM` if you need the deterministic property. `foldUDFM` also preserves determinism. Normal @UniqFM@ when you turn it into a list will use Data.IntMap.toList function that returns the elements in the order of the keys. The keys in @UniqFM@ are always @Uniques@, so you end up with with a list ordered by @Uniques@. The order of @Uniques@ is known to be not stable across rebuilds. See Note [Unique Determinism] in GHC.Types.Unique. There's more than one way to implement this. The implementation here tags every value with the insertion time that can later be used to sort the values when asked to convert to a list. An alternative would be to have data UniqDFM ele = UDFM (M.IntMap ele) [ele] where the list determines the order. This makes deletion tricky as we'd only accumulate elements in that list, but makes merging easier as you can just merge both structures independently. Deletion can probably be done in amortized fashion when the size of the list is twice the size of the set.
References 1
- Unique Determinism GHC.Types.Unique
Referenced by 39
- GHC.Types.Unique.FM call site ×9
- GHC.Types.Var.Set call site ×4
- GHC.Types.Unique.Set call site ×3
- GHC.Data.FastString.Env call site ×2
- GHC.Tc.Types.Evidence call site ×2
- GHC.Types.Unique.DSet call site ×2
- GHC.Types.Var.Env call site ×2
- FamInstEnv determinism GHC.Core.FamInstEnv
- InstEnv determinism GHC.Core.InstEnv
- GHC.Core.Opt.Specialise call site
- GHC.Core.TyCon.Env call site
- foldTM determinism GHC.Data.TrieMap
- GHC.Iface.Make call site
- Deterministic ApplicativeDo and RecursiveDo desugaring GHC.Rename.Expr
- CandidatesQTvs determinism and order GHC.Tc.Utils.TcMType
- quantifyTyVars determinism GHC.Tc.Utils.TcMType
- GHC.Types.Name.Env call site
- GHC.Types.Name.Set call site
- No Ord for Unique GHC.Types.Unique
- ModuleEnv performance and determinism GHC.Unit.Module.Env
- Deterministic FV GHC.Utils.FV
- GHC.Utils.FV call site