Skip to content

Refactor snapshot cache initialization to better support nnbd #2761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/dartdoc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ class Dartdoc {
return dartdocResults;
} finally {
// Clear out any cached tool snapshots and temporary directories.
SnapshotCache.instance?.dispose();
SnapshotCache.instanceFor(config.resourceProvider).dispose();
// ignore: unawaited_futures
ToolTempFileTracker.instance?.dispose();
}
Expand Down
20 changes: 11 additions & 9 deletions lib/src/tool_definition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ class DartToolDefinition extends ToolDefinition {
assert(args[0] == command.first);
// Set up flags to create a new snapshot, if needed, and use the first run
// as the training run.
SnapshotCache.createInstance(_resourceProvider);
var snapshot = SnapshotCache.instance.getSnapshot(command.first);
var snapshotCache = SnapshotCache.instanceFor(_resourceProvider);
var snapshot = snapshotCache.getSnapshot(command.first);
var snapshotFile = snapshot._snapshotFile;
var snapshotPath =
_resourceProvider.pathContext.absolute(snapshotFile.path);
Expand Down Expand Up @@ -212,11 +212,11 @@ class _Snapshot {
void _snapshotCompleted() => _snapshotCompleter.complete();
}

/// A singleton that keeps track of cached snapshot files. The [dispose]
/// A class that keeps track of cached snapshot files. The [dispose]
/// function must be called before process exit to clean up snapshots in the
/// cache.
class SnapshotCache {
static SnapshotCache _instance;
static final _instances = <ResourceProvider, SnapshotCache>{};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gnerally exactly one key/value pair in any given execution, yeah? Maybe like... two, for a test execution that uses mocks and real test packages.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The cache pretended before that there was only ever one, but I was suspicious of how it handled mocks or other inputs by ignoring them after the first call to createInstance if you hadn't dispose()d of them. That seemed like a bad idea in an asynchronous program, and was a pain to migrate without a refactor.


final Folder snapshotCache;
final ResourceProvider _resourceProvider;
Expand All @@ -227,10 +227,12 @@ class SnapshotCache {
: snapshotCache =
_resourceProvider.createSystemTemp('dartdoc_snapshot_cache_');

static SnapshotCache get instance => _instance;

static SnapshotCache createInstance(ResourceProvider resourceProvider) =>
_instance ??= SnapshotCache._(resourceProvider);
/// Returns a [SnapshotCache] for a given [ResourceProvider], creating one
/// only if one doesn't exist yet for the given [ResourceProvider].
factory SnapshotCache.instanceFor(ResourceProvider resourceProvider) {
return _instances.putIfAbsent(
resourceProvider, () => SnapshotCache._(resourceProvider));
}

_Snapshot getSnapshot(String toolPath) {
if (snapshots.containsKey(toolPath)) {
Expand All @@ -243,7 +245,7 @@ class SnapshotCache {
}

void dispose() {
_instance = null;
_instances.remove(_resourceProvider);
if (snapshotCache != null && snapshotCache.exists) {
return snapshotCache.delete();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/tool_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class ToolRunner {
// find out where their script was coming from as an absolute path on the
// filesystem.
envWithInput['DART_SNAPSHOT_CACHE'] = pathContext.absolute(
SnapshotCache.createInstance(resourceProvider).snapshotCache.path);
SnapshotCache.instanceFor(resourceProvider).snapshotCache.path);
if (toolDefinition.setupCommand != null) {
envWithInput['DART_SETUP_COMMAND'] = toolDefinition.setupCommand[0];
}
Expand Down
3 changes: 2 additions & 1 deletion test/tool_runner_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ echo:
tearDownAll(() {
tempDir?.deleteSync(recursive: true);
tracker?.dispose();
SnapshotCache.instance?.dispose();
SnapshotCache.instanceFor(pubPackageMetaProvider.resourceProvider)
.dispose();
setupFile = null;
tempDir = null;
});
Expand Down