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

Discard empty command histories before discarding LRU non-empty ones #13869

Merged
2 commits merged into from
Aug 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/host/history.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ CommandHistory* CommandHistory::s_Allocate(const std::wstring_view appName, cons
{
if (WI_IsFlagClear(it->Flags, CLE_ALLOCATED))
{
// use LRU history buffer with same app name
// use MRU history buffer with same app name
if (it->IsAppNameMatch(appName))
{
BestCandidate = *it;
Expand All @@ -367,18 +367,28 @@ CommandHistory* CommandHistory::s_Allocate(const std::wstring_view appName, cons
History._processHandle = processHandle;
return &s_historyLists.emplace_front(History);
}
else if (!BestCandidate.has_value() && s_historyLists.size() > 0)

// If we have no candidate already and we need one,
// take the LRU (which is the back/last one) which isn't allocated
// and if possible the one with empty commands list.
if (!BestCandidate.has_value())
{
// If we have no candidate already and we need one, take the LRU (which is the back/last one) which isn't allocated.
for (auto it = s_historyLists.crbegin(); it != s_historyLists.crend(); it++)
auto BestCandidateIt = s_historyLists.cend();
for (auto it = s_historyLists.cbegin(); it != s_historyLists.cend(); it++)
{
if (WI_IsFlagClear(it->Flags, CLE_ALLOCATED))
{
BestCandidate = *it;
s_historyLists.erase(std::next(it).base()); // trickery to turn reverse iterator into forward iterator for erase.
break;
if (it->_commands.empty() || BestCandidateIt == s_historyLists.cend() || !BestCandidateIt->_commands.empty())
{
BestCandidateIt = it;
}
}
}
if (BestCandidateIt != s_historyLists.cend())
{
BestCandidate = *BestCandidateIt;
s_historyLists.erase(BestCandidateIt);
}
}

// If the app name doesn't match, copy in the new app name and free the old commands.
Expand Down