From e42dd963c0322b7b2fe5e64af2f99dcdc9fe7e3c Mon Sep 17 00:00:00 2001 From: Eric Seidel Date: Sat, 7 Oct 2023 10:45:36 -0700 Subject: [PATCH] feat: make ConnectedSystems cache much faster to build --- .../cli/bin/systems_reachable_from_factions.dart | 4 +++- packages/cli/lib/nav/system_connectivity.dart | 13 ++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/cli/bin/systems_reachable_from_factions.dart b/packages/cli/bin/systems_reachable_from_factions.dart index 0bc88bde..9a552607 100644 --- a/packages/cli/bin/systems_reachable_from_factions.dart +++ b/packages/cli/bin/systems_reachable_from_factions.dart @@ -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 command(FileSystem fs, ArgResults argResults) async { final db = await defaultDatabase(); @@ -10,11 +10,13 @@ Future 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(); } diff --git a/packages/cli/lib/nav/system_connectivity.dart b/packages/cli/lib/nav/system_connectivity.dart index 49039dc5..64aee873 100644 --- a/packages/cli/lib/nav/system_connectivity.dart +++ b/packages/cli/lib/nav/system_connectivity.dart @@ -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) { @@ -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); + } + } } } }