Skip to content

Commit

Permalink
[7.5.0] Use long instead of int for thread indices (#24866)
Browse files Browse the repository at this point in the history
This protects against an integer overflow which could occur for large
key list size and large thread counts.

Regrettably, it's difficult to write a regression test for this
scenario, as exercising this overflow requires lots of time and heap, so
it would be a performance regression to our test suites.

Fixes #19445

PiperOrigin-RevId: 595420516
Change-Id: Ic0a475a6a273c50fe9895dd0852fa5b062859cb2

Commit
3e373d0

Co-authored-by: Googler <[email protected]>
  • Loading branch information
bazel-io and c-parsons authored Jan 9, 2025
1 parent 74d3d1b commit 5d7f1b3
Showing 1 changed file with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,18 +274,18 @@ protected void runInternal(ImmutableList<Pair<SkyKey, InvalidationType>> pending
MIN_TIME_FOR_LOGGING)) {
// To avoid contention and scheduling too many jobs for our #cpus, we start
// DEFAULT_THREAD_COUNT jobs, each processing a chunk of the pending visitations.
int listSize = pendingList.size();
int numThreads = min(DEFAULT_THREAD_COUNT, listSize);
for (int i = 0; i < numThreads; i++) {
int index = i;
long listSize = pendingList.size();
long numThreads = min(DEFAULT_THREAD_COUNT, listSize);
for (long i = 0; i < numThreads; i++) {
// Use long multiplication to avoid possible overflow, as numThreads * listSize might be
// larger than max int.
int startIndex = (int) ((i * listSize) / numThreads);
int endIndex = (int) (((i + 1) * listSize) / numThreads);
executor.execute(
() ->
visit(
Collections2.transform(
pendingList.subList(
(index * listSize) / numThreads,
((index + 1) * listSize) / numThreads),
Pair::getFirst),
pendingList.subList(startIndex, endIndex), Pair::getFirst),
InvalidationType.DELETED));
}
}
Expand Down

0 comments on commit 5d7f1b3

Please sign in to comment.