Skip to content

Commit 8a6f44d

Browse files
committed
Extract DivideUp lambda
1 parent 17655b7 commit 8a6f44d

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

lib/Driver/Compilation.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,18 +1361,24 @@ namespace driver {
13611361
// subprocesses than before. And significantly: it's doing so while
13621362
// not exceeding the RAM of a typical 2-core laptop.
13631363

1364+
// An explanation of why the partition calculation isn't integer division.
1365+
// Using an example, a module of 26 files exceeds the limit of 25 and must
1366+
// be compiled in 2 batches. Integer division yields 26/25 = 1 batch, but
1367+
// a single batch of 26 exceeds the limit. The calculation must round up,
1368+
// which can be calculated using: `(x + y - 1) / y`
1369+
//
1370+
// Two key properties of this calculation are:
1371+
// DivideUp(M*N, N) = M
1372+
// DivideUp(M*N + 1, N) = M + 1
1373+
auto DivideUp = [](size_t Num, size_t Div) -> size_t {
1374+
return (Num + Div - 1) / Div;
1375+
};
1376+
13641377
size_t DefaultSizeLimit = 25;
13651378
size_t NumTasks = TQ->getNumberOfParallelTasks();
13661379
size_t NumFiles = PendingExecution.size();
13671380
size_t SizeLimit = Comp.getBatchSizeLimit().getValueOr(DefaultSizeLimit);
1368-
1369-
// An explanation of why the partition calculation isn't simple division.
1370-
// Using the default limit as an example, a module of 26 files must be
1371-
// compiled in 2 batches. Simple division yields 26/25 = 1 batch, but a
1372-
// single batch of 26 would exceed the limit of 25. To round up, the
1373-
// calculation is: `(x - 1) / y + 1`.
1374-
size_t NumPartitions = (NumFiles - 1) / SizeLimit + 1;
1375-
return std::max(NumTasks, NumPartitions);
1381+
return std::max(NumTasks, DivideUp(NumFiles, SizeLimit));
13761382
}
13771383

13781384
/// Select jobs that are batch-combinable from \c PendingExecution, combine

0 commit comments

Comments
 (0)