Replace CiderMemoryByteStore with CiderCachedByteStore.

Change-Id: I6301cfbbaa4a65b5f1d326174879ebddff14cc98
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/164662
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Keerti Parthasarathy <keertip@google.com>
diff --git a/pkg/analyzer/lib/src/dart/micro/cider_byte_store.dart b/pkg/analyzer/lib/src/dart/micro/cider_byte_store.dart
index 5d97366..57531d9 100644
--- a/pkg/analyzer/lib/src/dart/micro/cider_byte_store.dart
+++ b/pkg/analyzer/lib/src/dart/micro/cider_byte_store.dart
@@ -2,6 +2,7 @@
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
 
+import 'package:analyzer/src/dart/analysis/cache.dart';
 import 'package:collection/collection.dart';
 
 /// Store of bytes associated with string keys and a hash.
@@ -23,19 +24,16 @@
   void put(String key, List<int> signature, List<int> bytes);
 }
 
-class CiderCacheEntry {
-  final List<int> signature;
-  final List<int> bytes;
+class CiderCachedByteStore implements CiderByteStore {
+  final Cache<String, CiderCacheEntry> _cache;
 
-  CiderCacheEntry(this.signature, this.bytes);
-}
-
-class CiderMemoryByteStore implements CiderByteStore {
-  final Map<String, CiderCacheEntry> _map = {};
+  CiderCachedByteStore(int maxCacheSize)
+      : _cache =
+            Cache<String, CiderCacheEntry>(maxCacheSize, (v) => v.bytes.length);
 
   @override
   List<int> get(String key, List<int> signature) {
-    var entry = _map[key];
+    var entry = _cache.get(key, () => null);
 
     if (entry != null &&
         const ListEquality<int>().equals(entry.signature, signature)) {
@@ -46,6 +44,13 @@
 
   @override
   void put(String key, List<int> signature, List<int> bytes) {
-    _map[key] = CiderCacheEntry(signature, bytes);
+    _cache.put(key, CiderCacheEntry(signature, bytes));
   }
 }
+
+class CiderCacheEntry {
+  final List<int> signature;
+  final List<int> bytes;
+
+  CiderCacheEntry(this.signature, this.bytes);
+}
diff --git a/pkg/analyzer/lib/src/dart/micro/resolve_file.dart b/pkg/analyzer/lib/src/dart/micro/resolve_file.dart
index 206d69c..5216076 100644
--- a/pkg/analyzer/lib/src/dart/micro/resolve_file.dart
+++ b/pkg/analyzer/lib/src/dart/micro/resolve_file.dart
@@ -40,6 +40,9 @@
 import 'package:meta/meta.dart';
 import 'package:yaml/yaml.dart';
 
+const M = 1024 * 1024 /*1 MiB*/;
+const memoryCacheSize = 200 * M;
+
 class FileContext {
   final AnalysisOptionsImpl analysisOptions;
   final FileState file;
@@ -113,7 +116,7 @@
         getFileDigest = getFileDigest,
         prefetchFiles = prefetchFiles,
         workspace = workspace {
-    byteStore ??= CiderMemoryByteStore();
+    byteStore ??= CiderCachedByteStore(memoryCacheSize);
     this.byteStore = byteStore;
     _libraryContextReset = _LibraryContextReset(
       fileResolver: this,
diff --git a/pkg/analyzer/test/src/dart/micro/file_resolution.dart b/pkg/analyzer/test/src/dart/micro/file_resolution.dart
index 3e90ad5..250b588 100644
--- a/pkg/analyzer/test/src/dart/micro/file_resolution.dart
+++ b/pkg/analyzer/test/src/dart/micro/file_resolution.dart
@@ -22,7 +22,8 @@
 class FileResolutionTest with ResourceProviderMixin, ResolutionTest {
   static final String _testFile = '/workspace/dart/test/lib/test.dart';
 
-  final CiderByteStore byteStore = CiderMemoryByteStore();
+  final CiderByteStore byteStore =
+      CiderCachedByteStore(20 * 1024 * 1024 /* 20 MB */);
 
   final StringBuffer logBuffer = StringBuffer();
   PerformanceLog logger;