Skip to content

Commit 816fd08

Browse files
alexmarkovcommit-bot@chromium.org
authored andcommitted
[vm/compiler] Infer more accurate generic type of list factory constructors
For recognized list factories, which take constant type arguments, compiler can infer full generic type in addition to cid. This helps remove AssertAssignable when creating List<List<...>> with List.generate constructor. Havlak +3.7-10% in JIT mode with null safety. Change-Id: I0a050c9a0d3604ffcfd1a0afa0f123a07e9ac03a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/159185 Commit-Queue: Alexander Markov <[email protected]> Reviewed-by: Vyacheslav Egorov <[email protected]> Reviewed-by: Régis Crelier <[email protected]>
1 parent fdd01da commit 816fd08

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

runtime/vm/compiler/backend/type_propagator.cc

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1340,10 +1340,36 @@ CompileType PolymorphicInstanceCallInstr::ComputeType() const {
13401340
return is_nullable ? type : type.CopyNonNullable();
13411341
}
13421342

1343+
static CompileType ComputeListFactoryType(CompileType* inferred_type,
1344+
Value* type_args_value) {
1345+
ASSERT(inferred_type != nullptr);
1346+
const intptr_t cid = inferred_type->ToNullableCid();
1347+
ASSERT(cid != kDynamicCid);
1348+
if ((cid == kGrowableObjectArrayCid || cid == kArrayCid ||
1349+
cid == kImmutableArrayCid) &&
1350+
type_args_value->BindsToConstant()) {
1351+
const auto& type_args =
1352+
type_args_value->BoundConstant().IsNull()
1353+
? TypeArguments::null_type_arguments()
1354+
: TypeArguments::Cast(type_args_value->BoundConstant());
1355+
const Class& cls =
1356+
Class::Handle(Isolate::Current()->class_table()->At(cid));
1357+
Type& type = Type::ZoneHandle(Type::New(
1358+
cls, type_args, TokenPosition::kNoSource, Nullability::kNonNullable));
1359+
ASSERT(type.IsInstantiated());
1360+
type.SetIsFinalized();
1361+
return CompileType(CompileType::kNonNullable, cid, &type);
1362+
}
1363+
return *inferred_type;
1364+
}
1365+
13431366
CompileType StaticCallInstr::ComputeType() const {
13441367
// TODO(alexmarkov): calculate type of StaticCallInstr eagerly
13451368
// (in optimized mode) and avoid keeping separate result_type.
1346-
CompileType* inferred_type = result_type();
1369+
CompileType* const inferred_type = result_type();
1370+
if (is_known_list_constructor()) {
1371+
return ComputeListFactoryType(inferred_type, ArgumentValueAt(0));
1372+
}
13471373
if ((inferred_type != NULL) &&
13481374
(inferred_type->ToNullableCid() != kDynamicCid)) {
13491375
return *inferred_type;

0 commit comments

Comments
 (0)