Skip to content

Commit

Permalink
Improve batching code (#1079)
Browse files Browse the repository at this point in the history
Fixes #1077
  • Loading branch information
godexsoft authored Jan 5, 2024
1 parent e89640b commit e26a1e3
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/util/Batching.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
#include "util/Assert.h"

#include <algorithm>
#include <cstdint>
#include <functional>
#include <iterator>
#include <ranges>

namespace util {
Expand All @@ -32,13 +33,19 @@ forEachBatch(std::ranges::forward_range auto&& container, std::size_t batchSize,
{
ASSERT(batchSize > 0, "Batch size must be greater than 0");

auto const totalSize = container.size();
auto const batches = totalSize / batchSize + (totalSize % batchSize ? 1 : 0);
auto to = std::begin(container);
auto end = std::end(container);

for (auto i = 0u; i < batches; ++i) {
auto const start = i * batchSize;
auto const end = std::min(start + batchSize, totalSize);
fn(container.begin() + start, container.begin() + end);
while (to != end) {
auto from = to;

auto cnt = batchSize;
while (to != end and cnt > 0) {
++to;
--cnt;
}

std::invoke(fn, from, to);
}
}

Expand Down

0 comments on commit e26a1e3

Please sign in to comment.