[dart2js] Stop storing entire emitted code buffer on emitter.

We are only using these buffers to get the length of the code buffers later. Instead just get and store the lengths up front.

Change-Id: I208c94267dc6569b094aec4fb170db658dc8887e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/337880
Reviewed-by: Mayank Patke <fishythefish@google.com>
Commit-Queue: Nate Biggs <natebiggs@google.com>
diff --git a/pkg/compiler/lib/src/js_emitter/startup_emitter/emitter.dart b/pkg/compiler/lib/src/js_emitter/startup_emitter/emitter.dart
index 36202e6..b4c55b5 100644
--- a/pkg/compiler/lib/src/js_emitter/startup_emitter/emitter.dart
+++ b/pkg/compiler/lib/src/js_emitter/startup_emitter/emitter.dart
@@ -242,8 +242,8 @@
   @override
   Map<OutputUnit, int> get generatedSizes {
     final mappedSizes = <OutputUnit, int>{};
-    _emitter.emittedOutputBuffers.forEach((outputUnit, outputData) {
-      mappedSizes[outputUnit] = outputData.length;
+    _emitter.emittedOutputSizes.forEach((outputUnit, dataLength) {
+      mappedSizes[outputUnit] = dataLength;
     });
     _emitter.omittedOutputUnits
         .forEach((outputUnit) => mappedSizes[outputUnit] = 0);
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 a6294f8..d0a07cc 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
@@ -119,8 +119,8 @@
   final SourceInformationStrategy _sourceInformationStrategy;
   final FragmentMerger fragmentMerger;
 
-  // The full code that is written to each hunk part-file.
-  final Map<OutputUnit, CodeOutput> emittedOutputBuffers = {};
+  // The length of the code that is written to each hunk part-file.
+  final Map<OutputUnit, int> emittedOutputSizes = {};
 
   final Set<OutputUnit> omittedOutputUnits = {};
 
@@ -360,7 +360,7 @@
     }
 
     // Return the total program size.
-    return emittedOutputBuffers.values.fold(0, (a, b) => a + b.length);
+    return emittedOutputSizes.values.fold(0, (a, b) => a + b);
   }
 
   /// Generates a simple header that provides the compiler's build id.
@@ -433,7 +433,6 @@
     CodeOutput mainOutput = StreamCodeOutput(
         _outputProvider.createOutputSink('', 'js', api.OutputType.js),
         codeOutputListeners);
-    emittedOutputBuffers[fragment.outputUnit] = mainOutput;
 
     js.Program program = js.Program([
       buildGeneratedBy(),
@@ -461,6 +460,7 @@
     }
 
     mainOutput.close();
+    emittedOutputSizes[fragment.outputUnit] = mainOutput.length;
 
     if (_shouldGenerateSourceMap) {
       _task.measureSubtask('source-maps', () {
@@ -540,6 +540,11 @@
     } else {
       output.close();
     }
+    for (final fragment in fragmentCode) {
+      for (final outputUnit in fragment.codeFragment.outputUnits) {
+        emittedOutputSizes[outputUnit] = output.length;
+      }
+    }
   }
 
   /// Writes a list of [CodeFragments] to [CodeOutput].
@@ -552,9 +557,6 @@
     for (var emittedCodeFragment in fragmentCode) {
       var codeFragment = emittedCodeFragment.codeFragment;
       var code = emittedCodeFragment.code;
-      for (var outputUnit in codeFragment.outputUnits) {
-        emittedOutputBuffers[outputUnit] = output;
-      }
       fragmentHashes[codeFragment] =
           writeCodeFragment(output, code, isFirst, outputFileName);
       isFirst = false;