Skip to content

Commit 8f85e8b

Browse files
Merge pull request #10756 from Icinga/improve-parallel-for
Improve `WorkQueue::ParallelFor()`
2 parents 2d172fb + 907f41b commit 8f85e8b

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

lib/base/workqueue.hpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,17 @@ class WorkQueue
6767
void Join(bool stop = false);
6868

6969
template<typename VectorType, typename FuncType>
70-
void ParallelFor(const VectorType& items, const FuncType& func)
70+
void ParallelFor(VectorType&& items, const FuncType& func)
7171
{
72-
ParallelFor(items, true, func);
72+
ParallelFor(std::forward<VectorType>(items), true, func);
7373
}
7474

75-
template<typename VectorType, typename FuncType>
76-
void ParallelFor(const VectorType& items, bool preChunk, const FuncType& func)
75+
template<typename VectorType, typename FuncType, typename = std::enable_if_t<!std::is_rvalue_reference_v<VectorType&&>>>
76+
void ParallelFor(VectorType&& items, bool preChunk, const FuncType& func)
7777
{
78-
using SizeType = decltype(items.size());
79-
80-
SizeType totalCount = items.size();
81-
SizeType chunks = preChunk ? m_ThreadCount : totalCount;
78+
const auto totalCount = std::size(items);
79+
using SizeType = std::remove_const_t<decltype(totalCount)>;
80+
const auto chunks = preChunk ? m_ThreadCount : totalCount;
8281

8382
auto lock = AcquireLock();
8483

@@ -103,7 +102,7 @@ class WorkQueue
103102
offset += count;
104103
}
105104

106-
ASSERT(offset == items.size());
105+
ASSERT(offset == totalCount);
107106
}
108107

109108
bool IsWorkerThread() const;

0 commit comments

Comments
 (0)