Use Unordered_Map or Vector with conversion to Unordered_Map to fill a File->FileInfo structure for file manager

1 day ago 1
ARTICLE AD BOX

I'm writing a file manager which needs to find and show files in the current path based on the filename-based Hash Map structure

file1.txt --> FileInfo file2.txt --> FileInfo file3.txt --> FileInfo

The reason it can't be just an std::vector (e.g. a list of FileInfos) is because quick lookups are required on selecting file(s). For example upon selecting items, I need to compute the total selection size and show it in the status bar, which requires quick access to the individual FileInfo by filename. Not all columns may be visible in the UI and I can't grab those values from anywhere other than the linked FileInfo.

The simple approach is

std::unordered_map<std::wstring, FileInfo> contents;

and on exploring the path, after populating each FileInfo, I do:

contents.emplace(fileInfo.filename, fileInfo);

The performance of inserting into std::unordered_map is reputed to be bad compared to inserting into a std::vector. For performance gains, I can add to a Vector first, which is claimed to be fast:

vector.push_back(fileInfo);

and then by using reserve / std::move I can quickly populate a std::unordered_map as follows:

contents.reserve(fileInfos.size()); for (auto& fileInfo : fileInfos) { map.try_emplace(fileInfo.filename, std::move(fileInfo)); }

Does #2 make sense and will it result in real performance gains when populating the initial data structure? My goal is to show the file list as quickly as possible. Paths can contain anywhere from 0-10,000 files/folders, but on average it's 100-1000.

I don't want to use third-party libraries.

Read Entire Article