From c223e3bb646883c3c506f875a26a643f6e35948a Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 29 Jan 2026 17:58:42 -0600 Subject: [PATCH] perf: more efficient Torrent.updateTorrents() (#8280) avoid unnecessary vector, manually coalesce notifications --- macosx/Torrent.mm | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/macosx/Torrent.mm b/macosx/Torrent.mm index ebcbc3b71..5551166be 100644 --- a/macosx/Torrent.mm +++ b/macosx/Torrent.mm @@ -264,9 +264,6 @@ bool trashDataFile(std::string_view const filename, tr_error* error) std::vector torrent_handles; torrent_handles.reserve(torrents.count); - std::vector was_transmitting; - was_transmitting.reserve(torrents.count); - for (Torrent* torrent in torrents) { if (torrent == nil || torrent.fHandle == nullptr) @@ -276,7 +273,6 @@ bool trashDataFile(std::string_view const filename, tr_error* error) torrent_objects.emplace_back(torrent); torrent_handles.emplace_back(torrent.fHandle); - was_transmitting.emplace_back(torrent.transmitting); } if (torrent_handles.empty()) @@ -284,23 +280,25 @@ bool trashDataFile(std::string_view const filename, tr_error* error) return; } - auto const stats = tr_torrentStat(torrent_handles.data(), torrent_handles.size()); + auto stats = tr_torrentStat(torrent_handles.data(), torrent_handles.size()); - // Assign stats and post notifications. + // Update stats + bool transmitting_changed = false; for (size_t i = 0, n = torrent_objects.size(); i < n; ++i) { Torrent* const torrent = torrent_objects[i]; - torrent.fStat = stats[i]; + auto const was_transmitting = torrent.transmitting; + torrent.fStat = std::move(stats[i]); + transmitting_changed |= was_transmitting != torrent.transmitting; + } - //make sure the "active" filter is updated when transmitting changes - if (was_transmitting[i] != torrent.transmitting) - { - //posting asynchronously with coalescing to prevent stack overflow on lots of torrents changing state at the same time - [NSNotificationQueue.defaultQueue enqueueNotification:[NSNotification notificationWithName:@"UpdateTorrentsState" object:nil] - postingStyle:NSPostASAP - coalesceMask:NSNotificationCoalescingOnName - forModes:nil]; - } + // make sure the "active" filter is updated if any `transmitting` property changed. + if (transmitting_changed) + { + [NSNotificationQueue.defaultQueue enqueueNotification:[NSNotification notificationWithName:@"UpdateTorrentsState" object:nil] + postingStyle:NSPostASAP + coalesceMask:NSNotificationCoalescingOnName + forModes:nil]; } }