Skip to content
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

Open
rakudrama opened this issue Dec 18, 2012 · 9 comments
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. dart2js-optimization P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug web-dart2js

Comments

@rakudrama
Copy link
Member

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.

@rakudrama
Copy link
Member Author

Changed the title to: "dart2js: readability and code size: || and && code generates unnecessary temporaries".

@kasperl
Copy link

kasperl commented Jan 7, 2013

Added this to the M3 milestone.

@anders-sandholm
Copy link
Contributor

Removed this from the M3 milestone.
Added this to the M4 milestone.

@kasperl
Copy link

kasperl commented Apr 22, 2013

Removed this from the M4 milestone.
Added this to the M5 milestone.

@kasperl
Copy link

kasperl commented May 23, 2013

Added TriageForM5 label.

@kasperl
Copy link

kasperl commented May 28, 2013

Removed this from the M5 milestone.
Added this to the Later milestone.
Removed TriageForM5 label.

@kasperl
Copy link

kasperl commented Jul 10, 2014

Removed this from the Later milestone.
Added Oldschool-Milestone-Later label.

@kasperl
Copy link

kasperl commented Aug 4, 2014

Removed Oldschool-Milestone-Later label.

@kevmoo kevmoo added P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug and removed triaged labels Feb 29, 2016
@vsmenon vsmenon added area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. and removed area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. labels Jul 20, 2019
@srawlins
Copy link
Member

srawlins commented Aug 9, 2019

This code is amazingly similar today to what @rakudrama reported in 2012, when compiling with -O1:

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))}};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. dart2js-optimization P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug web-dart2js
Projects
None yet
Development

No branches or pull requests

6 participants