diff options
author | Sylvain Henry <sylvain@haskus.fr> | 2021-12-20 14:56:41 +0100 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2022-01-06 02:24:54 -0500 |
commit | c080b44314248545c6ddea0c0eff02f8c9edbca4 (patch) | |
tree | 57eedf35d743c95e720f8ac6fe003b7f716d1fc8 /compiler/GHC/Builtin/Utils.hs | |
parent | e59bd46a6915c79e89d376aa22b0ae6def440e0a (diff) | |
download | haskell-c080b44314248545c6ddea0c0eff02f8c9edbca4.tar.gz |
Perf: use SmallArray for primops' Ids cache (#20857)
SmallArray doesn't perform bounds check (faster).
Make primop tags start at 0 to avoid index arithmetic.
Diffstat (limited to 'compiler/GHC/Builtin/Utils.hs')
-rw-r--r-- | compiler/GHC/Builtin/Utils.hs | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/compiler/GHC/Builtin/Utils.hs b/compiler/GHC/Builtin/Utils.hs index 9d91b1246d..4428716681 100644 --- a/compiler/GHC/Builtin/Utils.hs +++ b/compiler/GHC/Builtin/Utils.hs @@ -78,10 +78,10 @@ import GHC.Hs.Doc import GHC.Unit.Module.ModIface (IfaceExport) import GHC.Data.List.SetOps +import GHC.Data.SmallArray import Control.Applicative ((<|>)) import Data.List ( intercalate , find ) -import Data.Array import Data.Maybe import qualified Data.Map as Map @@ -133,7 +133,7 @@ knownKeyNames , concatMap wired_tycon_kk_names wiredInTyCons , concatMap wired_tycon_kk_names typeNatTyCons , map idName wiredInIds - , map (idName . primOpId) allThePrimOps + , map idName allThePrimOpIds , map (idName . primOpWrapperId) allThePrimOps , basicKnownKeyNames , templateHaskellNames @@ -238,13 +238,21 @@ sense of them in interface pragmas. It's cool, though they all have ************************************************************************ -} -primOpIds :: Array Int Id --- A cache of the PrimOp Ids, indexed by PrimOp tag -primOpIds = array (1,maxPrimOpTag) [ (primOpTag op, mkPrimOpId op) - | op <- allThePrimOps ] +-- | A cache of the PrimOp Ids, indexed by PrimOp tag (0 indexed) +primOpIds :: SmallArray Id +{-# NOINLINE primOpIds #-} +primOpIds = listToArray (maxPrimOpTag+1) primOpTag mkPrimOpId allThePrimOps +-- | Get primop id. +-- +-- Retrieve it from `primOpIds` cache without performing bounds checking. primOpId :: PrimOp -> Id -primOpId op = primOpIds ! primOpTag op +primOpId op = indexSmallArray primOpIds (primOpTag op) + +-- | All the primop ids, as a list +allThePrimOpIds :: [Id] +{-# INLINE allThePrimOpIds #-} +allThePrimOpIds = map (indexSmallArray primOpIds) [0..maxPrimOpTag] {- ************************************************************************ @@ -257,7 +265,7 @@ primOpId op = primOpIds ! primOpTag op ghcPrimExports :: [IfaceExport] ghcPrimExports = map (avail . idName) ghcPrimIds ++ - map (avail . idName . primOpId) allThePrimOps ++ + map (avail . idName) allThePrimOpIds ++ [ availTC n [n] [] | tc <- exposedPrimTyCons, let n = tyConName tc ] @@ -265,7 +273,7 @@ ghcPrimDeclDocs :: DeclDocMap ghcPrimDeclDocs = DeclDocMap $ Map.fromList $ mapMaybe findName primOpDocs where names = map idName ghcPrimIds ++ - map (idName . primOpId) allThePrimOps ++ + map idName allThePrimOpIds ++ map tyConName exposedPrimTyCons findName (nameStr, doc) | Just name <- find ((nameStr ==) . getOccString) names |