Skip to content

Commit be76c1b

Browse files
scheglovCommit Queue
authored and
Commit Queue
committed
When FileSystemState.changeFile() release data from unlinkedUnitStore.
This fixes b/269188397 in Cider because now unlikned data is present in both ByteStore and UnlinkedUnitStore, or absent in both. Before this change we would get it from UnlinkedUnitStore, but expected that these bytes were also requested from ByteStore, so we attempted to release them (and got "underflow" of reference counter). Also, we now put the unlinked data into UnlinkedUnitStore not only after reading bytes, but also after building it. So, we second use will get it from the store, not the third. Change-Id: I89bf554511d3be82f67ef1f237061ba8a4693592 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/284745 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent c1f3a5a commit be76c1b

File tree

7 files changed

+90
-6
lines changed

7 files changed

+90
-6
lines changed

pkg/analyzer/lib/src/dart/analysis/byte_store.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class MemoryByteStore implements ByteStore {
4242
@visibleForTesting
4343
final Map<String, MemoryByteStoreEntry> map = {};
4444

45+
/// Throws [StateError] if [release] invoked when there is no entry.
46+
@visibleForTesting
47+
bool throwIfReleaseWithoutEntry = false;
48+
4549
@override
4650
Uint8List? get(String key) {
4751
final entry = map[key];
@@ -74,6 +78,8 @@ class MemoryByteStore implements ByteStore {
7478
if (entry.refCount == 0) {
7579
map.remove(key);
7680
}
81+
} else if (throwIfReleaseWithoutEntry) {
82+
throw StateError('No entry: $key');
7783
}
7884
}
7985
}

