New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dart2js: readability and code size: || and && code generates unnecessary temporaries #7475
Comments
Changed the title to: "dart2js: readability and code size: || and && code generates unnecessary temporaries". |
Added this to the M3 milestone. |
Added TriageForM5 label. |
Removed this from the M5 milestone. |
Removed this from the Later milestone. |
Removed Oldschool-Milestone-Later label. |
This code is amazingly similar today to what @rakudrama reported in 2012, when compiling with import 'dart:html';
void main() async {
var request = await HttpRequest.request('f.json');
request.onReadyStateChange.listen((e) {
if (request.readyState == HttpRequest.DONE &&
(request.status == 200 || request.status == 0)) {
onSuccess(request);
}
});
}
void onSuccess(HttpRequest req) => print(req); compiled with -O1: F.main_closure.prototype = {
call$1: function(e) {
var t2,
t1 = this.request;
if (t1.readyState === 4) {
t2 = t1.status;
t2 = t2 === 200 || t2 === 0;
} else
t2 = false;
if (t2)
H.printString(H.S(t1));
},
$signature: 17
}; Compiled with -O4 is about the same, minified: F.bf.prototype={
$1:function(a){var u,t=this.a
if(t.readyState===4){u=t.status
u=u===200||u===0}else u=!1
if(u)H.cS(H.b(t))}}; |
This closure:
request.on.readyStateChange.add((e) {
if (request.readyState == HttpRequest.DONE &&
(request.status == 200 || request.status == 0)) {
onSuccess(request);
}
});
compiles to:
$$._HttpRequestUtils_get_anon = {"":"Closure;request_0,onSuccess_1",
call$1: function(e) {
var t1, t2;
t1 = this.request_0;
if ($.eqB(t1.get$readyState(), 4))
t2 = $.eqB(t1.get$status(), 200) || $.eqB(t1.get$status(), 0);
else
t2 = false;
if (t2)
this.onSuccess_1.call$1(t1);
}
};
t2 is unnecessary and, if removed, the code is smaller and closer to the original Dart code:
$$._HttpRequestUtils_get_anon = {"":"Closure;request_0,onSuccess_1",
call$1: function(e) {
var t1;
t1 = this.request_0;
if ($.eqB(t1.get$readyState(), 4))
if ($.eqB(t1.get$status(), 200) || $.eqB(t1.get$status(), 0))
this.onSuccess_1.call$1(t1);
}
};
The problem with the generated code is that one branch of the if-the-else ensures that the if-then is not taken.
Instead, the body of the if-then should be generated in the other branch of the if-then-else.
The text was updated successfully, but these errors were encountered: