From e01b1028cc2fcf844d94088e8553597d15dbc9ff Mon Sep 17 00:00:00 2001 From: zarzet <42882290+zarzet@users.noreply.github.com> Date: Sat, 21 Nov 2026 21:59:55 +0700 Subject: [PATCH] perf(history): find downloads by id and by path through an index Resolving a finished download and looking a file up by its path walked the whole history and built the path keys of every entry each time. The state now builds an id index and a path key index on first use, and carries both over when a download is added. --- lib/models/download_history.dart | 57 +++++++++++++++++++- lib/providers/download_history_provider.dart | 19 ++----- 2 files changed, 59 insertions(+), 17 deletions(-) diff --git a/lib/models/download_history.dart b/lib/models/download_history.dart index 7bbba9ab3..98ca77ea6 100644 --- a/lib/models/download_history.dart +++ b/lib/models/download_history.dart @@ -1,3 +1,4 @@ +import 'package:mornye/utils/path_match_keys.dart'; import 'package:mornye/utils/string_utils.dart'; /// Another transfer published the same origin while this one was fetching. @@ -297,6 +298,8 @@ class DownloadHistoryState { final Map _bySpotifyId; final Map _byIsrc; final Map _byTrackArtistKey; + Map? _byId; + Map? _byPathKey; DownloadHistoryState({ this.items = const [], @@ -325,7 +328,9 @@ class DownloadHistoryState { }) : _lookupItems = source._lookupItems, _bySpotifyId = source._bySpotifyId, _byIsrc = source._byIsrc, - _byTrackArtistKey = source._byTrackArtistKey; + _byTrackArtistKey = source._byTrackArtistKey, + _byId = source._byId, + _byPathKey = source._byPathKey; DownloadHistoryState._withPrependedLookup({ required DownloadHistoryState source, @@ -344,7 +349,55 @@ class DownloadHistoryState { source._byTrackArtistKey, trackArtistKey(item.trackName, item.artistName), item, - ); + ), + _byId = source._byId == null + ? null + : _indexWithNewest(source._byId!, item.id, item), + _byPathKey = source._byPathKey == null + ? null + : _indexPathKeysWithNewest(source._byPathKey!, item); + + static Map _indexPathKeysWithNewest( + Map index, + DownloadHistoryItem item, + ) { + final keys = buildPathMatchKeys(item.filePath); + if (keys.isEmpty) return index; + final next = Map.of(index); + for (final key in keys) { + next[key] = item; + } + return next; + } + + DownloadHistoryItem? getById(String id) { + if (id.isEmpty) return null; + final byId = _byId ??= _indexRecentHistoryItems( + _lookupItems, + (item) => item.id, + ); + return byId[id]; + } + + DownloadHistoryItem? getByFilePath(String filePath) { + final targetKeys = buildPathMatchKeys(filePath); + if (targetKeys.isEmpty) return null; + var byPath = _byPathKey; + if (byPath == null) { + byPath = {}; + for (final item in _lookupItems) { + for (final key in buildPathMatchKeys(item.filePath)) { + byPath.putIfAbsent(key, () => item); + } + } + _byPathKey = byPath; + } + for (final key in targetKeys) { + final hit = byPath[key]; + if (hit != null) return hit; + } + return null; + } /// The key [findByTrackArtistKey] reads; '' for an empty track name. static String trackArtistKey(String trackName, String artistName) { diff --git a/lib/providers/download_history_provider.dart b/lib/providers/download_history_provider.dart index 72fdcc29c..c7ac917be 100644 --- a/lib/providers/download_history_provider.dart +++ b/lib/providers/download_history_provider.dart @@ -291,13 +291,8 @@ class DownloadHistoryNotifier extends Notifier { Future<({DownloadHistoryItem item, String? existingId})> _resolveHistoryItem( DownloadHistoryItem item, ) async { - DownloadHistoryItem? existing; - for (final candidate in state.lookupItems) { - if (historyItemsReferToSameStoredFile(candidate, item)) { - existing = candidate; - break; - } - } + DownloadHistoryItem? existing = + state.getById(item.id) ?? state.getByFilePath(item.filePath); if (existing == null) { final json = await _db.getById(item.id); @@ -629,14 +624,8 @@ class DownloadHistoryNotifier extends Notifier { } Future getByFilePathAsync(String filePath) async { - final targetKeys = buildPathMatchKeys(filePath); - if (targetKeys.isEmpty) return null; - - for (final item in state.lookupItems) { - if (buildPathMatchKeys(item.filePath).any(targetKeys.contains)) { - return item; - } - } + final inMemory = state.getByFilePath(filePath); + if (inMemory != null) return inMemory; final json = await _db.findByFilePath(filePath); if (json == null) return null;