git.iko.soy/rowtype-yoga/purescript-backend-optimizer
on main help sign in
Generate empty CtorSaturated, optimize it in codegen instead (#98)
54b82acVerity Scheel
backend-es/src/PureScript/Backend/Optimizer/Codegen/EcmaScript/Convert.purs+2
@@ -293,6 +293,8 @@ codegenExpr env@(CodegenEnv { currentModule, inlineApp }) tcoExpr@(TcoExpr _ exp
293 [ build $ EsReturn $ Just $ codegenCtor env currentModule ct ty tag $
293 [ build $ EsReturn $ Just $ codegenCtor env currentModule ct ty tag $
294 (build <<< EsIdent <<< Qualified Nothing <<< toEsIdent <<< Ident) <$> fields
294 (build <<< EsIdent <<< Qualified Nothing <<< toEsIdent <<< Ident) <$> fields
295 ]
295 ]
296+ CtorSaturated qual _ _ _ [] ->
297+ codegenExpr env (TcoExpr mempty (Var qual))
296 CtorSaturated (Qualified qual _) ct ty tag fields ->
298 CtorSaturated (Qualified qual _) ct ty tag fields ->
297 codegenCtor env (fromMaybe currentModule qual) ct ty tag (codegenExpr env <<< snd <$> fields)
299 codegenCtor env (fromMaybe currentModule qual) ct ty tag (codegenExpr env <<< snd <$> fields)
298 PrimOp op ->
300 PrimOp op ->
backend-es/test/snapshots-out/Snapshot.CaseLeafTco.js+11−19
@@ -1,44 +1,36 @@
1−import * as $runtime from "../runtime.js";
2−import * as Data$dArray from "../Data.Array/index.js";
3const test1 = test1$a0$copy => test1$a1$copy => {
1const test1 = test1$a0$copy => test1$a1$copy => {
4 let test1$a0 = test1$a0$copy, test1$a1 = test1$a1$copy, test1$c = true, test1$r;
2 let test1$a0 = test1$a0$copy, test1$a1 = test1$a1$copy, test1$c = true, test1$r;
5 while (test1$c) {
3 while (test1$c) {
6 const b = test1$a0, arr = test1$a1;
4 const b = test1$a0, arr = test1$a1;
7− const v = Data$dArray.last(arr);
5+ const $0 = arr.length - 1 | 0;
8− if (0 < arr.length) {
6+ if ($0 >= 0 && $0 < arr.length) {
9− if (v.tag === "Just") {
7+ if (0 < arr.length) {
10− if (v._1 === 2 && arr[0] === 1) {
8+ if (arr[$0] === 2 && arr[0] === 1) {
11 test1$c = false;
9 test1$c = false;
12 test1$r = arr;
10 test1$r = arr;
13 continue;
11 continue;
14 }
12 }
13+ const $1 = arr[$0];
15 if (b) {
14 if (b) {
16 test1$c = false;
15 test1$c = false;
17 test1$r = [];
16 test1$r = [];
18 continue;
17 continue;
19 }
18 }
20 test1$a0 = b;
19 test1$a0 = b;
21− test1$a1 = [v._1, arr[0], 3, v._1, 5, 6, 7, 8, 9, 10, arr[0], 12, 13, 14, 15, 16, 17, ...arr];
20+ test1$a1 = [$1, arr[0], 3, $1, 5, 6, 7, 8, 9, 10, arr[0], 12, 13, 14, 15, 16, 17, ...arr];
22− continue;
23− }
24− if (v.tag === "Nothing") {
25− test1$c = false;
26− test1$r = [...arr, arr[0]];
27 continue;
21 continue;
28 }
22 }
29− $runtime.fail();
30− }
31− if (v.tag === "Just") {
32 test1$c = false;
23 test1$c = false;
33− test1$r = [...arr, v._1];
24+ test1$r = [...arr, arr[$0]];
34 continue;
25 continue;
35 }
26 }
36− if (v.tag === "Nothing") {
27+ if (0 < arr.length) {
37 test1$c = false;
28 test1$c = false;
38− test1$r = arr;
29+ test1$r = [...arr, arr[0]];
39 continue;
30 continue;
40 }
31 }
41− $runtime.fail();
32+ test1$c = false;
33+ test1$r = arr;
42 }
34 }
43 return test1$r;
35 return test1$r;
44};
36};
backend-es/test/snapshots-out/Snapshot.Fusion01.js+7−2
@@ -1,12 +1,17 @@
1// @inline export overArray arity=1
1// @inline export overArray arity=1
2import * as $runtime from "../runtime.js";
2import * as $runtime from "../runtime.js";
3import * as Data$dArray from "../Data.Array/index.js";
3import * as Data$dArray from "../Data.Array/index.js";
4−import * as Data$dList from "../Data.List/index.js";
5import * as Data$dList$dTypes from "../Data.List.Types/index.js";
4import * as Data$dList$dTypes from "../Data.List.Types/index.js";
5+import * as Data$dMaybe from "../Data.Maybe/index.js";
6import * as Data$dShow from "../Data.Show/index.js";
6import * as Data$dShow from "../Data.Show/index.js";
7import * as Data$dString$dCodeUnits from "../Data.String.CodeUnits/index.js";
7import * as Data$dString$dCodeUnits from "../Data.String.CodeUnits/index.js";
8+import * as Data$dTuple from "../Data.Tuple/index.js";
8import * as Data$dUnfoldable from "../Data.Unfoldable/index.js";
9import * as Data$dUnfoldable from "../Data.Unfoldable/index.js";
9−const toUnfoldable = /* #__PURE__ */ Data$dList.toUnfoldable(Data$dUnfoldable.unfoldableArray);
10+const toUnfoldable = /* #__PURE__ */ (() => Data$dUnfoldable.unfoldableArray.unfoldr(xs => {
11+ if (xs.tag === "Nil") { return Data$dMaybe.Nothing; }
12+ if (xs.tag === "Cons") { return Data$dMaybe.$Maybe("Just", Data$dTuple.$Tuple(xs._1, xs._2)); }
13+ $runtime.fail();
14+}))();
10const test = x => Data$dArray.reverse(toUnfoldable((() => {
15const test = x => Data$dArray.reverse(toUnfoldable((() => {
11 const loop = loop$a0$copy => loop$a1$copy => {
16 const loop = loop$a0$copy => loop$a1$copy => {
12 let loop$a0 = loop$a0$copy, loop$a1 = loop$a1$copy, loop$c = true, loop$r;
17 let loop$a0 = loop$a0$copy, loop$a1 = loop$a1$copy, loop$c = true, loop$r;
backend-es/test/snapshots-out/Snapshot.Fusion02.js+7−2
@@ -6,12 +6,17 @@
6// @inline export overArray arity=1
6// @inline export overArray arity=1
7import * as $runtime from "../runtime.js";
7import * as $runtime from "../runtime.js";
8import * as Data$dArray from "../Data.Array/index.js";
8import * as Data$dArray from "../Data.Array/index.js";
9−import * as Data$dList from "../Data.List/index.js";
10import * as Data$dList$dTypes from "../Data.List.Types/index.js";
9import * as Data$dList$dTypes from "../Data.List.Types/index.js";
10+import * as Data$dMaybe from "../Data.Maybe/index.js";
11import * as Data$dShow from "../Data.Show/index.js";
11import * as Data$dShow from "../Data.Show/index.js";
12import * as Data$dString$dCodeUnits from "../Data.String.CodeUnits/index.js";
12import * as Data$dString$dCodeUnits from "../Data.String.CodeUnits/index.js";
13+import * as Data$dTuple from "../Data.Tuple/index.js";
13import * as Data$dUnfoldable from "../Data.Unfoldable/index.js";
14import * as Data$dUnfoldable from "../Data.Unfoldable/index.js";
14−const toUnfoldable = /* #__PURE__ */ Data$dList.toUnfoldable(Data$dUnfoldable.unfoldableArray);
15+const toUnfoldable = /* #__PURE__ */ (() => Data$dUnfoldable.unfoldableArray.unfoldr(xs => {
16+ if (xs.tag === "Nil") { return Data$dMaybe.Nothing; }
17+ if (xs.tag === "Cons") { return Data$dMaybe.$Maybe("Just", Data$dTuple.$Tuple(xs._1, xs._2)); }
18+ $runtime.fail();
19+}))();
15const test = x => {
20const test = x => {
16 const loop = loop$a0$copy => loop$a1$copy => {
21 const loop = loop$a0$copy => loop$a1$copy => {
17 let loop$a0 = loop$a0$copy, loop$a1 = loop$a1$copy, loop$c = true, loop$r;
22 let loop$a0 = loop$a0$copy, loop$a1 = loop$a1$copy, loop$c = true, loop$r;
src/PureScript/Backend/Optimizer/Semantics.purs−2
@@ -1235,8 +1235,6 @@ quote = go
1235 build ctx $ Var qual
1235 build ctx $ Var qual
1236 NeutStop qual ->
1236 NeutStop qual ->
1237 buildStop ctx qual
1237 buildStop ctx qual
1238− NeutData qual _ _ _ [] ->
1239− build ctx $ Var qual
1240 NeutData qual ct ty tag values ->
1238 NeutData qual ct ty tag values ->
1241 build ctx $ CtorSaturated qual ct ty tag (map (quote ctx) <$> values)
1239 build ctx $ CtorSaturated qual ct ty tag (map (quote ctx) <$> values)
1242 NeutCtorDef _ ct ty tag fields ->
1240 NeutCtorDef _ ct ty tag fields ->