Skip to content

Commit eb96936

Browse files
committed
Option optimization: do not create redundant local vars
1 parent 645e1a9 commit eb96936

File tree

7 files changed

+48
-53
lines changed

7 files changed

+48
-53
lines changed

compiler/core/lam_util.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ let refine_let
9393
since function evaluation is always delayed
9494
*)
9595
Lam.let_ Alias param arg l
96+
| (Strict | StrictOpt),
97+
( Lprim {primitive = Psome_not_nest; _} as prim ), _ ->
98+
Lam.let_ Alias param prim l
9699
| ( (Strict | StrictOpt ) ), (Lfunction _ ), _ ->
97100
(*It can be promoted to [Alias], however,
98101
we don't want to do this, since we don't want the

packages/@rescript/runtime/lib/es6/Belt_internalAVLset.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -741,15 +741,13 @@ function rotateWithRightChild(k1) {
741741

742742
function doubleWithLeftChild(k3) {
743743
let k3l = k3.l;
744-
let v = rotateWithRightChild(k3l);
745-
k3.l = v;
744+
k3.l = rotateWithRightChild(k3l);
746745
return rotateWithLeftChild(k3);
747746
}
748747

749748
function doubleWithRightChild(k2) {
750749
let k2r = k2.r;
751-
let v = rotateWithLeftChild(k2r);
752-
k2.r = v;
750+
k2.r = rotateWithLeftChild(k2r);
753751
return rotateWithRightChild(k2);
754752
}
755753

packages/@rescript/runtime/lib/js/Belt_internalAVLset.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -741,15 +741,13 @@ function rotateWithRightChild(k1) {
741741

742742
function doubleWithLeftChild(k3) {
743743
let k3l = k3.l;
744-
let v = rotateWithRightChild(k3l);
745-
k3.l = v;
744+
k3.l = rotateWithRightChild(k3l);
746745
return rotateWithLeftChild(k3);
747746
}
748747

749748
function doubleWithRightChild(k2) {
750749
let k2r = k2.r;
751-
let v = rotateWithLeftChild(k2r);
752-
k2.r = v;
750+
k2.r = rotateWithLeftChild(k2r);
753751
return rotateWithRightChild(k2);
754752
}
755753

tests/gentype_tests/typescript-react-example/src/nested/Types.res.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/tests/src/option_optimisation.mjs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
44

55
function boolean(val1, val2) {
6-
let a = val1;
7-
let b = val2;
8-
if (b || a) {
6+
if (val2 || val1) {
97
return "a";
108
} else {
119
return "b";
@@ -33,8 +31,7 @@ function constant() {
3331
}
3432

3533
function param(opt) {
36-
let x = opt;
37-
console.log(x);
34+
console.log(opt);
3835
}
3936

4037
export {

tests/tests/src/option_wrapping_test.mjs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@ let x6 = {
88
x: 42
99
};
1010

11+
let x10 = null;
12+
13+
let x11 = Primitive_option.some(undefined);
14+
15+
let x1 = "hello";
16+
17+
let x2 = 1;
18+
19+
let x3 = {
20+
TAG: "Ok",
21+
_0: "hi"
22+
};
23+
24+
let x4 = "polyvar";
25+
26+
let x5 = {
27+
x: 42
28+
};
29+
1130
let x7 = [
1231
1,
1332
2,
@@ -16,9 +35,7 @@ let x7 = [
1635

1736
let x8 = () => {};
1837

19-
let x10 = null;
20-
21-
let x11 = Primitive_option.some(undefined);
38+
let x12 = "test";
2239

2340
let x20 = null;
2441

@@ -68,36 +85,19 @@ let x42 = new Intl.PluralRules();
6885

6986
let x43 = new Intl.Locale("en");
7087

71-
let x45 = Promise.resolve(true);
72-
73-
let x47 = {};
74-
75-
let x48 = Stdlib_Lazy.make(() => true);
76-
77-
let x1 = "hello";
78-
79-
let x2 = 1;
80-
81-
let x3 = {
82-
TAG: "Ok",
83-
_0: "hi"
84-
};
85-
86-
let x4 = "polyvar";
87-
88-
let x5 = {
89-
x: 42
90-
};
91-
92-
let x12 = "test";
93-
9488
let x44 = [
9589
1,
9690
2
9791
];
9892

93+
let x45 = Promise.resolve(true);
94+
9995
let x46 = /* [] */0;
10096

97+
let x47 = {};
98+
99+
let x48 = Stdlib_Lazy.make(() => true);
100+
101101
export {
102102
x1,
103103
x2,

tests/tests/src/reasonReact.mjs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,6 @@ function wrapReasonForJs(component, jsPropsToReason) {
9292
let dummyInteropComponent = basicComponent("interop");
9393

9494
function wrapJsForReason(reactClass, props, children) {
95-
let jsElementWrapped = (extra, extra$1) => {
96-
let props$1 = Object.assign(Object.assign({}, props), {
97-
ref: extra$1,
98-
key: extra
99-
});
100-
let varargs = Js_array.concat(children, [
101-
reactClass,
102-
props$1
103-
]);
104-
return React.createElement.apply(null, varargs);
105-
};
10695
return {
10796
debugName: dummyInteropComponent.debugName,
10897
reactClassInternal: dummyInteropComponent.reactClassInternal,
@@ -117,7 +106,17 @@ function wrapJsForReason(reactClass, props, children) {
117106
initialState: dummyInteropComponent.initialState,
118107
retainedProps: dummyInteropComponent.retainedProps,
119108
reducer: dummyInteropComponent.reducer,
120-
jsElementWrapped: jsElementWrapped
109+
jsElementWrapped: (extra, extra$1) => {
110+
let props$1 = Object.assign(Object.assign({}, props), {
111+
ref: extra$1,
112+
key: extra
113+
});
114+
let varargs = Js_array.concat(children, [
115+
reactClass,
116+
props$1
117+
]);
118+
return React.createElement.apply(null, varargs);
119+
}
121120
};
122121
}
123122

0 commit comments

Comments
 (0)