Use std::list and fixed step size.

This commit is contained in:
ZehMatt 2018-04-24 19:06:32 +02:00 committed by Aaron van Geffen
parent b9e0eede44
commit 7be1cd3e37
1 changed files with 7 additions and 10 deletions

View File

@ -20,6 +20,7 @@
#include <string> #include <string>
#include <tuple> #include <tuple>
#include <vector> #include <vector>
#include <list>
#include "../common.h" #include "../common.h"
#include "Console.hpp" #include "Console.hpp"
#include "File.h" #include "File.h"
@ -192,7 +193,7 @@ private:
if (_log_levels[DIAGNOSTIC_LEVEL_VERBOSE]) if (_log_levels[DIAGNOSTIC_LEVEL_VERBOSE])
{ {
std::unique_lock<std::mutex> lock(printLock); std::lock_guard<std::mutex> lock(printLock);
log_verbose("FileIndex:Indexing '%s'", filePath.c_str()); log_verbose("FileIndex:Indexing '%s'", filePath.c_str());
} }
@ -217,21 +218,17 @@ private:
JobPool jobPool; JobPool jobPool;
std::mutex printLock; // For verbose prints. std::mutex printLock; // For verbose prints.
std::vector<std::vector<TItem>> containers; std::list<std::vector<TItem>> containers;
size_t stepSize = std::thread::hardware_concurrency(); size_t stepSize = 100; // Handpicked, seems to work well with 4/8 cores.
size_t numTasks = totalCount / stepSize;
size_t taskGroup = 0;
numTasks += (totalCount % stepSize == 0 ? 0 : 1);
containers.resize(numTasks); for (size_t rangeStart = 0; rangeStart < totalCount; rangeStart += stepSize)
for (size_t rangeStart = 0; rangeStart < totalCount; rangeStart += stepSize, taskGroup++)
{ {
if (rangeStart + stepSize > totalCount) if (rangeStart + stepSize > totalCount)
stepSize = totalCount - rangeStart; stepSize = totalCount - rangeStart;
auto& items = containers[taskGroup]; containers.emplace_back();
auto& items = containers.back();
jobPool.addTask(std::bind(&FileIndex<TItem>::BuildRange, jobPool.addTask(std::bind(&FileIndex<TItem>::BuildRange,
this, this,