[ddc] Hoist all legacy and nullable types

As a possible performance improvement types are hoisted to top
level variables where they can be used from anywhere in
the program.

Change-Id: I93cee0b6e582e9218dae5b15b2b8816e7c64153b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/142905
Commit-Queue: Nicholas Shahan <nshahan@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
Reviewed-by: Mark Zhou <markzipan@google.com>
This commit is contained in:
Nicholas Shahan
2020-04-10 17:50:45 +00:00
committed by commit-bot@chromium.org
parent 8a6d0b57ea
commit 1ce773c67f
+32 -7
View File
@@ -2646,7 +2646,15 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
throw UnsupportedError('Undetermined Nullability');
}
return _emitNullabilityWrapper(typeRep, type.nullability);
// Emit non-nullable version directly.
typeRep = _emitNullabilityWrapper(typeRep, type.nullability);
if (!_cacheTypes || type.nullability == Nullability.nonNullable) {
return typeRep;
}
// Hoist the nullable or legacy versions of the type to the top level and
// use it everywhere it appears.
return _typeTable.nameType(type, typeRep);
}
/// Wraps [typeRep] in the appropriate wrapper for the given [nullability].
@@ -2777,13 +2785,22 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
helperCall = 'fnType(#)';
}
var typeRep = runtimeCall(helperCall, [typeParts]);
// Avoid caching the nullability of the function type itself so it can be
// shared by nullable, non-nullable, and legacy versions at the use site.
// First add the type to the type table in its non-nullable form. It can be
// reused by the nullable and legacy versions.
typeRep = _cacheTypes
? _typeTable.nameFunctionType(type, typeRep, lazy: lazy)
? _typeTable.nameFunctionType(
type.withNullability(Nullability.nonNullable), typeRep,
lazy: lazy)
: typeRep;
return _emitNullabilityWrapper(typeRep, type.nullability);
if (type.nullability == Nullability.nonNullable) return typeRep;
// Hoist the nullable or legacy versions of the type to the top level and
// use it everywhere it appears.
typeRep = _emitNullabilityWrapper(typeRep, type.nullability);
return _cacheTypes
? _typeTable.nameFunctionType(type, typeRep, lazy: lazy)
: typeRep;
}
/// Emits an expression that lets you access statics on a [type] from code.
@@ -2825,9 +2842,17 @@ class ProgramCompiler extends ComputeOnceConstantVisitor<js_ast.Expression>
js_ast.Expression _emitTypeParameterType(TypeParameterType type,
{bool emitNullability = true}) {
var typeParam = _emitTypeParameter(type.parameter);
if (!emitNullability) return typeParam;
if (!emitNullability ||
!_cacheTypes ||
// Emit non-nullable version directly.
type.isPotentiallyNonNullable) {
return typeParam;
}
return _emitNullabilityWrapper(typeParam, type.nullability);
// Hoist the wrapped version to the top level and use it everywhere this
// type appears.
return _typeTable.nameType(
type, _emitNullabilityWrapper(typeParam, type.nullability));
}
js_ast.Identifier _emitTypeParameter(TypeParameter t) =>