Skip to content

Commit 046cd07

Browse files
committed
Unnecessary and wasteful uses of std::map removed (performance optimization)
1 parent 029cb0f commit 046cd07

File tree

6 files changed

+29
-29
lines changed

6 files changed

+29
-29
lines changed

file-commander-core/src/cpanel.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ using FileSystemWatcher = CFileSystemWatcherWindows;
1616
using FileSystemWatcher = CFileSystemWatcherTimerBased;
1717
#endif
1818

19+
#include <QHash>
20+
1921
#include <mutex>
2022
#include <vector>
2123
#include <utility>
@@ -124,12 +126,20 @@ class CPanel final : public QObject
124126
_uiThreadQueue.enqueue(std::forward<Functor>(f), tag);
125127
}
126128

129+
private:
130+
struct QStringHash {
131+
using is_avalanching = void;
132+
[[nodiscard]] inline size_t operator()(const QString& s) const noexcept {
133+
return qHash(s);
134+
}
135+
};
136+
127137
private:
128138
CFileSystemObject _currentDirObject;
129139
FileSystemWatcher _watcher;
130140
FileListHashMap _items;
131141
CHistoryList<QString> _history;
132-
std::map<QString, qulonglong /*hash*/> _cursorPosForFolder;
142+
ankerl::unordered_dense::map<QString, qulonglong /*hash*/, QStringHash> _cursorPosForFolder;
133143
CallbackCaller<PanelContentsChangedListener> _panelContentsChangedListeners;
134144
CallbackCaller<CursorPositionListener> _currentItemChangeListener;
135145
const Panel _panelPosition;

file-commander-core/src/detail/file_list_hashmap.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
struct NullHash {
88
using is_avalanching = void;
99

10-
qulonglong operator()(qulonglong hashValue) const noexcept {
10+
[[nodiscard]] inline constexpr qulonglong operator()(qulonglong hashValue) const noexcept {
1111
return hashValue;
1212
}
1313
};

file-commander-core/src/fileoperations/coperationperformer.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -583,9 +583,9 @@ std::vector<QDir> COperationPerformer::enumerateSourcesAndCalcDest(uint64_t &tot
583583

584584
UserResponse COperationPerformer::getUserResponse(HaltReason hr, const CFileSystemObject& src, const CFileSystemObject& dst, const QString& message)
585585
{
586-
auto globalResponse = _globalResponses.find(hr);
587-
if (globalResponse != _globalResponses.end())
588-
return globalResponse->second;
586+
const auto globalResponse = _globalResponses[hr];
587+
if (globalResponse)
588+
return globalResponse.value();
589589

590590
if (_observer) _observer->onProcessHaltedCallback(hr, src, dst, message);
591591
waitForResponse();

file-commander-core/src/fileoperations/coperationperformer.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
#include "cfilesystemobject.h"
55
#include "system/ctimeelapsed.h"
66

7+
#include "3rdparty/magic_enum/magic_enum.hpp"
8+
9+
#include <array>
710
#include <atomic>
811
#include <condition_variable>
912
#include <functional>
1013
#include <mutex>
14+
#include <optional>
1115
#include <thread>
1216
#include <vector>
1317

@@ -104,7 +108,8 @@ class COperationPerformer
104108

105109
private:
106110
std::vector<ObjectToProcess> _source;
107-
std::map<HaltReason, UserResponse> _globalResponses;
111+
// Essentially a map<HaltReason, UserResponse>
112+
std::array<std::optional<UserResponse>, magic_enum::enum_count<HaltReason>()> _globalResponses;
108113
CFileSystemObject _destFileSystemObject;
109114
QString _newName;
110115
Operation _op;

file-commander-core/src/plugininterface/cpluginproxy.cpp

+6-22
Original file line numberDiff line numberDiff line change
@@ -65,46 +65,30 @@ PanelPosition CPluginProxy::otherPanel() const
6565
PanelState& CPluginProxy::panelState(const PanelPosition panel)
6666
{
6767
static PanelState empty;
68-
if (panel == PluginUnknownPanel)
68+
if (panel == PluginUnknownPanel) [[unlikely]]
6969
{
7070
assert_unconditional_r("Unknown panel");
7171
empty = PanelState();
7272
return empty;
7373
}
7474

75-
const auto state = _panelState.find(panel);
76-
if (state == _panelState.end())
77-
{
78-
assert_unconditional_r("Unknown panel");
79-
empty = PanelState();
80-
return empty;
81-
}
82-
else
83-
return state->second;
75+
return _panelState[panel];
8476
}
8577

8678
const PanelState & CPluginProxy::panelState(const PanelPosition panel) const
8779
{
88-
static const PanelState empty {};
89-
if (panel == PluginUnknownPanel)
90-
{
80+
static const PanelState empty;
81+
if (panel == PluginUnknownPanel) [[unlikely]]
9182
return empty;
92-
}
9383

94-
const auto state = _panelState.find(panel);
95-
assert_and_return_r(state != _panelState.end(), empty);
96-
97-
return state->second;
84+
return _panelState[panel];
9885
}
9986

10087
QString CPluginProxy::currentFolderPathForPanel(const PanelPosition panel) const
10188
{
10289
assert_and_return_r(panel != PluginUnknownPanel, QString());
10390

104-
const auto state = _panelState.find(panel);
105-
assert_and_return_r(state != _panelState.end(), QString());
106-
107-
return state->second.currentFolder;
91+
return _panelState[panel].currentFolder;
10892
}
10993

11094
QString CPluginProxy::currentItemPathForPanel(const PanelPosition panel) const

file-commander-core/src/plugininterface/cpluginproxy.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ DISABLE_COMPILER_WARNINGS
66
#include <QIcon>
77
RESTORE_COMPILER_WARNINGS
88

9+
#include <array>
910
#include <functional>
1011
#include <vector>
1112

@@ -68,7 +69,7 @@ class CPluginProxy
6869

6970
private:
7071
CreateToolMenuEntryImplementationType _createToolMenuEntryImplementation;
71-
std::map<PanelPosition, PanelState> _panelState;
72+
std::array<PanelState, 2> _panelState;
7273
std::function<void(std::function<void()>)> _execOnUiThreadImplementation;
7374
PanelPosition _currentPanel = PluginUnknownPanel;
7475
};

0 commit comments

Comments
 (0)