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

Poor code quality of window.console.dir #17508

Closed
peter-ahe-google opened this issue Mar 15, 2014 · 8 comments
Closed

Poor code quality of window.console.dir #17508

peter-ahe-google opened this issue Mar 15, 2014 · 8 comments
Assignees
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. dart2js-optimization library-html type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dart2js

Comments

@peter-ahe-google
Copy link
Contributor

onSelect(Event e) {
  window.console.dir(e);
}

Compiles to:

  function(e) {
    window;
    $.get$Console__safeConsole().toString;
    if (typeof console != "undefined")
      console.debug(e);
  }

This is code is pretty awful compared to what I would have written myself:

  function(e) {
    !!self.console && self.console.dir(e)
  }

I'm also puzzled why console.debug is used. But perhaps console.debug and console.dir are the same. If so, dir is shorter.

@rakudrama
Copy link
Member

There are multiple issues here.

  1. I believe 'debug' vs 'dir' is a copy&paste bug, otherwise I would expect a comment in the source that e.g. such-and-such browser is missing 'debug' -- Emily?
  2. If we can make the Console._safeConsole singleton a constant that would eliminate the lazy initialization getter and .toString null guard. We should definitely try this.

It is surprisingly difficult to optimize a lazy initializer. In the general case a lazy initializer can fail (throw) forcing a null value. At the time we do global type analysis we don't have sufficient information to tell the initializer can never fail, so we can't remove null from the type, forcing the null guard.

  1. I don't think we can get rid of the 'window'. It is a reference error in d8/jsshell. It would be wrong to crash on window.toString() but not window.console.
  2. On generating if(a)b vs a&&b, we have generally tried to generate what the programmer wrote. People do have to debug compiled code. We could do some local transformations at the JavaScript level for minified code only.

All this code comes from inlining on this basic body:

   C.Window_methods.get$console(get$window()).dir$1(e);

I think inlining Console.dir is a mistake. We replaced a call with a null guard and the body of the callee. The body of the callee could not be optimized in the new context. There is no performance benefit since the JavaScript engine likely inlines the monomorphic call anyway. Something like the following is probably the best compromise given the current source:

   window;
   $.get$Console__safeConsole().dir$1(e);

If we could pre-initialize Console._safeConsole:

   window;
   $.Console__safeConsole.dir$1(e);


Window get window => JS('Window', 'window');

class Window ...
  Console get console => Console._safeConsole;

class Console {
  Console._safe() {}
  static Console _safeConsole = new Console._safe();


Added Library-Html label.

@floitschG
Copy link
Contributor

cc @kmillikin.
Added Optimization label.

@efortuna
Copy link
Contributor

re: Stephen's first question -- I haven't ever worked on the internals of the (window.)console code so I don't know that we've added any error messages indicating "debug" is not available on specific browsers. Is this something we should add?

@rakudrama
Copy link
Member

https://chromiumcodereview.appspot.com/202983002/ addresses points 1. and 2. from comment #­1

              window;
              t2 = $.get$Console__safeConsole();
              t3 = "Removing disallowed type extension <" + H.S(node.tagName) + " is="" + isAttr + "">";
              t2.toString;
              if (typeof console != "undefined")
                console.warn(t3);

--->
              window;
              t2 = "Removing disallowed type extension <" + H.S(node.tagName) + " is="" + isAttr + "">";
              if (typeof console != "undefined")
                console.warn(t2);


Set owner to @rakudrama.
Added Started label.

@rakudrama
Copy link
Member

Are the improvements good enough to close this issue?


Added NeedsInfo label.

@rakudrama
Copy link
Member

Set owner to @peter-ahe-google.

@peter-ahe-google
Copy link
Contributor Author

The new version isn't callable from a Worker.

This one works like a charm from a Worker, and works in IE9: function dir(o) { !!self.console && self.console.dir(o) }

(if you know where to find the worker's console).

I believe we could get there relatively easily:

  1. Add a non-private constructor to Console.
  2. Change Console.dir from:

  void dir(Object arg) => _isConsoleDefined ?
      JS('void', 'console.dir(#)', arg) : null;

  to:

  void dir(Object arg) => JS('', '# && self.console.dir(#)', _isConsoleDefined, arg);

  1. Change Console._isConsoleDefined from:

  bool get _isConsoleDefined => JS('bool', 'typeof console != "undefined"');

  to:

  bool get _isConsoleDefined => JS('bool', '!!self.console');


Set owner to @rakudrama.
Added Started label.

@kevmoo kevmoo added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) and removed priority-unassigned labels Feb 29, 2016
@vsmenon vsmenon added the area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. label Jul 20, 2019
@joshualitt
Copy link
Contributor

Part of this issue has been fixed, but at this point it seems unlikely we'll have a chance to revisit. We can reopen this issue if it causes problems down the road.

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 library-html type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dart2js
Projects
None yet
Development

No branches or pull requests

7 participants