Skip to content

Commit 8a0289e

Browse files
sigmundchCommit Bot
authored andcommitted
[dart2js] migrate import_set.dart
Change-Id: Ie582bf62803e31b952e110f2c6160d2237bb9d10 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/249952 Commit-Queue: Sigmund Cherem <sigmund@google.com> Reviewed-by: Stephen Adams <sra@google.com>
1 parent f51149e commit 8a0289e

File tree

3 files changed

+51
-47
lines changed

3 files changed

+51
-47
lines changed

pkg/compiler/lib/src/deferred_load/algorithm_state.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class AlgorithmState {
5050
/// update later if we cannot. For more detail on [oldSet] and [newSet],
5151
/// please see the comment in deferred_load.dart.
5252
void update(EntityData entityData, ImportSet oldSet, ImportSet newSet) {
53-
ImportSet currentSet = entityToSet[entityData];
53+
ImportSet currentSet = entityToSet[entityData] ?? importSets.emptySet;
5454

5555
// If [currentSet] == [newSet], then currentSet must include all of newSet.
5656
if (currentSet == newSet) return;
@@ -67,11 +67,11 @@ class AlgorithmState {
6767
if (currentSet == oldSet) {
6868
// Continue recursively updating from [oldSet] to [newSet].
6969
entityToSet[entityData] = newSet;
70-
updateDependencies(entityData, oldSet, newSet);
70+
_updateDependencies(entityData, oldSet, newSet);
7171
} else if (entityData.needsRecursiveUpdate) {
7272
assert(
7373
// Invariant: we must mark main before we mark any deferred import.
74-
newSet != importSets.mainSet || oldSet != null,
74+
newSet != importSets.mainSet || oldSet != importSets.emptySet,
7575
failedAt(
7676
NO_LOCATION_SPANNABLE,
7777
"Tried to assign to the main output unit, but it was assigned "
@@ -106,7 +106,7 @@ class AlgorithmState {
106106

107107
/// Updates the dependencies of a given [EntityData] from [oldSet] to
108108
/// [newSet].
109-
void updateDependencies(
109+
void _updateDependencies(
110110
EntityData entityData, ImportSet oldSet, ImportSet newSet) {
111111
assert(directDependencies.containsKey(entityData));
112112
var directDependenciesList = directDependencies[entityData];

pkg/compiler/lib/src/deferred_load/import_set.dart

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// @dart = 2.10
6-
75
import 'output_unit.dart';
86

97
import 'program_split_constraints/builder.dart' as psc show SetTransition;
@@ -61,12 +59,12 @@ class ImportSetLattice {
6159
final Map<ImportEntity, _DeferredImport> _importIndex = {};
6260

6361
/// The canonical instance representing the empty import set.
64-
final ImportSet _emptySet = ImportSet.empty();
62+
final ImportSet emptySet = _EmptyImportSet();
6563

6664
/// The [ImportSet] representing the main output unit.
67-
ImportSet _mainSet;
65+
late ImportSet _mainSet;
6866
ImportSet get mainSet {
69-
assert(_mainSet != null && _mainSet.unit != null);
67+
assert(_mainSet.unit != null);
7068
return _mainSet;
7169
}
7270

@@ -81,14 +79,14 @@ class ImportSetLattice {
8179
/// [ImportEntity].
8280
ImportSet _singleton(ImportEntity import) {
8381
// Ensure we have import in the index.
84-
return _emptySet._add(_wrap(import));
82+
return emptySet._add(_wrap(import));
8583
}
8684

8785
/// A helper method to convert a [Set<ImportEntity>] to an [ImportSet].
8886
ImportSet setOfImportsToImportSet(Set<ImportEntity> setOfImports) {
8987
List<_DeferredImport> imports = setOfImports.map(_wrap).toList();
9088
imports.sort((a, b) => a.index - b.index);
91-
var result = _emptySet;
89+
var result = emptySet;
9290
for (var import in imports) {
9391
result = result._add(import);
9492
}
@@ -124,8 +122,8 @@ class ImportSetLattice {
124122

125123
/// Get the import set that includes the union of [a] and [b].
126124
ImportSet union(ImportSet a, ImportSet b) {
127-
if (a == null || a.isEmpty) return b;
128-
if (b == null || b.isEmpty) return a;
125+
if (a is _EmptyImportSet) return b;
126+
if (b is _EmptyImportSet) return a;
129127

130128
// Create the union by merging the imports in canonical order. The sets are
131129
// basically lists linked by the `_previous` field in reverse order. We do a
@@ -137,11 +135,11 @@ class ImportSetLattice {
137135
List<_DeferredImport> imports = [];
138136

139137
while (true) {
140-
if (a.isEmpty) {
138+
if (a is! _NonEmptyImportSet) {
141139
result = b;
142140
break;
143141
}
144-
if (b.isEmpty || identical(a, b)) {
142+
if (b is! _NonEmptyImportSet || identical(a, b)) {
145143
result = a;
146144
break;
147145
}
@@ -181,7 +179,7 @@ class ImportSetLattice {
181179

182180
// Try and apply any [ImportSetTransition]s that have not yet been
183181
// applied to this [ImportSet].
184-
var candidateTransitions = allCandidateTransitions[originalImportSet];
182+
var candidateTransitions = allCandidateTransitions[originalImportSet]!;
185183
for (var transition in candidateTransitions) {
186184
if (originalImportSet.containsAll(transition.source)) {
187185
importSet = union(importSet, transition.transitions);
@@ -221,51 +219,33 @@ class ImportSetLattice {
221219
}
222220

223221
/// A canonical set of deferred imports.
224-
class ImportSet {
225-
/// Last element added to set.
226-
///
227-
/// This set comprises [_import] appended onto [_previous]. *Note*: [_import]
228-
/// is the last element in the set in the canonical order imposed by
229-
/// [ImportSetLattice].
230-
final _DeferredImport _import; // `null` for empty ImportSet
231-
/// The set containing all previous elements.
232-
final ImportSet _previous;
233-
final int length;
222+
abstract class ImportSet {
223+
/// Links to other import sets in the lattice by adding one import.
224+
final Map<_DeferredImport, _NonEmptyImportSet> _transitions = Maplet();
225+
226+
/// The output unit corresponding to this set of imports, if any.
227+
OutputUnit? unit;
234228

235-
bool get isEmpty => _import == null;
236-
bool get isNotEmpty => _import != null;
229+
int get length;
237230

238231
/// Returns an iterable over the imports in this set in canonical order.
239232
Iterable<_DeferredImport> collectImports() {
240233
List<_DeferredImport> result = [];
241234
ImportSet current = this;
242-
while (current.isNotEmpty) {
235+
while (current is _NonEmptyImportSet) {
243236
result.add(current._import);
244237
current = current._previous;
245238
}
246239
assert(result.length == this.length);
247240
return result.reversed;
248241
}
249242

250-
/// Links to other import sets in the lattice by adding one import.
251-
final Map<_DeferredImport, ImportSet> _transitions = Maplet();
252-
253-
ImportSet.empty()
254-
: _import = null,
255-
_previous = null,
256-
length = 0;
257-
258-
ImportSet(this._import, this._previous, this.length);
259-
260-
/// The output unit corresponding to this set of imports, if any.
261-
OutputUnit unit;
262-
263243
/// Returns true if this [ImportSet] contains all of [other].
264244
bool containsAll(ImportSet other) {
265245
var current = this;
266246
while (true) {
267-
if (other.isEmpty) return true;
268-
if (current.isEmpty) return false;
247+
if (other is! _NonEmptyImportSet) return true;
248+
if (current is! _NonEmptyImportSet) return false;
269249

270250
if (current._import.index > other._import.index) {
271251
current = current._previous;
@@ -284,8 +264,10 @@ class ImportSet {
284264
/// this current set. This should only be called from [ImportSetLattice],
285265
/// since it is where we preserve this invariant.
286266
ImportSet _add(_DeferredImport import) {
287-
assert(_import == null || import.index > _import.index);
288-
return _transitions[import] ??= ImportSet(import, this, length + 1);
267+
var self = this;
268+
assert(self is! _NonEmptyImportSet || import.index > self._import.index);
269+
return _transitions[import] ??=
270+
_NonEmptyImportSet(import, this, length + 1);
289271
}
290272

291273
@override
@@ -304,3 +286,25 @@ class ImportSet {
304286
Set<ImportEntity> toSet() =>
305287
collectImports().map((i) => i.declaration).toSet();
306288
}
289+
290+
class _NonEmptyImportSet extends ImportSet {
291+
/// Last element added to set.
292+
///
293+
/// This set comprises [_import] appended onto [_previous]. *Note*: [_import]
294+
/// is the last element in the set in the canonical order imposed by
295+
/// [ImportSetLattice].
296+
final _DeferredImport _import; // `null` for empty ImportSet
297+
298+
/// The set containing all previous elements.
299+
final ImportSet _previous;
300+
301+
@override
302+
final int length;
303+
304+
_NonEmptyImportSet(this._import, this._previous, this.length);
305+
}
306+
307+
class _EmptyImportSet extends ImportSet {
308+
@override
309+
int get length => 0;
310+
}

pkg/compiler/lib/src/deferred_load/work_queue.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class WorkQueue {
6767
var entityData = item.entityData;
6868
pendingWorkItems.remove(entityData);
6969
state.processEntity(entityData);
70-
ImportSet oldSet = state.entityToSet[entityData];
70+
ImportSet oldSet = state.entityToSet[entityData] ?? _importSets.emptySet;
7171
ImportSet newSet = _importSets.union(oldSet, item.importsToAdd);
7272
state.update(entityData, oldSet, newSet);
7373
}

0 commit comments

Comments
 (0)