Skip to content

Commit

Permalink
[vm/tfa] Infer type for indexing into a const list
Browse files Browse the repository at this point in the history
Before:
DartNTZ.NTZ32(RunTime): 351.9357010381841 us.
DartNTZ.NTZ64(RunTime): 351.99127771911304 us.

After:
DartNTZ.NTZ32(RunTime): 150.19256867162275 us.
DartNTZ.NTZ64(RunTime): 149.48622578475337 us.

Issue: #37789
Change-Id: Ie8b16b2602a15140df9f0f21199ee9eb2bbf158c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/115012
Reviewed-by: Aart Bik <ajcbik@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
Reviewed-by: Samir Jindel <sjindel@google.com>
Commit-Queue: Alexander Markov <alexmarkov@google.com>
  • Loading branch information
alexmarkov authored and commit-bot@chromium.org committed Aug 30, 2019
1 parent 11a4091 commit 011144b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
27 changes: 26 additions & 1 deletion pkg/vm/lib/transformations/type_flow/summary_collector.dart
Expand Up @@ -945,9 +945,16 @@ class SummaryCollector extends RecursiveVisitor<TypeExpr> {

@override
TypeExpr visitMethodInvocation(MethodInvocation node) {
final receiver = _visit(node.receiver);
final receiverNode = node.receiver;
final receiver = _visit(receiverNode);
final args = _visitArguments(receiver, node.arguments);
final target = node.interfaceTarget;
if (receiverNode is ConstantExpression && node.name.name == '[]') {
Constant constant = receiverNode.constant;
if (constant is ListConstant) {
return _handleIndexingIntoListConstant(constant);
}
}
if (target == null) {
if (node.name.name == '==') {
assertx(args.values.length == 2);
Expand Down Expand Up @@ -993,6 +1000,24 @@ class SummaryCollector extends RecursiveVisitor<TypeExpr> {
}
}

TypeExpr _handleIndexingIntoListConstant(ListConstant list) {
final elementTypes = new Set<Type>();
for (var element in list.entries) {
elementTypes.add(constantAllocationCollector.typeFor(element));
}
switch (elementTypes.length) {
case 0:
return new Type.empty();
case 1:
return elementTypes.single;
default:
final join = new Join(null, list.typeArgument);
join.values.addAll(elementTypes);
_summary.add(join);
return join;
}
}

@override
TypeExpr visitPropertyGet(PropertyGet node) {
var receiver = _visit(node.receiver);
Expand Down
@@ -0,0 +1,17 @@
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

smiLiteral() => 42;

intLiteral() => 0x8000000000000000;

strLiteral() => 'abc';

const _constList1 = [1, 2, 3];
indexingIntoConstantList1(int i) => _constList1[i];

const _constList2 = ['hi', 33, null, -5];
indexingIntoConstantList2(int i) => _constList2[i];

main() {}
@@ -0,0 +1,25 @@
------------ #lib::smiLiteral ------------

RESULT: _T (dart.core::_Smi)
------------ #lib::intLiteral ------------

RESULT: _T (dart.core::int*)+
------------ #lib::strLiteral ------------

RESULT: _T (dart.core::_OneByteString)
------------ #lib::indexingIntoConstantList1 ------------
%i = _Parameter #0 [_T (dart.core::int*)+?]
RESULT: _T (dart.core::_Smi)
------------ #lib::indexingIntoConstantList2 ------------
%i = _Parameter #0 [_T (dart.core::int*)+?]
t1 = _Join [dart.core::Object*] (_T (dart.core::_OneByteString), _T (dart.core::_Smi), _T {}?)
RESULT: t1
------------ #lib::main ------------

RESULT: _T {}?
------------ #lib::_constList1 ------------

RESULT: _T (dart.core::_ImmutableList)
------------ #lib::_constList2 ------------

RESULT: _T (dart.core::_ImmutableList)

0 comments on commit 011144b

Please sign in to comment.