Replace CiderByteStore methods with variants without signature.

Change-Id: I42c3aa456554974c1f44b95833e14487542539a5
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/246986
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Keerti Parthasarathy <keertip@google.com>
This commit is contained in:
Konstantin Shcheglov 2022-06-02 21:50:35 +00:00 committed by Commit Bot
parent f28a665c4e
commit 3a1a08106e
4 changed files with 68 additions and 81 deletions

View file

@ -5,7 +5,6 @@
import 'dart:typed_data';
import 'package:analyzer/src/dart/analysis/cache.dart';
import 'package:collection/collection.dart';
class CacheData {
final int id;
@ -23,18 +22,25 @@ class CacheData {
/// Note that associations are not guaranteed to be persistent. The value
/// associated with a key can change or become `null` at any point in time.
abstract class CiderByteStore {
/// Return the bytes associated with the errors for given [key] and
/// [signature].
/// Return the bytes associated with the [key], and increment the reference
/// count.
///
/// Return `null` if the association does not exist.
CacheData? get(String key, Uint8List signature);
Uint8List? get2(String key);
/// Associate the given [bytes] with the [key] and [signature]. Return the
/// [CacheData].
CacheData putGet(String key, Uint8List signature, Uint8List bytes);
/// Associate [bytes] with [key].
/// Return an internalized version of [bytes], the reference count is `1`.
///
/// This method will throw an exception if there is already an association
/// for the [key]. The client should either use [get2] to access data,
/// or first [release2] it.
Uint8List putGet2(String key, Uint8List bytes);
/// Used to decrement reference count for the given ids, if implemented.
void release(Iterable<int> ids);
/// Decrement the reference count for every key in [keys].
void release2(Iterable<String> keys);
}
class CiderByteStoreTestView {
@ -42,45 +48,33 @@ class CiderByteStoreTestView {
}
class CiderCachedByteStore implements CiderByteStore {
final Cache<String, CiderCacheEntry> _cache;
int idCounter = 0;
final Cache<String, Uint8List> _cache;
/// This field gets value only during testing.
CiderByteStoreTestView? testView;
CiderCachedByteStore(int maxCacheSize)
: _cache = Cache<String, CiderCacheEntry>(
maxCacheSize, (v) => v.data.bytes.length);
: _cache = Cache<String, Uint8List>(maxCacheSize, (v) => v.length);
@override
CacheData? get(String key, Uint8List signature) {
final entry = _cache.get(key);
if (entry != null &&
const ListEquality<int>().equals(entry.signature, signature)) {
return entry.data;
}
return null;
Uint8List? get2(String key) {
return _cache.get(key);
}
@override
CacheData putGet(String key, Uint8List signature, Uint8List bytes) {
idCounter++;
var entry = CiderCacheEntry(signature, CacheData(idCounter, bytes));
_cache.put(key, entry);
Uint8List putGet2(String key, Uint8List bytes) {
_cache.put(key, bytes);
testView?.length++;
return entry.data;
return bytes;
}
@override
void release(Iterable<int> ids) {
// do nothing
}
}
class CiderCacheEntry {
final CacheData data;
final Uint8List signature;
CiderCacheEntry(this.signature, this.data);
@override
void release2(Iterable<String> keys) {
// TODO(scheglov) implement
}
}

View file

@ -139,7 +139,7 @@ class FileState {
Source get source => _location.source;
int get unlinkedId => _unlinked.unlinkedId;
String get unlinkedKey => _unlinked.unlinkedKey;
UnlinkedUnit get unlinkedUnit => _unlinked.unlinked.unit;
@ -330,12 +330,12 @@ class FileSystemState {
}
}
/// Clears all the cached files. Returns the list of ids of all the removed
/// Clears all the cached files. Returns the list of keys of all the removed
/// files.
Set<int> collectSharedDataIdentifiers() {
var result = <int>{};
Set<String> collectSharedDataKeys() {
var result = <String>{};
for (var file in _pathToFile.values) {
result.add(file._unlinked.unlinkedId);
result.add(file._unlinked.unlinkedKey);
}
return result;
}
@ -560,9 +560,9 @@ class LibraryCycle {
/// The hash of all the paths of the files in this cycle.
late String cyclePathsHash;
/// The ID of the resolution cache entry.
/// The key of the resolution cache entry.
/// It is `null` if we failed to load libraries of the cycle.
int? resolutionId;
String? resolutionKey;
LibraryCycle();
@ -713,8 +713,8 @@ class _FileStateUnlinked {
final bool exists;
final CiderUnlinkedUnit unlinked;
/// id of the cache entry with unlinked data.
final int unlinkedId;
/// Key of the cache entry with unlinked data.
final String unlinkedKey;
factory _FileStateUnlinked({
required _FileStateLocation location,
@ -723,7 +723,6 @@ class _FileStateUnlinked {
}) {
location._fsState.testView.refreshedFiles.add(location.path);
int unlinkedId;
CiderUnlinkedUnit unlinked;
var digest = performance.run('digest', (performance) {
@ -734,15 +733,14 @@ class _FileStateUnlinked {
var exists = digest.isNotEmpty;
var unlinkedKey = '${location.path}.unlinked';
final unlinkedKey = '${hex.encode(digest)}.unlinked';
var isUnlinkedFromCache = true;
// Prepare bytes of the unlinked bundle - existing or new.
// TODO(migration): should not be nullable
Uint8List? unlinkedBytes;
{
var unlinkedData = location._fsState._byteStore.get(unlinkedKey, digest);
unlinkedBytes = unlinkedData?.bytes;
unlinkedBytes = location._fsState._byteStore.get2(unlinkedKey);
if (unlinkedBytes == null || unlinkedBytes.isEmpty) {
isUnlinkedFromCache = false;
@ -763,14 +761,12 @@ class _FileStateUnlinked {
var unlinkedUnit = serializeAstCiderUnlinked(unit);
unlinkedBytes = unlinkedUnit.toBytes();
performance.getDataInt('length').add(unlinkedBytes!.length);
unlinkedData = location._fsState._byteStore
.putGet(unlinkedKey, digest, unlinkedBytes!);
unlinkedBytes = unlinkedData!.bytes;
unlinkedBytes =
location._fsState._byteStore.putGet2(unlinkedKey, unlinkedBytes!);
});
unlinked = CiderUnlinkedUnit.fromBytes(unlinkedBytes!);
}
unlinkedId = unlinkedData!.id;
}
// Read the unlinked bundle.
@ -782,7 +778,7 @@ class _FileStateUnlinked {
digest: digest,
exists: exists,
unlinked: unlinked,
unlinkedId: unlinkedId,
unlinkedKey: unlinkedKey,
);
if (isUnlinkedFromCache) {
performance.run('prefetch', (_) {
@ -798,7 +794,7 @@ class _FileStateUnlinked {
required this.digest,
required this.exists,
required this.unlinked,
required this.unlinkedId,
required this.unlinkedKey,
}) : _partOfLibrary = partOfLibrary;
FileState? get partOfLibrary {

View file

@ -114,10 +114,10 @@ class FileResolver {
_LibraryContext? libraryContext;
/// List of ids for cache elements that are invalidated. Track elements that
/// List of keys for cache elements that are invalidated. Track elements that
/// are invalidated during [changeFile]. Used in [releaseAndClearRemovedIds]
/// to release the cache items and is then cleared.
final Set<int> removedCacheIds = {};
final Set<String> removedCacheKeys = {};
/// The cache of file results, cleared on [changeFile].
///
@ -173,21 +173,21 @@ class FileResolver {
// Schedule disposing references to cached unlinked data.
for (var removedFile in removedFiles) {
removedCacheIds.add(removedFile.unlinkedId);
removedCacheKeys.add(removedFile.unlinkedKey);
}
// Remove libraries represented by removed files.
// If we need these libraries later, we will relink and reattach them.
if (libraryContext != null) {
libraryContext!.remove(removedFiles, removedCacheIds);
libraryContext!.remove(removedFiles, removedCacheKeys);
}
}
/// Collects all the cached artifacts and add all the cache id's for the
/// removed artifacts to [removedCacheIds].
/// removed artifacts to [removedCacheKeys].
void collectSharedDataIdentifiers() {
removedCacheIds.addAll(fsState!.collectSharedDataIdentifiers());
removedCacheIds.addAll(libraryContext!.collectSharedDataIdentifiers());
removedCacheKeys.addAll(fsState!.collectSharedDataKeys());
removedCacheKeys.addAll(libraryContext!.collectSharedDataKeys());
}
/// Looks for references to the given Element. All the files currently
@ -252,7 +252,7 @@ class FileResolver {
);
var file = fileContext.file;
var errorsSignatureBuilder = ApiSignature();
final errorsSignatureBuilder = ApiSignature();
errorsSignatureBuilder.addBytes(file.libraryCycle.signature);
errorsSignatureBuilder.addBytes(file.digest);
final errorsKey = '${errorsSignatureBuilder.toHex()}.errors';
@ -414,10 +414,10 @@ class FileResolver {
_resetContextObjects();
}
/// Update the cache with list of invalidated ids and clears [removedCacheIds].
/// Releases from the cache and clear [removedCacheKeys].
void releaseAndClearRemovedIds() {
byteStore.release(removedCacheIds);
removedCacheIds.clear();
byteStore.release2(removedCacheKeys);
removedCacheKeys.clear();
}
/// Remove cached [FileState]'s that were not used in the current analysis
@ -427,7 +427,7 @@ class FileResolver {
void removeFilesNotNecessaryForAnalysisOf(List<String> files) {
var removedFiles = fsState!.removeUnusedFiles(files);
for (var removedFile in removedFiles) {
removedCacheIds.add(removedFile.unlinkedId);
removedCacheKeys.add(removedFile.unlinkedKey);
}
}
@ -829,20 +829,20 @@ class _LibraryContext {
/// Clears all the loaded libraries. Returns the cache ids for the removed
/// artifacts.
Set<int> collectSharedDataIdentifiers() {
var idSet = <int>{};
Set<String> collectSharedDataKeys() {
var keySet = <String>{};
void addIfNotNull(int? id) {
if (id != null) {
idSet.add(id);
void addIfNotNull(String? key) {
if (key != null) {
keySet.add(key);
}
}
for (var cycle in loadedBundles) {
addIfNotNull(cycle.resolutionId);
addIfNotNull(cycle.resolutionKey);
}
loadedBundles.clear();
return idSet;
return keySet;
}
/// Load data required to access elements of the given [targetLibrary].
@ -864,9 +864,8 @@ class _LibraryContext {
await loadBundle(directDependency);
}
var resolutionKey = '${cycle.cyclePathsHash}.resolution';
var resolutionData = byteStore.get(resolutionKey, cycle.signature);
var resolutionBytes = resolutionData?.bytes;
var resolutionKey = '${cycle.signatureStr}.resolution';
var resolutionBytes = byteStore.get2(resolutionKey);
var unitsInformativeBytes = <Uri, Uint8List>{};
for (var library in cycle.libraries) {
@ -939,9 +938,7 @@ class _LibraryContext {
librariesLinked += cycle.libraries.length;
resolutionBytes = linkResult.resolutionBytes;
resolutionData =
byteStore.putGet(resolutionKey, cycle.signature, resolutionBytes);
resolutionBytes = resolutionData.bytes;
resolutionBytes = byteStore.putGet2(resolutionKey, resolutionBytes);
performance.getDataInt('bytesPut').add(resolutionBytes.length);
librariesLinkedTimer.stop();
@ -956,7 +953,7 @@ class _LibraryContext {
),
);
}
cycle.resolutionId = resolutionData!.id;
cycle.resolutionKey = resolutionKey;
// We might have just linked dart:core, ensure the type provider.
_createElementFactoryTypeProvider();
@ -975,22 +972,22 @@ class _LibraryContext {
/// Remove libraries represented by the [removed] files.
/// If we need these libraries later, we will relink and reattach them.
void remove(List<FileState> removed, Set<int> removedIds) {
void remove(List<FileState> removed, Set<String> removedKeys) {
elementFactory.removeLibraries(
removed.map((e) => e.uriStr).toSet(),
);
var removedSet = removed.toSet();
void addIfNotNull(int? id) {
if (id != null) {
removedIds.add(id);
void addIfNotNull(String? key) {
if (key != null) {
removedKeys.add(key);
}
}
loadedBundles.removeWhere((cycle) {
if (cycle.libraries.any(removedSet.contains)) {
addIfNotNull(cycle.resolutionId);
addIfNotNull(cycle.resolutionKey);
return true;
}
return false;

View file

@ -337,7 +337,7 @@ class A {}
await resolveFile(aPath);
fileResolver.collectSharedDataIdentifiers();
expect(fileResolver.removedCacheIds.length,
expect(fileResolver.removedCacheKeys.length,
(fileResolver.byteStore as CiderCachedByteStore).testView!.length);
}