Skip to content

dart2js: more compact encoding of fields that are always initialized to null #21450

Closed
@rakudrama

Description

@rakudrama

Many fields are always initialized to null.
We can tell this by looking at the field declarations (are they initialized, and to what) and the generative constructor initializer lists, e.g:

class _StreamImplEvents extends _PendingEvents {
  _DelayedEvent firstPendingEvent = null;
  _DelayedEvent lastPendingEvent = null;
  ...
}

This leads to allocating constructor calls like the following

        t1 = new P._StreamImplEvents(null, null, 0); // all the field initial values.

The synthesized allocating JavaScript constructor looks like this:

  P._StreamImplEvents =
  function _StreamImplEvents(param_firstPendingEvent, param_lastPendingEvent, param_state) {
    this.firstPendingEvent = param_firstPendingEvent;
    this.lastPendingEvent = param_lastPendingEvent;
    this.state = param_state;
  }

It is generated from the field info

  _StreamImplEvents: {
    "^": "_PendingEvents;firstPendingEvent,lastPendingEvent,_state",

The field info could encode the always-null fields, e.g. preceding the field with "+"

  _StreamImplEvents: {
    "^": "_PendingEvents;+firstPendingEvent,+lastPendingEvent,_state",

Now the synthesized constructor has null values in-line:

  P._StreamImplEvents =
  function _StreamImplEvents(param_state) {
    this.firstPendingEvent = null;
    this.lastPendingEvent = null;
    this.state = param_state;
  }

and the calls to the constructor function that omit the always-null parameters:

       t1 = new P._StreamImplEvents(0);

This is a class-local optimization since it is a function of the field declaration initializers and generative constructor initializer lists.

A global version of the optimization would augment these fields with fields with initializers inferred to be null.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-web-jsIssues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop.dart2js-optimizationtype-enhancementA request for a change that isn't a bugweb-dart2js

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions