git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Optimise (f <<< g $ x) as (f (g x))
d8d96cfBrian McKenna
src/Language/PureScript/CodeGen/JS/Optimizer.hs+2−1
@@ -77,7 +77,8 @@ optimize' js = do
77 , inlineOperator (C.prelude, (C.$)) $ \f x -> JSApp f [x]
77 , inlineOperator (C.prelude, (C.$)) $ \f x -> JSApp f [x]
78 , inlineOperator (C.prelude, (C.#)) $ \x f -> JSApp f [x]
78 , inlineOperator (C.prelude, (C.#)) $ \x f -> JSApp f [x]
79 , inlineOperator (C.preludeUnsafe, C.unsafeIndex) $ flip JSIndexer
79 , inlineOperator (C.preludeUnsafe, C.unsafeIndex) $ flip JSIndexer
80− , inlineCommonOperators ]) js
80+ , inlineCommonOperators
81+ , inlineAppliedArrComposition ]) js
81
82
82untilFixedPoint :: (Eq a) => (a -> a) -> a -> a
83untilFixedPoint :: (Eq a) => (a -> a) -> a -> a
83untilFixedPoint f = go
84untilFixedPoint f = go
src/Language/PureScript/CodeGen/JS/Optimizer/Inliner.hs+16
@@ -18,6 +18,7 @@ module Language.PureScript.CodeGen.JS.Optimizer.Inliner (
18 inlineValues,
18 inlineValues,
19 inlineOperator,
19 inlineOperator,
20 inlineCommonOperators,
20 inlineCommonOperators,
21+ inlineAppliedArrComposition,
21 etaConvert,
22 etaConvert,
22 unThunk,
23 unThunk,
23 evaluateIifes
24 evaluateIifes
@@ -31,6 +32,10 @@ import Language.PureScript.Names
31import Language.PureScript.CodeGen.JS.Optimizer.Common
32import Language.PureScript.CodeGen.JS.Optimizer.Common
32import qualified Language.PureScript.Constants as C
33import qualified Language.PureScript.Constants as C
33
34
35+-- TODO: Potential bug:
36+-- Shouldn't just inline this case: { var x = 0; x.toFixed(10); }
37+-- Needs to be: { 0..toFixed(10); }
38+-- Probably needs to be fixed in pretty-printer instead.
34shouldInline :: JS -> Bool
39shouldInline :: JS -> Bool
35shouldInline (JSVar _) = True
40shouldInline (JSVar _) = True
36shouldInline (JSNumericLiteral _) = True
41shouldInline (JSNumericLiteral _) = True
@@ -233,6 +238,14 @@ inlineCommonOperators = applyAll $
233 go m acc (JSApp lhs [arg]) = go (m - 1) (arg : acc) lhs
238 go m acc (JSApp lhs [arg]) = go (m - 1) (arg : acc) lhs
234 go _ _ _ = Nothing
239 go _ _ _ = Nothing
235
240
241+-- (f << g $ x) = f (g x)
242+inlineAppliedArrComposition :: JS -> JS
243+inlineAppliedArrComposition = everywhereOnJS convert
244+ where
245+ convert :: JS -> JS
246+ convert (JSApp (JSApp (JSApp (JSApp fn [dict']) [x]) [y]) [z]) | isDict semigroupoidArr dict' && isPreludeFn (C.<<<) fn = JSApp x [JSApp y [z]]
247+ convert other = other
248+
236isDict :: (String, String) -> JS -> Bool
249isDict :: (String, String) -> JS -> Bool
237isDict (moduleName, dictName) (JSAccessor x (JSVar y)) = x == dictName && y == moduleName
250isDict (moduleName, dictName) (JSAccessor x (JSVar y)) = x == dictName && y == moduleName
238isDict _ _ = False
251isDict _ _ = False
@@ -292,3 +305,6 @@ latticeBoolean = (C.prelude, C.latticeBoolean)
292
305
293complementedLatticeBoolean :: (String, String)
306complementedLatticeBoolean :: (String, String)
294complementedLatticeBoolean = (C.prelude, C.complementedLatticeBoolean)
307complementedLatticeBoolean = (C.prelude, C.complementedLatticeBoolean)
308+
309+semigroupoidArr :: (String, String)
310+semigroupoidArr = (C.prelude, C.semigroupoidArr)
src/Language/PureScript/Constants.hs+3
@@ -83,6 +83,9 @@ unsafeIndex = "unsafeIndex"
83(.^.) :: String
83(.^.) :: String
84(.^.) = ".^."
84(.^.) = ".^."
85
85
86+(<<<) :: String
87+(<<<) = "<<<"
88+
86-- Functions
89-- Functions
87
90
88negate :: String
91negate :: String