pkg/analyzer/lib/src/dart/analysis/file_state.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,7 @@ class FileState {
690690
var bytes = driverUnlinkedUnit.toBytes();
691691
_fsState._byteStore.putGet(_unlinkedKey!, bytes);
692692
testData?.unlinkedKeyPut.add(unlinkedKey);
693+
_fsState.unlinkedUnitStore.put(_unlinkedKey!, driverUnlinkedUnit);
693694
return driverUnlinkedUnit;
694695
});
695696
}
@@ -1196,6 +1197,9 @@ class FileSystemState {
11961197
// The removed file does not reference other files anymore.
11971198
file._kind?.dispose();
11981199

1200+
// Release this unlinked data.
1201+
unlinkedUnitStore.release(file.unlinkedKey);
1202+
11991203
// Recursively remove files that reference the removed file.
12001204
for (var reference in file.referencingFiles.toList()) {
12011205
changeFile(reference.path, removedFiles);

pkg/analyzer/lib/src/dart/analysis/unlinked_unit_store.dart

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/src/dart/analysis/unlinked_data.dart';
6+
import 'package:meta/meta.dart';
67

78
abstract class UnlinkedUnitStore {
89
void clear();
@@ -14,16 +15,17 @@ abstract class UnlinkedUnitStore {
1415
class UnlinkedUnitStoreImpl implements UnlinkedUnitStore {
1516
// TODO(jensj): Could we use finalizers and automatically clean up
1617
// this map?
17-
final Map<String, _UnlinkedUnitStoreData> _deserializedUnlinked = {};
18+
@visibleForTesting
19+
final Map<String, _UnlinkedUnitStoreData> map = {};
1820

1921
@override
2022
void clear() {
21-
_deserializedUnlinked.clear();
23+
map.clear();
2224
}
2325

2426
@override
2527
AnalysisDriverUnlinkedUnit? get(String key) {
26-
var lookup = _deserializedUnlinked[key];
28+
var lookup = map[key];
2729
if (lookup != null) {
2830
lookup.usageCount++;
2931
return lookup.value.target;
@@ -33,16 +35,16 @@ class UnlinkedUnitStoreImpl implements UnlinkedUnitStore {
3335

3436
@override
3537
void put(String key, AnalysisDriverUnlinkedUnit value) {
36-
_deserializedUnlinked[key] = _UnlinkedUnitStoreData(WeakReference(value));
38+
map[key] = _UnlinkedUnitStoreData(WeakReference(value));
3739
}
3840

3941
@override
4042
void release(String key) {
41-
var lookup = _deserializedUnlinked[key];
43+
var lookup = map[key];
4244
if (lookup != null) {
4345
lookup.usageCount--;
4446
if (lookup.usageCount <= 0) {
45-
_deserializedUnlinked.remove(key);
47+
map.remove(key);
4648
}
4749
}
4850
}

pkg/analyzer/test/src/dart/analysis/analyzer_state_printer.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import 'package:analyzer/src/dart/analysis/driver.dart';
88
import 'package:analyzer/src/dart/analysis/file_state.dart';
99
import 'package:analyzer/src/dart/analysis/library_context.dart';
1010
import 'package:analyzer/src/dart/analysis/library_graph.dart';
11+
import 'package:analyzer/src/dart/analysis/unlinked_unit_store.dart';
1112
import 'package:analyzer/src/dart/micro/resolve_file.dart';
1213
import 'package:collection/collection.dart';
1314
import 'package:path/path.dart';
1415
import 'package:test/test.dart';
1516

1617
class AnalyzerStatePrinter {
1718
final MemoryByteStore byteStore;
19+
final UnlinkedUnitStoreImpl unlinkedUnitStore;
1820
final IdProvider idProvider;
1921
final LibraryContext libraryContext;
2022
final bool omitSdkFiles;
@@ -27,6 +29,7 @@ class AnalyzerStatePrinter {
2729

2830
AnalyzerStatePrinter({
2931
required this.byteStore,
32+
required this.unlinkedUnitStore,
3033
required this.idProvider,
3134
required this.libraryContext,
3235
required this.omitSdkFiles,
@@ -47,6 +50,7 @@ class AnalyzerStatePrinter {
4750
_writeFiles(testData.fileSystem);
4851
_writeLibraryContext(testData.libraryContext);
4952
_writeElementFactory();
53+
_writeUnlinkedUnitStore();
5054
_writeByteStore();
5155
}
5256

@@ -595,6 +599,21 @@ class AnalyzerStatePrinter {
595599
}
596600
}
597601

602+
void _writeUnlinkedUnitStore() {
603+
_writelnWithIndent('unlinkedUnitStore');
604+
_withIndent(() {
605+
final groups = unlinkedUnitStore.map.entries.groupListsBy((element) {
606+
return element.value.usageCount;
607+
});
608+
609+
for (final groupEntry in groups.entries) {
610+
final keys = groupEntry.value.map((e) => e.key).toList();
611+
final shortKeys = idProvider.shortKeys(keys)..sort();
612+
_writelnWithIndent('${groupEntry.key}: $shortKeys');
613+
}
614+
});
615+
}
616+
598617
void _writeUriList(String name, Iterable<Uri> uriIterable) {
599618
final uriStrList = <String>[];
600619
for (final uri in uriIterable) {

pkg/analyzer/test/src/dart/micro/file_resolution.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import 'package:analyzer/src/dart/analysis/byte_store.dart';
1010
import 'package:analyzer/src/dart/analysis/file_state.dart';
1111
import 'package:analyzer/src/dart/analysis/library_context.dart';
1212
import 'package:analyzer/src/dart/analysis/performance_logger.dart';
13+
import 'package:analyzer/src/dart/analysis/unlinked_unit_store.dart';
1314
import 'package:analyzer/src/dart/micro/resolve_file.dart';
1415
import 'package:analyzer/src/dart/sdk/sdk.dart';
1516
import 'package:analyzer/src/test_utilities/find_element.dart';
@@ -69,6 +70,7 @@ class FileResolutionTest with ResourceProviderMixin, ResolutionTest {
6970
final buffer = StringBuffer();
7071
printer.AnalyzerStatePrinter(
7172
byteStore: byteStore,
73+
unlinkedUnitStore: fsState.unlinkedUnitStore as UnlinkedUnitStoreImpl,
7274
idProvider: _idProvider,
7375
libraryContext: libraryContext,
7476
omitSdkFiles: omitSdkFiles,

pkg/analyzer/test/src/dart/micro/simple_file_resolver_test.dart

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ elementFactory
116116
package:dart.test/a.dart
117117
package:dart.test/b.dart
118118
package:dart.test/c.dart
119+
unlinkedUnitStore
120+
1: [k00, k01, k02, k06, k07, k08, k09, k10]
119121
byteStore
120122
1: [k00, k01, k02, k03, k04, k05, k06, k07, k08, k09, k10, k11]
121123
''');
@@ -193,6 +195,8 @@ elementFactory
193195
package:dart.test/a.dart
194196
package:dart.test/b.dart
195197
package:dart.test/c.dart
198+
unlinkedUnitStore
199+
1: [k00, k01, k02, k06, k07, k08, k09, k10]
196200
byteStore
197201
1: [k00, k01, k02, k03, k04, k05, k06, k07, k08, k09, k10, k11]
198202
''');
@@ -270,6 +274,8 @@ elementFactory
270274
package:dart.test/a.dart
271275
package:dart.test/b.dart
272276
package:dart.test/c.dart
277+
unlinkedUnitStore
278+
1: [k00, k01, k02, k06, k07, k08, k09, k10]
273279
byteStore
274280
1: [k00, k01, k02, k03, k04, k05, k06, k07, k08, k09, k10, k11]
275281
''');
@@ -315,6 +321,8 @@ libraryCycles
315321
elementFactory
316322
hasElement
317323
package:dart.test/b.dart
324+
unlinkedUnitStore
325+
1: [k01, k06, k07, k08, k09, k10]
318326
byteStore
319327
1: [k01, k04, k06, k07, k08, k09, k10, k11]
320328
''');
@@ -392,6 +400,8 @@ elementFactory
392400
package:dart.test/a.dart
393401
package:dart.test/b.dart
394402
package:dart.test/c.dart
403+
unlinkedUnitStore
404+
1: [k00, k01, k02, k06, k07, k08, k09, k10]
395405
byteStore
396406
1: [k00, k01, k02, k03, k04, k05, k06, k07, k08, k09, k10, k11]
397407
''');
@@ -553,6 +563,8 @@ libraryCycles
553563
elementFactory
554564
hasElement
555565
package:dart.test/a.dart
566+
unlinkedUnitStore
567+
1: [k00, k01, k03, k04, k05, k06, k07]
556568
byteStore
557569
1: [k00, k01, k02, k03, k04, k05, k06, k07, k08]
558570
''');
@@ -574,6 +586,8 @@ libraryCycles
574586
get: []
575587
put: [k02]
576588
elementFactory
589+
unlinkedUnitStore
590+
1: [k03, k04, k05, k06, k07]
577591
byteStore
578592
1: [k03, k04, k05, k06, k07, k08]
579593
''');
@@ -617,6 +631,8 @@ libraryCycles
617631
elementFactory
618632
hasElement
619633
package:dart.test/a.dart
634+
unlinkedUnitStore
635+
1: [k00, k01, k03, k04, k05, k06, k07]
620636
byteStore
621637
1: [k00, k01, k02, k03, k04, k05, k06, k07, k08]
622638
''');
@@ -700,6 +716,8 @@ elementFactory
700716
hasElement
701717
package:dart.test/a.dart
702718
package:dart.test/c.dart
719+
unlinkedUnitStore
720+
1: [k00, k01, k02, k05, k06, k07, k08, k09]
703721
byteStore
704722
1: [k00, k01, k02, k03, k04, k05, k06, k07, k08, k09, k10]
705723
''');
@@ -728,6 +746,8 @@ libraryCycles
728746
get: []
729747
put: [k04]
730748
elementFactory
749+
unlinkedUnitStore
750+
1: [k05, k06, k07, k08, k09]
731751
byteStore
732752
1: [k05, k06, k07, k08, k09, k10]
733753
''');
@@ -794,6 +814,8 @@ elementFactory
794814
hasElement
795815
package:dart.test/a.dart
796816
package:dart.test/c.dart
817+
unlinkedUnitStore
818+
1: [k00, k01, k02, k05, k06, k07, k08, k09]
797819
byteStore
798820
1: [k00, k01, k02, k03, k04, k05, k06, k07, k08, k09, k10]
799821
''');
@@ -942,6 +964,8 @@ libraryCycles
942964
elementFactory
943965
hasElement
944966
package:dart.test/a.dart
967+
unlinkedUnitStore
968+
1: [k00, k02, k03, k04, k05, k06]
945969
byteStore
946970
1: [k00, k01, k02, k03, k04, k05, k06, k07]
947971
''');
@@ -961,6 +985,8 @@ libraryCycles
961985
get: []
962986
put: [k01]
963987
elementFactory
988+
unlinkedUnitStore
989+
1: [k00, k02, k03, k04, k05, k06]
964990
byteStore
965991
''');
966992
}
@@ -1511,6 +1537,8 @@ libraryCycles
15111537
elementFactory
15121538
hasReader
15131539
package:dart.test/a.dart
1540+
unlinkedUnitStore
1541+
1: [k00, k02, k03, k04, k05, k06]
15141542
byteStore
15151543
1: [k00, k01, k02, k03, k04, k05, k06, k07]
15161544
''');
@@ -1545,6 +1573,8 @@ elementFactory
15451573
package:dart.test/a.dart
15461574
hasReader
15471575
package:dart.test/a.dart
1576+
unlinkedUnitStore
1577+
1: [k00, k02, k03, k04, k05, k06]
15481578
byteStore
15491579
1: [k00, k01, k02, k03, k04, k05, k06, k07]
15501580
''');
@@ -1602,6 +1632,8 @@ elementFactory
16021632
hasReader
16031633
package:dart.test/a.dart
16041634
package:dart.test/b.dart
1635+
unlinkedUnitStore
1636+
1: [k00, k02, k03, k04, k05, k06, k08]
16051637
byteStore
16061638
1: [k00, k01, k02, k03, k04, k05, k06, k07, k08, k09]
16071639
''');
@@ -1668,6 +1700,8 @@ elementFactory
16681700
hasReader
16691701
package:dart.test/a.dart
16701702
package:dart.test/b.dart
1703+
unlinkedUnitStore
1704+
1: [k00, k02, k03, k04, k05, k06, k08]
16711705
byteStore
16721706
1: [k00, k01, k02, k03, k04, k05, k06, k07, k08, k09]
16731707
''');
@@ -1711,6 +1745,8 @@ libraryCycles
17111745
elementFactory
17121746
hasReader
17131747
package:dart.test/test.dart
1748+
unlinkedUnitStore
1749+
1: [k00, k02, k03, k04, k05, k06]
17141750
byteStore
17151751
1: [k00, k01, k02, k03, k04, k05, k06, k07]
17161752
''');
@@ -1751,6 +1787,8 @@ elementFactory
17511787
package:dart.test/test.dart
17521788
hasReader
17531789
package:dart.test/test.dart
1790+
unlinkedUnitStore
1791+
1: [k00, k02, k03, k04, k05, k06]
17541792
byteStore
17551793
1: [k00, k01, k02, k03, k04, k05, k06, k07]
17561794
''');
@@ -1936,6 +1974,8 @@ elementFactory
19361974
package:dart.aaa/a.dart
19371975
package:dart.aaa/b.dart
19381976
package:dart.aaa/c.dart
1977+
unlinkedUnitStore
1978+
1: [k00, k01, k02, k06, k07, k08, k09, k10]
19391979
byteStore
19401980
1: [k00, k01, k02, k03, k04, k05, k06, k07, k08, k09, k10, k11]
19411981
''');
@@ -1998,6 +2038,8 @@ elementFactory
19982038
hasElement
19992039
package:dart.aaa/a.dart
20002040
package:dart.aaa/c.dart
2041+
unlinkedUnitStore
2042+
1: [k00, k02, k06, k07, k08, k09, k10]
20012043
byteStore
20022044
1: [k00, k02, k03, k05, k06, k07, k08, k09, k10, k11]
20032045
''');
@@ -2167,6 +2209,8 @@ elementFactory
21672209
package:dart.aaa/d.dart
21682210
package:dart.aaa/e.dart
21692211
package:dart.aaa/f.dart
2212+
unlinkedUnitStore
2213+
1: [k00, k01, k02, k03, k04, k05, k12, k13, k14, k15, k16]
21702214
byteStore
21712215
1: [k00, k01, k02, k03, k04, k05, k06, k07, k08, k09, k10, k11, k12, k13, k14, k15, k16, k17]
21722216
''');
@@ -2278,6 +2322,8 @@ elementFactory
22782322
package:dart.aaa/c.dart
22792323
package:dart.aaa/d.dart
22802324
package:dart.aaa/f.dart
2325+
unlinkedUnitStore
2326+
1: [k00, k02, k03, k05, k12, k13, k14, k15, k16]
22812327
byteStore
22822328
1: [k00, k02, k03, k05, k06, k08, k09, k11, k12, k13, k14, k15, k16, k17]
22832329
''');
@@ -2319,6 +2365,8 @@ libraryCycles
23192365
elementFactory
23202366
hasElement
23212367
package:dart.aaa/a.dart
2368+
unlinkedUnitStore
2369+
1: [k00, k02, k03, k04, k05, k06]
23222370
byteStore
23232371
1: [k00, k01, k02, k03, k04, k05, k06, k07]
23242372
''');

pkg/analyzer/test/src/dart/resolution/context_collection_resolution.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'package:analyzer/src/dart/analysis/byte_store.dart';
1313
import 'package:analyzer/src/dart/analysis/driver.dart';
1414
import 'package:analyzer/src/dart/analysis/driver_based_analysis_context.dart';
1515
import 'package:analyzer/src/dart/analysis/experiments.dart';
16+
import 'package:analyzer/src/dart/analysis/unlinked_unit_store.dart';
1617
import 'package:analyzer/src/generated/engine.dart' show AnalysisOptionsImpl;
1718
import 'package:analyzer/src/generated/sdk.dart';
1819
import 'package:analyzer/src/summary2/kernel_compilation_service.dart';
@@ -180,6 +181,8 @@ abstract class ContextResolutionTest
180181
final buffer = StringBuffer();
181182
AnalyzerStatePrinter(
182183
byteStore: _byteStore,
184+
unlinkedUnitStore:
185+
analysisDriver.fsState.unlinkedUnitStore as UnlinkedUnitStoreImpl,
183186
idProvider: _idProvider,
184187
libraryContext: analysisDriver.libraryContext,
185188
omitSdkFiles: omitSdkFiles,

0 commit comments

Comments
 (0)