[dart2js] Slightly smaller constant lists

Move the setArrayType call into the makeConstantList helper.

Shaves ~8k of small Flutter apps.

Change-Id: I8ff17e93ad3ad9e09292e294e2c2ddcbe66c5d33
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/428560
Commit-Queue: Stephen Adams <sra@google.com>
Reviewed-by: Nate Biggs <natebiggs@google.com>
diff --git a/pkg/compiler/lib/src/js_backend/constant_emitter.dart b/pkg/compiler/lib/src/js_backend/constant_emitter.dart
index 6a91a64..0e87aae 100644
--- a/pkg/compiler/lib/src/js_backend/constant_emitter.dart
+++ b/pkg/compiler/lib/src/js_backend/constant_emitter.dart
@@ -27,7 +27,10 @@
     js_ast.Expression Function(ConstantValue constant);
 
 typedef _ConstantListGenerator =
-    js_ast.Expression Function(js_ast.Expression array);
+    js_ast.Expression Function(
+      js_ast.Expression array,
+      js_ast.Expression reifiedType,
+    );
 
 /// Visitor that creates [js_ast.Expression]s for constants that are inlined
 /// and therefore can be created during modular code generation.
@@ -232,13 +235,6 @@
 /// (if there are some). It is hence up to that function to decide which
 /// constants should be inlined or not.
 class ConstantEmitter extends ModularConstantEmitter {
-  // Matches blank lines, comment lines and trailing comments that can't be part
-  // of a string.
-  static final RegExp commentRE = RegExp(
-    r'''^ *(//.*)?\n|  *//[^''"\n]*$''',
-    multiLine: true,
-  );
-
   final JCommonElements _commonElements;
   final JElementEnvironment _elementEnvironment;
   final RuntimeTypesNeed _rtiNeed;
@@ -271,9 +267,13 @@
     List<js_ast.Expression> elements = constant.entries
         .map(_constantReferenceGenerator)
         .toList(growable: false);
-    js_ast.ArrayInitializer array = js_ast.ArrayInitializer(elements);
-    js_ast.Expression value = _makeConstantList(array);
-    return maybeAddListTypeArguments(constant, constant.type, value);
+    final array = js_ast.ArrayInitializer(elements);
+    final type = constant.type;
+    assert(constant.type.element == _commonElements.jsArrayClass);
+    final rti = _rtiNeed.classNeedsTypeArguments(type.element)
+        ? _reifiedType(type)
+        : js_ast.LiteralNull();
+    return _makeConstantList(array, rti);
   }
 
   @override
@@ -536,22 +536,7 @@
   }
 
   String stripComments(String rawJavaScript) {
-    return rawJavaScript.replaceAll(commentRE, '');
-  }
-
-  js_ast.Expression maybeAddListTypeArguments(
-    ConstantValue constant,
-    InterfaceType type,
-    js_ast.Expression value,
-  ) {
-    assert(type.element == _commonElements.jsArrayClass);
-    if (_rtiNeed.classNeedsTypeArguments(type.element)) {
-      return js_ast.Call(getHelperProperty(_commonElements.setArrayType), [
-        value,
-        _reifiedType(type),
-      ]);
-    }
-    return value;
+    return rawJavaScript.replaceAll(_commentRE, '');
   }
 
   js_ast.Expression _reifiedType(DartType type) {
@@ -567,3 +552,10 @@
     return _constantReferenceGenerator(constant.referenced);
   }
 }
+
+// Matches blank lines, comment lines and trailing comments that can't be part
+// of a string.
+final RegExp _commentRE = RegExp(
+  r'''^ *(//.*)?\n|  *//[^''"\n]*$''',
+  multiLine: true,
+);
diff --git a/pkg/compiler/lib/src/js_emitter/startup_emitter/fragment_emitter.dart b/pkg/compiler/lib/src/js_emitter/startup_emitter/fragment_emitter.dart
index 198795d..12c0ade 100644
--- a/pkg/compiler/lib/src/js_emitter/startup_emitter/fragment_emitter.dart
+++ b/pkg/compiler/lib/src/js_emitter/startup_emitter/fragment_emitter.dart
@@ -171,7 +171,8 @@
 // Given a list, marks it as constant.
 //
 // The runtime ensures that const-lists cannot be modified.
-function makeConstList(list) {
+function makeConstList(list, rti) {
+  if (rti != null) #setArrayType(list, rti);
   list.#arrayFlagsProperty = ${ArrayFlags.constant};
   return list;
 }
@@ -691,6 +692,9 @@
       'throwLateFieldADI': _emitter.staticFunctionAccess(
         _closedWorld.commonElements.throwLateFieldADI,
       ),
+      'setArrayType': _emitter.staticFunctionAccess(
+        _closedWorld.commonElements.setArrayType,
+      ),
       'arrayFlagsProperty': js.string(_namer.fixedNames.arrayFlagsPropertyName),
       'operatorIsPrefix': js.string(_namer.fixedNames.operatorIsPrefix),
       'tearOffCode': js.Block(
diff --git a/pkg/compiler/lib/src/js_emitter/startup_emitter/model_emitter.dart b/pkg/compiler/lib/src/js_emitter/startup_emitter/model_emitter.dart
index ecad087..2571b8a 100644
--- a/pkg/compiler/lib/src/js_emitter/startup_emitter/model_emitter.dart
+++ b/pkg/compiler/lib/src/js_emitter/startup_emitter/model_emitter.dart
@@ -190,9 +190,9 @@
     );
   }
 
-  js.Expression constantListGenerator(js.Expression array) {
+  js.Expression constantListGenerator(js.Expression array, js.Expression rti) {
     // TODO(floitsch): remove hard-coded name.
-    return js.js('makeConstList(#)', [array]);
+    return js.js('makeConstList(#, #)', [array, rti]);
   }
 
   bool isConstantInlinedOrAlreadyEmitted(ConstantValue constant) {
diff --git a/pkg/compiler/test/dump_info/data/members.dart b/pkg/compiler/test/dump_info/data/members.dart
index 9221f7c..e8d1ad4 100644
--- a/pkg/compiler/test/dump_info/data/members.dart
+++ b/pkg/compiler/test/dump_info/data/members.dart
@@ -41,12 +41,12 @@
   "code": "B.JavaScriptObject_methods = J.JavaScriptObject.prototype;\n"
 },
   {
-  "id": "constant/B.List_A = A._setArrayType(makeConstList([B.C_A]), A.findType(\"JSArray<A>\"));\n",
+  "id": "constant/B.List_A = makeConstList([B.C_A], A.findType(\"JSArray<A>\"));\n",
   "kind": "constant",
   "name": "",
-  "size": 78,
+  "size": 61,
   "outputUnit": "outputUnit/main",
-  "code": "B.List_A = A._setArrayType(makeConstList([B.C_A]), A.findType(\"JSArray<A>\"));\n"
+  "code": "B.List_A = makeConstList([B.C_A], A.findType(\"JSArray<A>\"));\n"
 }],
  deferredFiles=[{}],
  dependencies=[{}],