Skip to content
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

perf: reduce copying in enforceSwarmPeerLimit() #5731

Merged
merged 2 commits into from Jul 5, 2023

Conversation

tearfur
Copy link
Member

@tearfur tearfur commented Jul 5, 2023

A small improvement that should help with #5714.

Copy link
Member

@ckerr ckerr left a comment

Choose a reason for hiding this comment

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

Nice! TIL about std::partial_sort_copy()

@ckerr ckerr added scope:core type:perf A code change that improves performance notes:none Should not be listed in release notes labels Jul 5, 2023
@ckerr ckerr added this to the 4.1.0 milestone Jul 5, 2023
@tearfur
Copy link
Member Author

tearfur commented Jul 5, 2023

I am actually quite looking forward to C++20 so I can just join views of the peers vectors of each torrent and make one single pass of std::partial_sort_copy() in enforceSessionPeerLimit() as well.

Edit: Even more so in here, C++20 views is probably gonna help quite a bit.

for (auto& [socket_address, atom] : swarm->pool)
{
if (isPeerCandidate(tor, atom, now))
{
candidates.push_back({ getPeerCandidateScore(tor, atom, salter()), tor, &atom });
}
}
}
// only keep the best `max` candidates
if (std::size(candidates) > max)
{
std::partial_sort(
std::begin(candidates),
std::begin(candidates) + max,
std::end(candidates),
[](auto const& a, auto const& b) { return a.score < b.score; });
candidates.resize(max);
}
return candidates;

@ckerr ckerr merged commit 6482d78 into transmission:main Jul 5, 2023
21 checks passed
@tearfur tearfur deleted the perf/peer-limit branch July 6, 2023 01:41
ckerr pushed a commit that referenced this pull request Jul 25, 2023
* reduce copying in `enforceSwarmPeerLimit()`

* clearer comparator naming
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
notes:none Should not be listed in release notes scope:core type:perf A code change that improves performance
Development

Successfully merging this pull request may close these issues.

None yet

2 participants