Skip to content

Commit cba613f

Browse files
committed
Optimize library cycle loader for many library cycles.
1 parent 21408da commit cba613f

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

build_runner/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## 2.10.3-wip
22

3+
- Performance: improve scalability with the number of library cycles, making
4+
builds much faster for some large codebases.
35
- Bug fix: fix crash when you run `dart run build_runner build` in a
46
subdirectory of a package.
57
- Bug fix: in a workspace, generate for transitive dependencies of the current

build_runner/lib/src/build/library_cycle_graph/library_cycle_graph_loader.dart

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -341,24 +341,21 @@ class LibraryCycleGraphLoader {
341341
///
342342
/// A [_graphs] entry will be created for each ID in [newCycles].
343343
void _buildGraphs(int phase, {required List<LibraryCycle> newCycles}) {
344-
// Build lookup from ID to [LibraryCycle] including new and existing cycles.
345-
final existingCycles = <LibraryCycle>[];
346-
for (final phasedCycle in _cycles.values) {
347-
if (phasedCycle.isExpiredAt(phase: phase)) continue;
348-
existingCycles.add(phasedCycle.valueAt(phase: phase));
349-
}
344+
// Cycles by ID at the current phase.
350345
final cycleById = <AssetId, LibraryCycle>{};
351-
for (final cycle in existingCycles) {
352-
for (final id in cycle.ids) {
353-
cycleById[id] = cycle;
354-
}
355-
}
356346
for (final cycle in newCycles) {
357347
for (final id in cycle.ids) {
358348
cycleById[id] = cycle;
359349
}
360350
}
361351

352+
/// Lookups up [id] in [cycleById], falling back to [_cycles] if it's not
353+
/// present.
354+
LibraryCycle lookupLibraryCycle(AssetId id) =>
355+
cycleById.putIfAbsent(id, () {
356+
return _cycles[id]!.valueAt(phase: phase);
357+
});
358+
362359
// Create the graph for each cycle in [newCycles].
363360
for (final root in newCycles) {
364361
final graph = LibraryCycleGraphBuilder()..root.replace(root);
@@ -379,7 +376,7 @@ class LibraryCycleGraphLoader {
379376
for (final id in root.ids) {
380377
final assetDeps = _assetDeps[id]!.valueAt(phase: phase);
381378
for (final dep in assetDeps.deps) {
382-
final depCycle = cycleById[dep]!;
379+
final depCycle = lookupLibraryCycle(dep);
383380
if (identical(depCycle, root)) continue;
384381
if (alreadyAddedChildren.add(depCycle)) {
385382
final childGraph = _graphs[dep]!.expiringValueAt(phase: phase);

0 commit comments

Comments
 (0)