[kernel] Short-circut 'computeNonNull' when already nonNullable

When compiling "compile.dart" the type given to computeNonNull
is already non-nullable in ~42% of the cases, meaning it spends
time not doing anything.

Change-Id: I94598efceb3b46f0ac81ec36814c216d01a9e38b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/298823
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Jens Johansen <jensj@google.com>
diff --git a/pkg/front_end/test/spell_checking_list_code.txt b/pkg/front_end/test/spell_checking_list_code.txt
index 77689cf..98a588b 100644
--- a/pkg/front_end/test/spell_checking_list_code.txt
+++ b/pkg/front_end/test/spell_checking_list_code.txt
@@ -211,6 +211,7 @@
 chloestefantsova
 chunks
 ci
+circuit
 circuited
 ck
 cl
@@ -1485,6 +1486,7 @@
 subscription
 subsection
 subsections
+subsequently
 subspace
 subspaces
 subst
diff --git a/pkg/kernel/lib/src/non_null.dart b/pkg/kernel/lib/src/non_null.dart
index f03c436..d59e40e 100644
--- a/pkg/kernel/lib/src/non_null.dart
+++ b/pkg/kernel/lib/src/non_null.dart
@@ -7,6 +7,16 @@
 
 /// Returns the type defined as `NonNull(type)` in the nnbd specification.
 DartType computeNonNull(DartType type) {
+  if (type.nullability == Nullability.nonNullable) {
+    // The visitor below always returns null when the input type is already
+    // nullable, subsequently returning the input type.
+    // When compiling "compile.dart" this is exactly what happens ~42% of the
+    // time. Here we short-circuit the visit.
+    // Note that some use [declaredNullability] instead of [nullability], but
+    // that [nullability] is only nonNullable if [declaredNullability] is too.
+    assert(type.accept(const _NonNullVisitor()) == null);
+    return type;
+  }
   return type.accept(const _NonNullVisitor()) ?? type;
 }