Skip to content

Commit

Permalink
feat: make ConnectedSystems cache much faster to build
Browse files Browse the repository at this point in the history
  • Loading branch information
eseidel committed Oct 7, 2023
1 parent 4eb486c commit e42dd96
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
4 changes: 3 additions & 1 deletion packages/cli/bin/systems_reachable_from_factions.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:cli/cache/caches.dart';
import 'package:cli/cli.dart';
import 'package:db/db.dart';
import 'package:types/types.dart';
import 'package:types/api.dart';

Future<void> command(FileSystem fs, ArgResults argResults) async {
final db = await defaultDatabase();
Expand All @@ -10,11 +10,13 @@ Future<void> command(FileSystem fs, ArgResults argResults) async {
final factions = await loadFactions(db, factionsApi);

final clusterCache = SystemConnectivity.fromSystemsCache(systemsCache);

for (final faction in factions) {
final hq = faction.headquartersSymbol;
final reachable = clusterCache.connectedSystemCount(hq.systemSymbol);
logger.info('${faction.symbol}: $reachable');
}

// Required or main will hang.
await db.close();
}
Expand Down
13 changes: 10 additions & 3 deletions packages/cli/lib/nav/system_connectivity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ class _ClusterFinder {
if (_clusterForSystem.containsKey(startSystemSymbol)) {
return;
}
final queue = [startSystemSymbol];
final queue = {startSystemSymbol};
final cluster = _nextClusterId++;

while (queue.isNotEmpty) {
final systemSymbol = queue.removeAt(0);
final systemSymbol = queue.first;
queue.remove(systemSymbol);
final maybeCluster = _clusterForSystem[systemSymbol];
if (maybeCluster != null) {
if (maybeCluster != cluster) {
Expand All @@ -39,8 +40,14 @@ class _ClusterFinder {
continue;
}
_clusterForSystem[systemSymbol] = cluster;
// The first time we run this, it builds the connectedSystems
// cache on demand, which takes about 2s on my machine.
final connected = systemsCache.connectedSystems(systemSymbol);
queue.addAll(connected.map((s) => s.systemSymbol));
for (final symbol in connected.map((s) => s.systemSymbol)) {
if (!_clusterForSystem.containsKey(symbol)) {
queue.add(symbol);
}
}
}
}
}
Expand Down

0 comments on commit e42dd96

Please sign in to comment.