git.iko.soy/rowtype-yoga/purescript-backend-optimizer
on main help sign in
Generate empty CtorSaturated, optimize it in codegen instead (#98)
commit54b82ac23143dd47bfafaf52dec81dd7ccf2b490 authorVerity Scheel <monoidmusician@gmail.com> dateWed 20 Sep 2023, 17:13 (3 years ago) parentcd03c12
5 files changed+27−25
backend-es/src/PureScript/Backend/Optimizer/Codegen/EcmaScript/Convert.purs+2 backend-es/test/snapshots-out/Snapshot.CaseLeafTco.js+11−19 backend-es/test/snapshots-out/Snapshot.Fusion01.js+7−2 backend-es/test/snapshots-out/Snapshot.Fusion02.js+7−2 src/PureScript/Backend/Optimizer/Semantics.purs−2
backend-es/src/PureScript/Backend/Optimizer/Codegen/EcmaScript/Convert.purs+2
@@ -293,6 +293,8 @@ codegenExpr env@(CodegenEnv { currentModule, inlineApp }) tcoExpr@(TcoExpr _ exp
293293 [ build $ EsReturn $ Just $ codegenCtor env currentModule ct ty tag $
294294 (build <<< EsIdent <<< Qualified Nothing <<< toEsIdent <<< Ident) <$> fields
295295 ]
296+ CtorSaturated qual _ _ _ [] ->
297+ codegenExpr env (TcoExpr mempty (Var qual))
296298 CtorSaturated (Qualified qual _) ct ty tag fields ->
297299 codegenCtor env (fromMaybe currentModule qual) ct ty tag (codegenExpr env <<< snd <$> fields)
298300 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";
31const test1 = test1$a0$copy => test1$a1$copy => {
42 let test1$a0 = test1$a0$copy, test1$a1 = test1$a1$copy, test1$c = true, test1$r;
53 while (test1$c) {
64 const b = test1$a0, arr = test1$a1;
7− const v = Data$dArray.last(arr);
8− if (0 < arr.length) {
9− if (v.tag === "Just") {
10− if (v._1 === 2 && arr[0] === 1) {
5+ const $0 = arr.length - 1 | 0;
6+ if ($0 >= 0 && $0 < arr.length) {
7+ if (0 < arr.length) {
8+ if (arr[$0] === 2 && arr[0] === 1) {
119 test1$c = false;
1210 test1$r = arr;
1311 continue;
1412 }
13+ const $1 = arr[$0];
1514 if (b) {
1615 test1$c = false;
1716 test1$r = [];
1817 continue;
1918 }
2019 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];
22− continue;
23− }
24− if (v.tag === "Nothing") {
25− test1$c = false;
26− test1$r = [...arr, arr[0]];
20+ test1$a1 = [$1, arr[0], 3, $1, 5, 6, 7, 8, 9, 10, arr[0], 12, 13, 14, 15, 16, 17, ...arr];
2721 continue;
2822 }
29− $runtime.fail();
30− }
31− if (v.tag === "Just") {
3223 test1$c = false;
33− test1$r = [...arr, v._1];
24+ test1$r = [...arr, arr[$0]];
3425 continue;
3526 }
36− if (v.tag === "Nothing") {
27+ if (0 < arr.length) {
3728 test1$c = false;
38− test1$r = arr;
29+ test1$r = [...arr, arr[0]];
3930 continue;
4031 }
41− $runtime.fail();
32+ test1$c = false;
33+ test1$r = arr;
4234 }
4335 return test1$r;
4436};
backend-es/test/snapshots-out/Snapshot.Fusion01.js+7−2
@@ -1,12 +1,17 @@
11// @inline export overArray arity=1
22import * as $runtime from "../runtime.js";
33import * as Data$dArray from "../Data.Array/index.js";
4−import * as Data$dList from "../Data.List/index.js";
54import * as Data$dList$dTypes from "../Data.List.Types/index.js";
5+import * as Data$dMaybe from "../Data.Maybe/index.js";
66import * as Data$dShow from "../Data.Show/index.js";
77import * as Data$dString$dCodeUnits from "../Data.String.CodeUnits/index.js";
8+import * as Data$dTuple from "../Data.Tuple/index.js";
89import * 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+}))();
1015const test = x => Data$dArray.reverse(toUnfoldable((() => {
1116 const loop = loop$a0$copy => loop$a1$copy => {
1217 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 @@
66// @inline export overArray arity=1
77import * as $runtime from "../runtime.js";
88import * as Data$dArray from "../Data.Array/index.js";
9−import * as Data$dList from "../Data.List/index.js";
109import * as Data$dList$dTypes from "../Data.List.Types/index.js";
10+import * as Data$dMaybe from "../Data.Maybe/index.js";
1111import * as Data$dShow from "../Data.Show/index.js";
1212import * as Data$dString$dCodeUnits from "../Data.String.CodeUnits/index.js";
13+import * as Data$dTuple from "../Data.Tuple/index.js";
1314import * 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+}))();
1520const test = x => {
1621 const loop = loop$a0$copy => loop$a1$copy => {
1722 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
12351235 build ctx $ Var qual
12361236 NeutStop qual ->
12371237 buildStop ctx qual
1238− NeutData qual _ _ _ [] ->
1239− build ctx $ Var qual
12401238 NeutData qual ct ty tag values ->
12411239 build ctx $ CtorSaturated qual ct ty tag (map (quote ctx) <$> values)
12421240 NeutCtorDef _ ct ty tag fields ->