Skip to content

Commit bc79f06

Browse files
committed
Add inlineAppliedVars optimisation
Translates the following: (function(a) { return a * a * a; })(b) Into: b * b * b
1 parent c4c5830 commit bc79f06

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/Language/PureScript/CodeGen/JS/Optimizer.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ optimize' js = do
7878
, inlineOperator (C.prelude, (C.#)) $ \x f -> JSApp f [x]
7979
, inlineOperator (C.preludeUnsafe, C.unsafeIndex) $ flip JSIndexer
8080
, inlineCommonOperators
81-
, inlineAppliedArrComposition ]) js
81+
, inlineAppliedArrComposition
82+
, inlineAppliedVars ]) js
8283

8384
untilFixedPoint :: (Monad m, Eq a) => (a -> m a) -> a -> m a
8485
untilFixedPoint f = go

src/Language/PureScript/CodeGen/JS/Optimizer/Inliner.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module Language.PureScript.CodeGen.JS.Optimizer.Inliner (
1919
inlineOperator,
2020
inlineCommonOperators,
2121
inlineAppliedArrComposition,
22+
inlineAppliedVars,
2223
etaConvert,
2324
unThunk,
2425
evaluateIifes
@@ -246,6 +247,13 @@ inlineAppliedArrComposition = everywhereOnJS convert
246247
convert (JSApp (JSApp (JSApp (JSApp fn [dict']) [x]) [y]) [z]) | isDict semigroupoidArr dict' && isPreludeFn (C.<<<) fn = JSApp x [JSApp y [z]]
247248
convert other = other
248249

250+
inlineAppliedVars :: JS -> JS
251+
inlineAppliedVars = everywhereOnJS convert
252+
where
253+
convert :: JS -> JS
254+
convert (JSApp (JSFunction Nothing [a] (JSBlock [JSReturn b])) [JSVar x]) = replaceIdent a (JSVar x) b
255+
convert other = other
256+
249257
isDict :: (String, String) -> JS -> Bool
250258
isDict (moduleName, dictName) (JSAccessor x (JSVar y)) = x == dictName && y == moduleName
251259
isDict _ _ = False

0 commit comments

Comments
 (0)