Dart 2.2 Announced: More Efficient Machine Code, Support for Set Literals

Source [Announcing Dart 2.2: Faster native code, support for set literals]

Today (February 26, 2019) we announce the release of the Dart 2.2 SDK, an update for Dart 2 that offers improved ahead-of-time (AOT) code performance and Set support literals.

image

Improving Dart Performance for Flutter Development


We continue our work to make AOT-compiled code , such as Flutter applications, even faster. In Dart 2.1, we reduced the cost of type checking by significantly reducing the cost of type checking for both code compiled by AOT and code that runs on a virtual machine with JIT (just-in-time) compilation.

In Dart 2.2, we focused on the performance of AOT-compiled code, improving performance by 11-16% on microbenchmarks (by increasing the size of the code by ~ 1%). This improvement is the result of work over several quarters, in order to reduce the costs of static calls. Our optimized AOT code can now call the requested object directly using a PC call (i.e. usingProgram counter ). Prior to this, we had to conduct a search several times in the pool of objects to determine the required address. These optimizations are especially useful when the code contains many constructors and static method calls, such as Flutter user interface code, which contains many widgets.

Support for Set Literals in Dart 2.2


The main Dart library (dart: core) contains several collection classes: Maps , Lists , and Sets . Maps are sets of key-value pairs. Lists are ordered sequences of values, each of which can be accessed using the index and which can occur several times. Sets are unordered collections of values ​​in which each value can only occur once and where it can be effectively checked to see if there is a value in the collection.

Dart collections are usually initialized with compile-time constants, and Dart offers convenient syntax for writing this initialization. In the Dart List, you can initialize as follows:

const List<String> releases = ['Dart 2.0', 'Dart 2.1', 'Dart 2.2'];

Previously, Dart only supported literal syntax for List and Map, so Set initialization was cumbersome, as we had to initialize through the list:

Set<String> currencies = Set.of(['EUR', 'USD', 'JPY']);

This code is not just inconvenient and inefficient; the lack of literal support prevents the creation of an immutable Set at compile time. With the addition of Set literal support in Dart 2.2, initialize the set and make it immutable using a convenient new syntax:

const Set<String> currencies = {'EUR', 'USD', 'JPY'};

Real examples of using Set literals with the Flutter command can be found here . For a deeper insight into the issue, we suggest that you familiarize yourself with the updated Dart 2.2 language tour .

Innovate with the Dart 2 Common Front-End (CFE)


Dart offers several implementations of its own: the Dart VM used by Flutter, the dart2js compiler, and the Dart dev compiler (dartdevc), all of which use the Common Front-End compiler. Dart Common Front End, or CFE, parses Dart code, performs type inference, and translates Dart to a lower-level intermediate language that compilers take back end as input.

Set literals are an example of a language construct that we were able to quickly implement thanks to CFE. The code for parsing Set literals and performing type inference was implemented once using CFE for all implementations. In addition, we created a transitional implementation that could be used by back end compilers at the initial stage. In an intermediate implementation, a mutable version of the specified Set of literals was translated during compilation into the equivalent form:

Set<String> currencies = Set<String>()..add('EUR')..add('USD')..add('JPY');

An intermediate implementation of constant Set literals differs in that they cannot be assembled gradually in parts. Instead, we implemented this in terms of a private immutable Set class that wraps an immutable Map, where Set elements are Map keys:

const Set<String> currencies =
   _UnmodifiableSet<String>({'EUR': null, 'USD': null, 'JPY': null});

The immutable Set class implements methods in the Set interface, delegating them to the internal Map.

In general, we were able to implement Set literals initially as a function exclusively of CFE. The back end could immediately use the CFE implementation, and implement its own support later. This allowed the back end compilers to postpone their support until the performance aspects of this function were better understood.

Dart 2.2 Specification


Dart 2 was such a significant update to Dart that it took us a while to update the official language specification to meet all the changes we made. We finally completed this work, and the specification was updated to version Dart 2.2. We have also moved the language specification to the new repository and added continuous integration to ensure that the specification is constantly updated in PDF format as future versions of the Dart language evolve. Both 2.2 and future Dart 2.x specifications are available on the Dart specification page .

How to install Dart 2.2


The Dart SDK 2.2 is available on the Dart homepage from today. If you are a Flutter developer, Dart 2.2 is already included. (Note: on the flutter doctor console command, the current Flutter master and dev channels will report Dart 2.2. Today's stable version of Flutter 1.2 will output Dart 2.1.2; this version has the same capabilities as Dart 2.2).

That's all for now. We hope you enjoy Dart 2.2!



Thank you so much for helping me translate PsyHaSTe

Also popular now: