[dartdevc] Optimize cast operation

Avoid checking for legacy, nullable, null and top. These checks should
all return false because if they were true the dispatch would have gone
directly to the as() or _check() methods on LegacyType, NullableType,
Null, DynamicType, and VoidType. Object? is dispatched through
NullableType.

Add is(), as(), and _check() instance methods to VoidType with logic
similar to DynamicType.

Change-Id: Ib28f40605c66fa7e8569ace405eba51035f2079a
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/138668
Reviewed-by: Mark Zhou <markzipan@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
Commit-Queue: Nicholas Shahan <nshahan@google.com>
This commit is contained in:
Nicholas Shahan 2020-03-07 00:29:34 +00:00 committed by commit-bot@chromium.org
parent c1cf29b2d4
commit 4799f38767
2 changed files with 18 additions and 16 deletions

View file

@ -420,24 +420,15 @@ bool instanceOf(obj, type) {
cast(obj, type, @notNull bool isImplicit) {
// We hoist the common case where null is checked against another type here
// for better performance.
if (obj == null) {
if (_isLegacy(type) ||
_isNullType(type) ||
_isTop(type) ||
_isNullable(type)) {
return obj;
}
if (_strictSubtypeChecks) {
return castError(obj, type, isImplicit);
}
if (obj == null && !_strictSubtypeChecks) {
// Check the null comparison cache to avoid emitting repeated warnings.
_nullWarnOnType(type);
return obj;
} else {
var actual = getReifiedType(obj);
if (isSubtypeOf(actual, type)) return obj;
}
var actual = getReifiedType(obj);
if (isSubtypeOf(actual, type)) {
return obj;
}
return castError(obj, type, isImplicit);
}

View file

@ -85,14 +85,15 @@ class DartType implements Type {
class DynamicType extends DartType {
toString() => 'dynamic';
@notNull
@JSExportName('is')
bool is_T(object) => true;
@JSExportName('as')
as_T(object) => object;
Object? as_T(Object? object) => object;
@JSExportName('_check')
check_T(object) => object;
Object? check_T(Object? object) => object;
}
@notNull
@ -412,6 +413,16 @@ final _dynamic = DynamicType();
class VoidType extends DartType {
toString() => 'void';
@notNull
@JSExportName('is')
bool is_T(object) => true;
@JSExportName('as')
Object? as_T(Object? object) => object;
@JSExportName('_check')
Object? check_T(Object? object) => object;
}
@JSExportName('void')