Skip to content

Commit 58cfc6c

Browse files
committed
Combine function composition optimisations
They messed with each other when separate. Work really well combined.
1 parent 82a1f79 commit 58cfc6c

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ optimize' js = do
7979
, inlineOperator (C.prelude, (C.#)) $ \x f -> JSApp f [x]
8080
, inlineOperator (C.preludeUnsafe, C.unsafeIndex) $ flip JSIndexer
8181
, inlineCommonOperators
82-
, inlineAppliedArrComposition
8382
, inlineAppliedVars ]) js
8483

8584
untilFixedPoint :: (Monad m, Eq a) => (a -> m a) -> a -> m a

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ module Language.PureScript.CodeGen.JS.Optimizer.Inliner (
1818
inlineValues,
1919
inlineOperator,
2020
inlineCommonOperators,
21-
inlineAppliedArrComposition,
2221
inlineAppliedVars,
2322
inlineArrComposition,
2423
etaConvert,
@@ -241,29 +240,27 @@ inlineCommonOperators = applyAll $
241240
go m acc (JSApp lhs [arg]) = go (m - 1) (arg : acc) lhs
242241
go _ _ _ = Nothing
243242

244-
-- (f << g $ x) = f (g x)
245-
inlineAppliedArrComposition :: JS -> JS
246-
inlineAppliedArrComposition = everywhereOnJS convert
247-
where
248-
convert :: JS -> JS
249-
convert (JSApp (JSApp (JSApp (JSApp fn [dict']) [x]) [y]) [z]) | isDict semigroupoidArr dict' && isPreludeFn (C.<<<) fn = JSApp x [JSApp y [z]]
250-
convert other = other
251-
252243
inlineAppliedVars :: JS -> JS
253244
inlineAppliedVars = everywhereOnJS convert
254245
where
255246
convert :: JS -> JS
256247
convert (JSApp (JSFunction Nothing [a] (JSBlock [JSReturn b])) [JSVar x]) = replaceIdent a (JSVar x) b
257248
convert other = other
258249

250+
-- (f <<< g $ x) = f (g x)
251+
-- (f <<< g) = \x -> f (g x)
259252
inlineArrComposition :: (MonadSupply m) => JS -> m JS
260-
inlineArrComposition js = everywhereOnJSTopDownM convert js
253+
inlineArrComposition = everywhereOnJSTopDownM convert
261254
where
262255
convert :: (MonadSupply m) => JS -> m JS
263-
convert (JSApp (JSApp (JSApp fn [dict']) [x]) [y]) | isDict semigroupoidArr dict' && isPreludeFn (C.<<<) fn = do
256+
convert (JSApp (JSApp (JSApp (JSApp fn [dict']) [x]) [y]) [z]) | isArrCompose dict' fn =
257+
return $ JSApp x [JSApp y [z]]
258+
convert (JSApp (JSApp (JSApp fn [dict']) [x]) [y]) | isArrCompose dict' fn = do
264259
arg <- freshName
265260
return $ JSFunction Nothing [arg] (JSBlock [JSReturn $ JSApp x [JSApp y [JSVar arg]]])
266261
convert other = return other
262+
isArrCompose :: JS -> JS -> Bool
263+
isArrCompose dict' fn = isDict semigroupoidArr dict' && isPreludeFn (C.<<<) fn
267264

268265
isDict :: (String, String) -> JS -> Bool
269266
isDict (moduleName, dictName) (JSAccessor x (JSVar y)) = x == dictName && y == moduleName

0 commit comments

Comments
 (0)