55// DO NOT EDIT. This file was generated from async_import_cache.dart.
66// See tool/grind/synchronize.dart for details.
77//
8- // Checksum: 36bc42050cf2eb3a43f36376c4f06c1708eee777
8+ // Checksum: 4362e28e5cd425786c235d2a6a2bb60539403799
99//
1010// ignore_for_file: unused_import
1111
@@ -23,6 +23,7 @@ import 'importer/no_op.dart';
2323import 'importer/utils.dart' ;
2424import 'io.dart' ;
2525import 'logger.dart' ;
26+ import 'util/map.dart' ;
2627import 'util/nullable.dart' ;
2728import 'utils.dart' ;
2829
@@ -47,29 +48,26 @@ final class ImportCache {
4748 /// The `forImport` in each key is true when this canonicalization is for an
4849 /// `@import` rule. Otherwise, it's for a `@use` or `@forward` rule.
4950 ///
50- /// This cache isn't used for relative imports, because they depend on the
51- /// specific base importer. That's stored separately in
52- /// [_relativeCanonicalizeCache ] .
51+ /// This cache covers loads that go through the entire chain of [_importers] ,
52+ /// but it doesn't cover individual loads or loads in which any importer
53+ /// accesses `containingUrl` . See also [_perImporterCanonicalizeCache ] .
5354 final _canonicalizeCache = < (Uri , {bool forImport}), CanonicalizeResult ? > {};
5455
55- /// The canonicalized URLs for each non-canonical URL that's resolved using a
56- /// relative importer .
56+ /// Like [_canonicalizeCache] but also includes the specific importer in the
57+ /// key .
5758 ///
58- /// The map's keys have four parts:
59+ /// This is used to cache both relative imports from the base importer and
60+ /// individual importer results in the case where some other component of the
61+ /// importer chain isn't cacheable.
62+ final _perImporterCanonicalizeCache =
63+ < (Importer , Uri , {bool forImport}), CanonicalizeResult ? > {};
64+
65+ /// A map from the keys in [_perImporterCanonicalizeCache] that are generated
66+ /// for relative URL loads agains the base importer to the original relative
67+ /// URLs what were loaded.
5968 ///
60- /// 1. The URL passed to [canonicalize] (the same as in [_canonicalizeCache] ).
61- /// 2. Whether the canonicalization is for an `@import` rule.
62- /// 3. The `baseImporter` passed to [canonicalize] .
63- /// 4. The `baseUrl` passed to [canonicalize] .
64- ///
65- /// The map's values are the same as the return value of [canonicalize] .
66- final _relativeCanonicalizeCache = < (
67- Uri , {
68- bool forImport,
69- Importer baseImporter,
70- Uri ? baseUrl
71- }),
72- CanonicalizeResult ? > {};
69+ /// This is used to invalidate the cache when files are changed.
70+ final _nonCanonicalRelativeUrls = < (Importer , Uri , {bool forImport}), Uri > {};
7371
7472 /// The parsed stylesheets for each canonicalized import URL.
7573 final _importCache = < Uri , Stylesheet ? > {};
@@ -155,18 +153,16 @@ final class ImportCache {
155153 }
156154
157155 if (baseImporter != null && url.scheme == '' ) {
158- var relativeResult = _relativeCanonicalizeCache.putIfAbsent ((
159- url,
160- forImport: forImport,
161- baseImporter: baseImporter,
162- baseUrl: baseUrl
163- ), () {
164- var (result, cacheable) = _canonicalize (
165- baseImporter, baseUrl? .resolveUri (url) ?? url, baseUrl, forImport);
156+ var resolvedUrl = baseUrl? .resolveUri (url) ?? url;
157+ var key = (baseImporter, resolvedUrl, forImport: forImport);
158+ var relativeResult = _perImporterCanonicalizeCache.putIfAbsent (key, () {
159+ var (result, cacheable) =
160+ _canonicalize (baseImporter, resolvedUrl, baseUrl, forImport);
166161 assert (
167162 cacheable,
168163 "Relative loads should always be cacheable because they never "
169164 "provide access to the containing URL." );
165+ if (baseUrl != null ) _nonCanonicalRelativeUrls[key] = url;
170166 return result;
171167 });
172168 if (relativeResult != null ) return relativeResult;
@@ -182,17 +178,41 @@ final class ImportCache {
182178 // `canonicalize()` calls we've attempted are cacheable. Only if they are do
183179 // we store the result in the cache.
184180 var cacheable = true ;
185- for (var importer in _importers) {
181+ for (var i = 0 ; i < _importers.length; i++ ) {
182+ var importer = _importers[i];
183+ var perImporterKey = (importer, url, forImport: forImport);
184+ switch (_perImporterCanonicalizeCache.getOption (perImporterKey)) {
185+ case (var result? ,):
186+ return result;
187+ case (null ,):
188+ continue ;
189+ }
190+
186191 switch (_canonicalize (importer, url, baseUrl, forImport)) {
187192 case (var result? , true ) when cacheable:
188193 _canonicalizeCache[key] = result;
189194 return result;
190195
191- case (var result? , _):
192- return result;
193-
194- case (_, false ):
195- cacheable = false ;
196+ case (var result, true ) when ! cacheable:
197+ _perImporterCanonicalizeCache[perImporterKey] = result;
198+ if (result != null ) return result;
199+
200+ case (var result, false ):
201+ if (cacheable) {
202+ // If this is the first uncacheable result, add all previous results
203+ // to the per-importer cache so we don't have to re-run them for
204+ // future uses of this importer.
205+ for (var j = 0 ; j < i; j++ ) {
206+ _perImporterCanonicalizeCache[(
207+ _importers[j],
208+ url,
209+ forImport: forImport
210+ )] = null ;
211+ }
212+ cacheable = false ;
213+ }
214+
215+ if (result != null ) return result;
196216 }
197217 }
198218
@@ -312,7 +332,7 @@ final class ImportCache {
312332 Uri sourceMapUrl (Uri canonicalUrl) =>
313333 _resultsCache[canonicalUrl]? .sourceMapUrl ?? canonicalUrl;
314334
315- /// Clears the cached canonical version of the given [url] .
335+ /// Clears the cached canonical version of the given non-canonical [url] .
316336 ///
317337 /// Has no effect if the canonical version of [url] has not been cached.
318338 ///
@@ -321,7 +341,8 @@ final class ImportCache {
321341 void clearCanonicalize (Uri url) {
322342 _canonicalizeCache.remove ((url, forImport: false ));
323343 _canonicalizeCache.remove ((url, forImport: true ));
324- _relativeCanonicalizeCache.removeWhere ((key, _) => key.$1 == url);
344+ _perImporterCanonicalizeCache.removeWhere (
345+ (key, _) => key.$2 == url || _nonCanonicalRelativeUrls[key] == url);
325346 }
326347
327348 /// Clears the cached parse tree for the stylesheet with the given
0 commit comments