Skip to content

Optimize load_session_torrents code path #1546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft

Conversation

jesec
Copy link

@jesec jesec commented Jul 19, 2025

Torrents / Time to Load (seconds) w/o this PR w/ this PR
100 0.0988322 0.100783
500 0.316298 0.2778484
1000 0.5230036 0.3384636
2000 1.1452586 0.488410
5000 5.4459174 0.8843604
10000 24.0999498 1.574791
15000 62.0685972 2.3090436
20000 122.2196932 3.118153
25000 194.3020272 3.9291368
30000 301.594044 4.7896626

image

jesec added 6 commits July 19, 2025 11:42
Eliminate O(n²) hash checking overhead during session loading by moving
hashing view initialization to after session torrents are loaded.

Performance Impact:
- Before: O(n²) - each session torrent triggers individual hash processing
- After: O(n) - all session torrents processed in single batch when view connects
- Critical speedup when loading thousands of session torrents at startup

Implementation leverages existing view signal system:
- Session torrents load without triggering hash checking
- Hashing view connects after loading and naturally batch-processes all torrents
- No additional state management or complexity needed

This approach is more elegant than batch mode flags as it uses the
view system's natural batching behavior when initially populated.
…ading

Bypass scheduler queuing for session torrents to eliminate priority queue
operations during bulk session loading.

Performance Impact:
- Before: O(n*log n) due to scheduler priority queue operations
- After: O(n) direct execution for session torrents
- Critical speedup when loading thousands of session torrents at startup

Leverages existing m_session flag to automatically detect session torrents
and use direct execution path while preserving async handling for regular torrents.
Eliminate expensive view sorting when running in daemon mode where
RPC clients handle their own sorting.

Performance Impact:
- Before: O(n*log n) sorting operations per view update
- After: O(1) - sorting completely disabled when not needed
- Critical benefit for RPC clients like Flood/ruTorrent

Sorting is only enabled in interactive mode where users need
visually sorted download lists.
Add fast path for view insertion that skips expensive sorted insertion
when no sorting criteria are configured.

Performance Impact:
- Before: O(n) std::find_if search for insertion position always
- After: O(1) direct insertion at end when sorting not required
- Significant speedup for unsorted views during bulk operations

Particularly benefits session loading and views without sort commands.
Add fast path for View::filter_download() that avoids expensive search
for newly added downloads.

Performance Impact:
- Before: O(n) std::find() search through entire view always
- After: O(1) check for newly added downloads at end position first
- Reduces session loading from O(n²) to O(n) for filter operations

Newly added downloads are always at vector end, making this
optimization highly effective during bulk session loading.
…n torrents

Skip expensive download list verification check during session loading
since session torrents are trusted and cannot be removed immediately.

Performance Impact:
- Before: O(n) download list search per session torrent
- After: O(1) - verification skipped for session torrents only
- Reduces session loading verification from O(n²) to O(n)

Session torrents are pre-validated and trusted, making this
verification redundant and expensive.
@neheb
Copy link
Contributor

neheb commented Jul 22, 2025

impressive.

rpc::call_command("d.state.set", (int64_t)m_start, rpc::make_target(download));
if (!m_session) {
if (m_manager->download_list()->find(infohash) == m_manager->download_list()->end())
throw torrent::input_error("The newly created download was removed.");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How big of an effect does this change have?

As session torrents are loaded after Control is initialized, we can still delete torrents during session load using xmlrpc. (or other event triggers)

"schedule2 = view.main,10,10,((view.sort,main,20))\n"
"schedule2 = view.name,10,10,((view.sort,name,20))\n"
);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Daemon mode still uses views, so the name view should be sorted.

However this could be changed to be added on event.system.startup_done, which likely should also be used to enable scgi if opened.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants