mirror of
https://github.com/dart-lang/sdk
synced 2024-09-05 16:41:07 +00:00
[ddc] Fix list literal control flow optimization
Restore the behavior originally added in https://dart-review.googlesource.com/c/sdk/+/172082 to call `.push()` on Javascript Arrays directly when it is safe. After some experimenting this optimization no longer appears to be as effective for lists of native types (the type check is little overhead) but is still observable for lists of user defined interface types. Change-Id: I034666a512a7886e5022498e2d6bd3f8aaceebbd Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/215483 Reviewed-by: Stephen Adams <sra@google.com> Commit-Queue: Nicholas Shahan <nshahan@google.com>
This commit is contained in:
parent
a25f41f605
commit
1345eb8baa
|
@ -4803,21 +4803,33 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
|
||||||
/// list and the element type is known to be invariant so it can skip the
|
/// list and the element type is known to be invariant so it can skip the
|
||||||
/// type check.
|
/// type check.
|
||||||
bool isNativeListInvariantAdd(InvocationExpression node) {
|
bool isNativeListInvariantAdd(InvocationExpression node) {
|
||||||
Expression receiver;
|
if (node is InstanceInvocation &&
|
||||||
if (receiver != null && node.name.text == 'add') {
|
node.isInvariant &&
|
||||||
|
node.name.text == 'add') {
|
||||||
// The call to add is marked as invariant, so the type check on the
|
// The call to add is marked as invariant, so the type check on the
|
||||||
// parameter to add is not needed.
|
// parameter to add is not needed.
|
||||||
|
var receiver = node.receiver;
|
||||||
if (receiver is VariableGet &&
|
if (receiver is VariableGet &&
|
||||||
receiver.variable.isFinal &&
|
receiver.variable.isFinal &&
|
||||||
!receiver.variable.isLate) {
|
!receiver.variable.isLate) {
|
||||||
// The receiver is a final variable, so it only contains the
|
// The receiver is a final variable, so it only contains the
|
||||||
// initializer value. Also, avoid late variables in case the CFE
|
// initializer value. Also, avoid late variables in case the CFE
|
||||||
// lowering of late variables is changed in the future.
|
// lowering of late variables is changed in the future.
|
||||||
if (receiver.variable.initializer is ListLiteral) {
|
var initializer = receiver.variable.initializer;
|
||||||
|
if (initializer is ListLiteral) {
|
||||||
// The initializer is a list literal, so we know the list can be
|
// The initializer is a list literal, so we know the list can be
|
||||||
// grown, modified, and is represented by a JavaScript Array.
|
// grown, modified, and is represented by a JavaScript Array.
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
if (initializer is StaticInvocation &&
|
||||||
|
initializer.target.enclosingClass == _coreTypes.listClass &&
|
||||||
|
initializer.target.name.text == 'of' &&
|
||||||
|
initializer.arguments.named.isEmpty) {
|
||||||
|
// The initializer is a `List.of()` call from the dart:core library
|
||||||
|
// and the growable named argument has not been passed (it defaults
|
||||||
|
// to true).
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
Loading…
Reference in a new issue