I want to load the content of thousands of files with different sizes (1KB ~ 50MB) on Windows.
Currently, I am using the mapping library from here, but it is still too slow, even with the std::async solution.
void RegisterArchives(const std::vector<std::string_view>& archives)
{
std::vector<std::future<bool>> archives_vector;
archives_future.reserve(archives.size());
for (auto& i : archives)
archives_future.emplace_back(std::async(RegisterArchive, i));
for (auto& i : archives_future)
i.get();
}
bool RegisterArchive(const std::string_view archive)
{
for (auto& i : std::filesystem::recursive_directory_iterator(archive))
{
if (i.is_directory())
continue;
mapped_file map(i.path().string().c_str());
}
return true;
}
My question is, how can I map these files as quickly as possible?
I tried without std::async and tried other loading methods like FILE*, std::ifstream etc.
If what you need is to reduce file-system API calls for all redundant read-only file-read operations, you should use a cache.
Here is my multi-level cache implementation (direct mapped + lru) for requesting data while cache-miss is handled automatically:
Test code:
output:
This is only for 4 bytes per request. With any file of 1kB+, you'd get the performance of your CPU's cache or RAM.
Implementation files (header-only but enable optimization flags, avx512 instruction set, C++14, etc):
CacheThreader.h:
DirectMappedCache:
LruClockCache.h:
CpuBenchmarker.h:
If you need read+write coherence in multithreaded use, you can look at implementation here:
https://github.com/tugrul512bit/LruClockCache/blob/main/AsyncCache.h
If you are using just a single thread, then it's better to use non-thread-safe version like this:
it's also automatically coherent when you set/write something through the cache.