git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Workaround for TCO/composition inlining interaction
commit5481c38d2f128b9c4929f330b6cffba17fcc42ca authorGary Burgess <gary.burgess@gmail.com> dateMon 25 Jan 2016, 15:02 (11 years ago) parentca66265
3 files changed+37−13
examples/passing/TCO.purs+20 src/Language/PureScript/CodeGen/JS/Optimizer.hs+14−10 src/Language/PureScript/CodeGen/JS/Optimizer/Inliner.hs+3−3
examples/passing/TCO.pursnew file+20
@@ -0,0 +1,20 @@
1+module Main where
2+
3+import Prelude
4+import Control.Monad.Eff.Console (print)
5+
6+main = do
7+ let f x = x + 1
8+ let v = 0
9+ print (applyN 0 f v)
10+ print (applyN 1 f v)
11+ print (applyN 2 f v)
12+ print (applyN 3 f v)
13+ print (applyN 4 f v)
14+
15+applyN :: forall a. Int -> (a -> a) -> a -> a
16+applyN = go id
17+ where
18+ go f n _ | n <= 0 = f
19+ go f n g = go (f >>> g) (n - 1) g
20+
src/Language/PureScript/CodeGen/JS/Optimizer.hs+14−10
@@ -51,11 +51,21 @@ optimize js = do
5151optimize' :: (Monad m, MonadReader Options m, Applicative m, MonadSupply m) => JS -> m JS
5252optimize' js = do
5353 opts <- ask
54− untilFixedPoint (inlineFnComposition . applyAll
54+ js' <- untilFixedPoint (inlineFnComposition . tidyUp . applyAll
55+ [ inlineCommonValues
56+ , inlineOperator (C.prelude, (C.$)) $ \f x -> JSApp f [x]
57+ , inlineOperator (C.dataFunction, C.apply) $ \f x -> JSApp f [x]
58+ , inlineOperator (C.prelude, (C.#)) $ \x f -> JSApp f [x]
59+ , inlineOperator (C.dataFunction, C.applyFlipped) $ \x f -> JSApp f [x]
60+ , inlineOperator (C.dataArrayUnsafe, C.unsafeIndex) $ flip JSIndexer
61+ , inlineCommonOperators
62+ ]) js
63+ untilFixedPoint (return . tidyUp) . tco opts . magicDo opts $ js'
64+ where
65+ tidyUp :: JS -> JS
66+ tidyUp = applyAll
5567 [ collapseNestedBlocks
5668 , collapseNestedIfs
57− , tco opts
58− , magicDo opts
5969 , removeCodeAfterReturnStatements
6070 , removeUnusedArg
6171 , removeUndefinedApp
@@ -63,13 +73,7 @@ optimize' js = do
6373 , etaConvert
6474 , evaluateIifes
6575 , inlineVariables
66− , inlineValues
67− , inlineOperator (C.prelude, (C.$)) $ \f x -> JSApp f [x]
68− , inlineOperator (C.dataFunction, C.apply) $ \f x -> JSApp f [x]
69− , inlineOperator (C.prelude, (C.#)) $ \x f -> JSApp f [x]
70− , inlineOperator (C.dataFunction, C.applyFlipped) $ \x f -> JSApp f [x]
71− , inlineOperator (C.dataArrayUnsafe, C.unsafeIndex) $ flip JSIndexer
72− , inlineCommonOperators ]) js
76+ ]
7377
7478untilFixedPoint :: (Monad m, Eq a) => (a -> m a) -> a -> m a
7579untilFixedPoint f = go
src/Language/PureScript/CodeGen/JS/Optimizer/Inliner.hs+3−3
@@ -3,7 +3,7 @@
33--
44module Language.PureScript.CodeGen.JS.Optimizer.Inliner
55 ( inlineVariables
6− , inlineValues
6+ , inlineCommonValues
77 , inlineOperator
88 , inlineCommonOperators
99 , inlineFnComposition
@@ -77,8 +77,8 @@ inlineVariables = everywhereOnJS $ removeFromBlock go
7777 go (map (replaceIdent var js) sts)
7878 go (s:sts) = s : go sts
7979
80−inlineValues :: JS -> JS
81−inlineValues = everywhereOnJS convert
80+inlineCommonValues :: JS -> JS
81+inlineCommonValues = everywhereOnJS convert
8282 where
8383 convert :: JS -> JS
8484 convert (JSApp fn [dict])