Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve positionsArrayToSelectedPositions #8624

Merged
merged 1 commit into from
Jul 21, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ static SelectedPositions positionsArrayToSelectedPositions(boolean[] selectedPos
int selectedCount = 0;
for (int i = 0; i < size; i++) {
boolean selectedPosition = selectedPositions[i];
if (selectedPosition) {
selectedCount++;
}
// Avoid branching by casting boolean to integer.
// This improves CPU utilization by avoiding branch mispredictions.
selectedCount += selectedPosition ? 1 : 0;
sopel39 marked this conversation as resolved.
Show resolved Hide resolved
}

if (selectedCount == 0 || selectedCount == size) {
Expand All @@ -40,11 +40,11 @@ static SelectedPositions positionsArrayToSelectedPositions(boolean[] selectedPos

int[] positions = new int[selectedCount];
int index = 0;
for (int position = 0; position < size; position++) {
if (selectedPositions[position]) {
positions[index] = position;
index++;
}
for (int position = 0; index < selectedCount; position++) {
// Improve CPU utilization by avoiding setting of position conditionally.
// This improves CPU utilization by avoiding branch mispredictions.
positions[index] = position;
sopel39 marked this conversation as resolved.
Show resolved Hide resolved
index += selectedPositions[position] ? 1 : 0;
}
return SelectedPositions.positionsList(positions, 0, selectedCount);
}
Expand Down