1{-# OPTIONS_GHC -Wall #-}
2module Optimize.Patterns (optimize) where
4import Control.Arrow (second)
5import qualified Data.Map as Map
7import qualified Data.Maybe as Maybe
9import qualified AST.Expression.Optimized as Opt
10import qualified AST.Pattern as P
11import Elm.Utils ((|>))
12import qualified Optimize.Environment as Env
13import qualified Optimize.Patterns.DecisionTree as DT
14import qualified Reporting.Annotation as A
15import qualified Reporting.Region as R
22 -> [(P.CanonicalPattern, Opt.Expr)]
23 -> Env.Optimizer Opt.Expr
24optimize variantDict region optExpr optBranches =
26 indexify index (pattern, branch) =
32 case last optBranches of
33 (A.A ann P.Anything, Opt.Call (Opt.Crash _ _) [arg]) ->
34 do name <- Env.freshName
35 let newPattern = A.A ann (P.Var name)
36 let newCall = Opt.Call (Opt.Crash region (Just name)) [arg]
37 return (init optBranches ++ [(newPattern, newCall)])
42 let (patterns, indexedBranches) =
43 unzip (zipWith indexify [0..] crashBranches)
46 DT.compile variantDict patterns
48 tempName <- Env.freshName
52 [ Opt.Def Opt.dummyFacts tempName optExpr ]
53 (inlinedCase tempName decisionTree indexedBranches)
58 -> DT.DecisionTree Int
61inlinedCase name decisionTree jumpTargets =
64 countTargets decisionTree
66 (decisions, sharedBranches) =
67 unzip (map (toDecisions targetCounts) jumpTargets)
71 (inlineDecisions (Map.fromList decisions) decisionTree)
72 (Maybe.catMaybes sharedBranches)
75countTargets :: DT.DecisionTree Int -> Map.Map Int Int
76countTargets decisionTree =
78 DT.Decision _ edges fallback ->
80 |> maybe id (:) fallback
85 Map.singleton target 1
91 -> ( (Int, Opt.Decision), Maybe (Int, Opt.Expr) )
92toDecisions targetCounts (target, branch) =
93 if targetCounts ! target == 1 then
94 ( (target, Opt.Inline branch)
99 ( (target, Opt.Jump target)
100 , Just (target, branch)
105 :: Map.Map Int Opt.Decision
106 -> DT.DecisionTree Int
107 -> DT.DecisionTree Opt.Decision
108inlineDecisions jumpDict decisionTree =
111 inlineDecisions jumpDict
114 DT.Decision test edges fallback ->
115 DT.Decision test (map (second go) edges) (fmap go fallback)
118 DT.Match (jumpDict ! target)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list