From eb976387432ff558a03f95db8738b7b1bfa2aa27 Mon Sep 17 00:00:00 2001 From: Johannes Schmidt Date: Thu, 12 Mar 2026 14:10:07 +0100 Subject: [PATCH 1/2] Make retrieval of the vector size more generic This also enables the use of the function with "containers" that don't have a size function but implement an overload for `std::size()`, like c-arrays with fixed sizes. --- lib/base/workqueue.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/base/workqueue.hpp b/lib/base/workqueue.hpp index 8199ec613..a6cfb7216 100644 --- a/lib/base/workqueue.hpp +++ b/lib/base/workqueue.hpp @@ -75,10 +75,10 @@ public: template void ParallelFor(const VectorType& items, bool preChunk, const FuncType& func) { - using SizeType = decltype(items.size()); - SizeType totalCount = items.size(); - SizeType chunks = preChunk ? m_ThreadCount : totalCount; + const auto totalCount = std::size(items); + using SizeType = std::remove_const_t; + const auto chunks = preChunk ? m_ThreadCount : totalCount; auto lock = AcquireLock(); @@ -103,7 +103,7 @@ public: offset += count; } - ASSERT(offset == items.size()); + ASSERT(offset == totalCount); } bool IsWorkerThread() const; From 907f41bae46b5d2a48bf7cf31addbc0ac71a61c3 Mon Sep 17 00:00:00 2001 From: Johannes Schmidt Date: Thu, 12 Mar 2026 14:12:00 +0100 Subject: [PATCH 2/2] Use perfect-forwarding references to prevent passing temporaries Without this change, it is easy to pass a temporary, for example a vector returned by a function to ParallelFor(). With this commit a type-constraint is added to disable the use of this function for rvalue sequences. An alternative would have been to capture rvalue-references in the function, but that would have made the function unnecessarily complex, involving tuple-capture and `shared_ptr`s, while it is usually easy to control lifetime at the call-site. --- lib/base/workqueue.hpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/base/workqueue.hpp b/lib/base/workqueue.hpp index a6cfb7216..fc3711555 100644 --- a/lib/base/workqueue.hpp +++ b/lib/base/workqueue.hpp @@ -67,15 +67,14 @@ public: void Join(bool stop = false); template - void ParallelFor(const VectorType& items, const FuncType& func) + void ParallelFor(VectorType&& items, const FuncType& func) { - ParallelFor(items, true, func); + ParallelFor(std::forward(items), true, func); } - template - void ParallelFor(const VectorType& items, bool preChunk, const FuncType& func) + template>> + void ParallelFor(VectorType&& items, bool preChunk, const FuncType& func) { - const auto totalCount = std::size(items); using SizeType = std::remove_const_t; const auto chunks = preChunk ? m_ThreadCount : totalCount;