mirror of
https://github.com/dart-lang/sdk
synced 2024-09-16 00:39:49 +00:00
[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:
parent
c1cf29b2d4
commit
4799f38767
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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')
|
||||
|
|
Loading…
Reference in a new issue