Make LibraryCycle.linkedKey/macroKey computable properties.

Change-Id: I64a1d63fc7f7fecda38c497672695af65e4f8384
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/250107
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analyzer/lib/src/dart/analysis/library_context.dart b/pkg/analyzer/lib/src/dart/analysis/library_context.dart
index 696c370..ed95565 100644
--- a/pkg/analyzer/lib/src/dart/analysis/library_context.dart
+++ b/pkg/analyzer/lib/src/dart/analysis/library_context.dart
@@ -29,7 +29,6 @@
 import 'package:analyzer/src/summary2/macro.dart';
 import 'package:analyzer/src/summary2/reference.dart';
 import 'package:analyzer/src/util/performance/operation_performance.dart';
-import 'package:analyzer/src/utilities/extensions/collection.dart';
 import 'package:collection/collection.dart';
 import 'package:path/src/context.dart';
 
@@ -167,10 +166,9 @@
         }
       }
 
-      var resolutionKey = '${cycle.apiSignature}.linked_bundle';
-      var resolutionBytes = byteStore.get(resolutionKey);
+      var linkedBytes = byteStore.get(cycle.linkedKey);
 
-      if (resolutionBytes == null) {
+      if (linkedBytes == null) {
         librariesLinkedTimer.start();
 
         testData?.linkedCycles.add(
@@ -232,41 +230,38 @@
           _throwLibraryCycleLinkException(cycle, exception, stackTrace);
         }
 
-        resolutionBytes = linkResult.resolutionBytes;
-        byteStore.putGet(resolutionKey, resolutionBytes);
-        performance.getDataInt('bytesPut').add(resolutionBytes.length);
-        testData?.forCycle(cycle).putKeys.add(resolutionKey);
-        bytesPut += resolutionBytes.length;
+        linkedBytes = linkResult.resolutionBytes;
+        byteStore.putGet(cycle.linkedKey, linkedBytes);
+        performance.getDataInt('bytesPut').add(linkedBytes.length);
+        testData?.forCycle(cycle).putKeys.add(cycle.linkedKey);
+        bytesPut += linkedBytes.length;
 
         librariesLinkedTimer.stop();
       } else {
-        testData?.forCycle(cycle).getKeys.add(resolutionKey);
-        performance.getDataInt('bytesGet').add(resolutionBytes.length);
+        testData?.forCycle(cycle).getKeys.add(cycle.linkedKey);
+        performance.getDataInt('bytesGet').add(linkedBytes.length);
         performance.getDataInt('libraryLoadCount').add(cycle.libraries.length);
         // TODO(scheglov) Take / clear parsed units in files.
-        bytesGet += resolutionBytes.length;
+        bytesGet += linkedBytes.length;
         librariesLoaded += cycle.libraries.length;
         elementFactory.addBundle(
           BundleReader(
             elementFactory: elementFactory,
             unitsInformativeBytes: unitsInformativeBytes,
-            resolutionBytes: resolutionBytes,
+            resolutionBytes: linkedBytes,
           ),
         );
       }
-      // TODO(scheglov) We probably should set this key when create the cycle
-      cycle.resolutionKey = resolutionKey;
 
       final macroKernelBuilder = this.macroKernelBuilder;
       if (macroKernelBuilder != null && macroLibraries.isNotEmpty) {
-        var macroKernelKey = '${cycle.implSignature}.macro_kernel';
-        var macroKernelBytes = byteStore.get(macroKernelKey);
+        var macroKernelBytes = byteStore.get(cycle.macroKey);
         if (macroKernelBytes == null) {
           macroKernelBytes = await macroKernelBuilder.build(
             fileSystem: _MacroFileSystem(fileSystemState),
             libraries: macroLibraries,
           );
-          byteStore.putGet(macroKernelKey, macroKernelBytes);
+          byteStore.putGet(cycle.macroKey, macroKernelBytes);
           bytesPut += macroKernelBytes.length;
         } else {
           bytesGet += macroKernelBytes.length;
@@ -317,8 +312,7 @@
     loadedBundles.removeWhere((cycle) {
       final cycleFiles = cycle.libraries.map((e) => e.file);
       if (cycleFiles.any(removed.contains)) {
-        // TODO(scheglov) It should be never null.
-        removedKeys.addIfNotNull(cycle.resolutionKey);
+        removedKeys.add(cycle.linkedKey);
         return true;
       }
       return false;
@@ -333,8 +327,7 @@
     final uriSet = <Uri>{};
 
     for (final cycle in loadedBundles) {
-      // TODO(scheglov) It should be never null.
-      keySet.addIfNotNull(cycle.resolutionKey);
+      keySet.add(cycle.linkedKey);
       uriSet.addAll(cycle.libraries.map((e) => e.file.uri));
     }
 
diff --git a/pkg/analyzer/lib/src/dart/analysis/library_graph.dart b/pkg/analyzer/lib/src/dart/analysis/library_graph.dart
index 6cdb13d..405c981 100644
--- a/pkg/analyzer/lib/src/dart/analysis/library_graph.dart
+++ b/pkg/analyzer/lib/src/dart/analysis/library_graph.dart
@@ -55,10 +55,6 @@
   /// include [implSignature] of the macro defining library.
   String implSignature;
 
-  /// The key of the resolution cache entry.
-  /// TODO(scheglov) clean up
-  String? resolutionKey;
-
   late final bool hasMacroClass = () {
     for (final library in libraries) {
       for (final file in library.file.libraryFiles) {
@@ -86,6 +82,12 @@
     }
   }
 
+  /// The key of the linked libraries in the byte store.
+  String get linkedKey => '$apiSignature.linked';
+
+  /// The key of the macro kernel in the byte store.
+  String get macroKey => '$implSignature.macro_kernel';
+
   /// Invalidate this cycle and any cycles that directly or indirectly use it.
   ///
   /// Practically invalidation means that we clear the library cycle in all the
diff --git a/pkg/analyzer/test/src/dart/analysis/analyzer_state_printer.dart b/pkg/analyzer/test/src/dart/analysis/analyzer_state_printer.dart
index f2c32d5..c7eab9b 100644
--- a/pkg/analyzer/test/src/dart/analysis/analyzer_state_printer.dart
+++ b/pkg/analyzer/test/src/dart/analysis/analyzer_state_printer.dart
@@ -460,7 +460,7 @@
             _writelnWithIndent('current: $id');
             _withIndent(() {
               // TODO(scheglov) Print it with the cycle instead?
-              final short = idProvider.shortKey(current.resolutionKey!);
+              final short = idProvider.shortKey(current.linkedKey);
               _writelnWithIndent('key: $short');
             });
           }