git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Some optimizer improvements
3f20972Phil Freeman
src/Language/PureScript/Optimizer.hs+4−2
@@ -53,19 +53,21 @@ import Language.PureScript.Optimizer.Blocks
53--
53--
54optimize :: Options -> JS -> JS
54optimize :: Options -> JS -> JS
55optimize opts | optionsNoOptimizations opts = id
55optimize opts | optionsNoOptimizations opts = id
56− | otherwise = untilFixedPoint $ applyAll
56+ | otherwise = untilFixedPoint (applyAll
57 [ collapseNestedBlocks
57 [ collapseNestedBlocks
58 , tco opts
58 , tco opts
59 , magicDo opts
59 , magicDo opts
60 , removeUnusedVariables
60 , removeUnusedVariables
61+ , removeCodeAfterReturnStatements
61 , unThunk
62 , unThunk
62 , etaConvert
63 , etaConvert
64+ , evaluateIifes
63 , inlineVariables
65 , inlineVariables
64 , inlineOperator (C.$) $ \f x -> JSApp f [x]
66 , inlineOperator (C.$) $ \f x -> JSApp f [x]
65 , inlineOperator (C.#) $ \x f -> JSApp f [x]
67 , inlineOperator (C.#) $ \x f -> JSApp f [x]
66 , inlineOperator (C.!!) $ flip JSIndexer
68 , inlineOperator (C.!!) $ flip JSIndexer
67 , inlineOperator (C.++) $ JSBinary Add
69 , inlineOperator (C.++) $ JSBinary Add
68− , inlineCommonOperators ]
70+ , inlineCommonOperators ])
69
71
70untilFixedPoint :: (Eq a) => (a -> a) -> a -> a
72untilFixedPoint :: (Eq a) => (a -> a) -> a -> a
71untilFixedPoint f = go
73untilFixedPoint f = go
src/Language/PureScript/Optimizer/Inliner.hs+9−1
@@ -18,7 +18,8 @@ module Language.PureScript.Optimizer.Inliner (
18 inlineOperator,
18 inlineOperator,
19 inlineCommonOperators,
19 inlineCommonOperators,
20 etaConvert,
20 etaConvert,
21− unThunk
21+ unThunk,
22+ evaluateIifes
22) where
23) where
23
24
24import Data.Generics
25import Data.Generics
@@ -57,6 +58,13 @@ unThunk = everywhere (mkT convert)
57 convert (JSBlock [JSReturn (JSApp (JSFunction Nothing [] (JSBlock body)) [])]) = JSBlock body
58 convert (JSBlock [JSReturn (JSApp (JSFunction Nothing [] (JSBlock body)) [])]) = JSBlock body
58 convert js = js
59 convert js = js
59
60
61+evaluateIifes :: JS -> JS
62+evaluateIifes = everywhere (mkT convert)
63+ where
64+ convert :: JS -> JS
65+ convert (JSApp (JSFunction Nothing [] (JSBlock [JSReturn ret])) []) = ret
66+ convert js = js
67+
60inlineVariables :: JS -> JS
68inlineVariables :: JS -> JS
61inlineVariables = everywhere (mkT $ removeFromBlock go)
69inlineVariables = everywhere (mkT $ removeFromBlock go)
62 where
70 where
src/Language/PureScript/Optimizer/Unused.hs+11−1
@@ -14,7 +14,8 @@
14-----------------------------------------------------------------------------
14-----------------------------------------------------------------------------
15
15
16module Language.PureScript.Optimizer.Unused (
16module Language.PureScript.Optimizer.Unused (
17− removeUnusedVariables
17+ removeUnusedVariables,
18+ removeCodeAfterReturnStatements
18) where
19) where
19
20
20import Data.Generics
21import Data.Generics
@@ -29,3 +30,12 @@ removeUnusedVariables = everywhere (mkT $ removeFromBlock go)
29 go [] = []
30 go [] = []
30 go (JSVariableIntroduction var _ : sts) | not (isUsed var sts) = go sts
31 go (JSVariableIntroduction var _ : sts) | not (isUsed var sts) = go sts
31 go (s:sts) = s : go sts
32 go (s:sts) = s : go sts
33+
34+removeCodeAfterReturnStatements :: JS -> JS
35+removeCodeAfterReturnStatements = everywhere (mkT $ removeFromBlock go)
36+ where
37+ go :: [JS] -> [JS]
38+ go jss | not (any isJSReturn jss) = jss
39+ | otherwise = let (body, ret : _) = span (not . isJSReturn) jss in body ++ [ret]
40+ isJSReturn (JSReturn _) = True
41+ isJSReturn _ = False