[dart2js] Add back kernel source file caching guarded by a flag.

Removing this cache means that errors from dill-loaded sources only display an offset rather than an exact file location. This makes debugging externally reported issues more difficult.

See here for more background: https://github.com/dart-lang/sdk/issues/52020#issuecomment-1513533855

Change-Id: I243d0bd340708ddbf979d77b5e9559751ee1b79e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/296040
Commit-Queue: Nate Biggs <natebiggs@google.com>
Reviewed-by: Sigmund Cherem <sigmund@google.com>
This commit is contained in:
Nate Biggs 2023-04-19 00:34:03 +00:00 committed by Commit Queue
parent 1c29e9ab3f
commit 21de916eb8
4 changed files with 35 additions and 11 deletions

View file

@ -190,6 +190,9 @@ class Flags {
static const String cfeConstants = '--cfe-constants';
static const String disableDiagnosticByteCache =
'--disable-diagnostic-byte-cache';
// `--no-shipping` and `--canary` control sets of flags. For simplicity, these
// flags live in options.dart.
// Shipping features default to on, but can be disabled individually. All

View file

@ -685,6 +685,7 @@ Future<api.CompilationResult> compile(List<String> argv,
_OneOption('${Flags.cfeInvocationModes}=.+', passThrough),
_OneOption('${Flags.invoker}=.+', setInvoker),
_OneOption('${Flags.verbosity}=.+', passThrough),
_OneOption(Flags.disableDiagnosticByteCache, passThrough),
// Experimental features.
// We don't provide documentation for these yet.
@ -979,12 +980,16 @@ Future<api.CompilationResult> compile(List<String> argv,
'The options --bazel-root and --multi-root cannot be supplied '
'together, please choose one or the other.');
}
inputProvider = BazelInputProvider(bazelPaths!, byteReader);
inputProvider = BazelInputProvider(bazelPaths!, byteReader,
disableByteCache: compilerOptions.disableDiagnosticByteCache);
} else if (multiRoots != null) {
inputProvider =
MultiRootInputProvider(multiRootScheme!, multiRoots!, byteReader);
inputProvider = MultiRootInputProvider(
multiRootScheme!, multiRoots!, byteReader,
disableByteCache: compilerOptions.disableDiagnosticByteCache);
} else {
inputProvider = CompilerSourceFileProvider(byteReader: byteReader);
inputProvider = CompilerSourceFileProvider(
byteReader: byteReader,
disableByteCache: compilerOptions.disableDiagnosticByteCache);
}
diagnostic.registerFileProvider(inputProvider);

View file

@ -599,6 +599,9 @@ class CompilerOptions implements DiagnosticOptions {
// Whether or not to dump a list of unused libraries.
bool dumpUnusedLibraries = false;
// Whether or not to disable byte cache for sources loaded from Kernel dill.
bool disableDiagnosticByteCache = false;
late FeatureOptions features;
// -------------------------------------------------
@ -737,6 +740,8 @@ class CompilerOptions implements DiagnosticOptions {
_extractStringOption(
options, '${Flags.verbosity}=', fe.Verbosity.defaultValue)!,
onError: onError)
..disableDiagnosticByteCache =
_hasOption(options, Flags.disableDiagnosticByteCache)
..features = featureOptions;
}

View file

@ -25,8 +25,10 @@ abstract class SourceFileProvider implements api.CompilerInput {
SourceFileByteReader byteReader;
final Set<Uri> _registeredUris = {};
final Map<Uri, Uri> _mappedUris = {};
final bool disableByteCache;
final Map<Uri, List<int>> _byteCache = {};
SourceFileProvider(this.byteReader);
SourceFileProvider(this.byteReader, {this.disableByteCache = true});
Future<api.Input<List<int>>> readBytesFromUri(
Uri resourceUri, api.InputKind inputKind) {
@ -56,9 +58,11 @@ abstract class SourceFileProvider implements api.CompilerInput {
if (!resourceUri.isAbsolute) {
resourceUri = cwd.resolveUri(resourceUri);
}
// Do not register URIs with schemes other than 'file' as these are internal
// libraries that we cannot read later.
registerUri(resourceUri);
if (!disableByteCache) {
_byteCache[resourceUri] = source;
}
}
/// Registers the URI and returns true if the URI is new.
@ -115,6 +119,11 @@ abstract class SourceFileProvider implements api.CompilerInput {
if (!resourceUri.isAbsolute) {
resourceUri = cwd.resolveUri(resourceUri);
}
if (_byteCache.containsKey(resourceUri)) {
return _sourceToFile(
resourceUri, _byteCache[resourceUri]!, api.InputKind.UTF8);
}
return resourceUri.isScheme('file')
? _readFromFileSync(resourceUri, api.InputKind.UTF8)
: null;
@ -152,8 +161,8 @@ Uint8List readAll(String filename, {bool zeroTerminated = true}) {
class CompilerSourceFileProvider extends SourceFileProvider {
CompilerSourceFileProvider(
{SourceFileByteReader byteReader =
const MemoryCopySourceFileByteReader()})
{SourceFileByteReader byteReader = const MemoryCopySourceFileByteReader(),
super.disableByteCache})
: super(byteReader);
@override
@ -541,7 +550,8 @@ class _BinaryOutputSinkWrapper extends api.BinaryOutputSink {
class BazelInputProvider extends SourceFileProvider {
final List<Uri> dirs;
BazelInputProvider(List<String> searchPaths, super.byteReader)
BazelInputProvider(List<String> searchPaths, super.byteReader,
{super.disableByteCache})
: dirs = searchPaths.map(_resolve).toList();
static Uri _resolve(String path) => Uri.base.resolve(path);
@ -588,7 +598,8 @@ class MultiRootInputProvider extends SourceFileProvider {
final List<Uri> roots;
final String markerScheme;
MultiRootInputProvider(this.markerScheme, this.roots, super.byteReader);
MultiRootInputProvider(this.markerScheme, this.roots, super.byteReader,
{super.disableByteCache});
@override
Future<api.Input<List<int>>> readFromUri(Uri uri,