Skip to content

Commit

Permalink
Remove quadratic complexity from panel updates
Browse files Browse the repository at this point in the history
  • Loading branch information
alabuzhev committed Nov 24, 2024
1 parent 11710eb commit ffc3218
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
38 changes: 20 additions & 18 deletions plugins/tmppanel/TmpClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,11 +366,6 @@ void TmpPanel::RemoveEmptyItems()
m_Panel->Items.resize(m_Panel->Items.size() - EmptyCount);
}

static bool same_name(const WIN32_FIND_DATA& wfd, const PluginPanelItem& ffd)
{
return !lstrcmp(wfd.cFileName, FSF.PointToName(ffd.FileName));
}

void TmpPanel::UpdateItems(const bool ShowOwners, const bool ShowLinks)
{
if (m_UpdateNotNeeded || m_Panel->Items.empty())
Expand All @@ -385,13 +380,15 @@ void TmpPanel::UpdateItems(const bool ShowOwners, const bool ShowLinks)
m_LastOwnersRead = ShowOwners;
m_LastLinksRead = ShowLinks;

std::unordered_map<string_view, PluginPanelItem*> SameFolderItems;

auto NameDataIterator = m_Panel->StringData.begin();
for (auto CurItem = m_Panel->Items.begin(), end = m_Panel->Items.end(); CurItem != end; ++CurItem, ++NameDataIterator)
{
const string_view FullName(CurItem->FileName);
const auto SlashPos = FullName.rfind(L'\\');
const auto Dir = FullName.substr(0, SlashPos == FullName.npos? 0 : SlashPos + 1);
size_t SameFolderItems = 1;
size_t SameFolderItemsNumber = 1;

/* $ 23.12.2001 DJ
если FullName - это каталог, то FindFirstFile (FullName+"*.*")
Expand All @@ -404,7 +401,7 @@ void TmpPanel::UpdateItems(const bool ShowOwners, const bool ShowLinks)
{
const string_view NextName = Next->FileName;
if (NextName.starts_with(Dir) && !contains(NextName.substr(Dir.size()), L'\\'))
SameFolderItems++;
SameFolderItemsNumber++;
else
break;
}
Expand All @@ -414,36 +411,41 @@ void TmpPanel::UpdateItems(const bool ShowOwners, const bool ShowLinks)
// несколько файлов из одного и того же каталога. При этом
// FindFirstFile() делается один раз на каталог, а не отдельно для
// каждого файла.
if (SameFolderItems > 2)
if (SameFolderItemsNumber > 2)
{
WIN32_FIND_DATA FindData;
const auto FindFile = Dir + L"*"sv;
const auto NtPath = FormNtPath(FindFile);

for (auto& j: std::ranges::subrange(CurItem, CurItem + SameFolderItems))
SameFolderItems.clear();
SameFolderItems.reserve(SameFolderItemsNumber);

for (auto& j: std::ranges::subrange(CurItem, CurItem + SameFolderItemsNumber))
{
j.Flags |= REMOVE_FLAG;
SameFolderItems.try_emplace(FSF.PointToName(j.FileName), &j);
}

if (const auto FindHandle = FindFirstFile(NtPath.c_str(), &FindData); FindHandle != INVALID_HANDLE_VALUE)
{
SCOPE_EXIT{ FindClose(FindHandle); };

do
{
for (auto& j: std::ranges::subrange(CurItem, CurItem + SameFolderItems))
if (const auto& SameFolderItem = SameFolderItems.find(FindData.cFileName); SameFolderItem != SameFolderItems.end())
{
if ((j.Flags & REMOVE_FLAG) && same_name(FindData, j))
{
j.Flags &= ~REMOVE_FLAG;
WFD2FFD(FindData, j, {});
break;
}
SameFolderItem->second->Flags &= ~REMOVE_FLAG;
WFD2FFD(FindData, *SameFolderItem->second, {});
SameFolderItems.erase(SameFolderItem);
}
}
while (FindNextFile(FindHandle, &FindData));
}

CurItem += SameFolderItems - 1;
std::advance(NameDataIterator, SameFolderItems - 1);
SameFolderItems.clear();

CurItem += SameFolderItemsNumber - 1;
std::advance(NameDataIterator, SameFolderItemsNumber - 1);
}
else
{
Expand Down
4 changes: 4 additions & 0 deletions plugins/tmppanel/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
drkns 2024-11-24 11:48:49+00:00 - build 121

1. Remove quadratic complexity from panel updates.

drkns 2023-02-14 21:56:31+00:00 - build 120

1. A few fixes.
Expand Down
2 changes: 1 addition & 1 deletion plugins/tmppanel/version.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <farversion.hpp>

#define PLUGIN_BUILD 120
#define PLUGIN_BUILD 121
#define PLUGIN_DESC L"Temporary Panel for Far Manager"
#define PLUGIN_NAME L"TmpPanel"
#define PLUGIN_FILENAME L"TmpPanel.dll"
Expand Down

0 comments on commit ffc3218

Please sign in to comment.