precompute maxlevel in LogMergePolicy (#1045)

This commit is contained in:
tang donghai 2022-07-26 19:42:32 +08:00 committed by GitHub
parent 2efc204a39
commit 94960a0aff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 7 deletions

View File

@ -530,18 +530,22 @@ public abstract class LogMergePolicy extends MergePolicy {
final int numMergeableSegments = levels.size();
// precompute the max level on the right side.
// arr size is numMergeableSegments + 1 to handle the case
// when numMergeableSegments is 0.
float[] maxLevels = new float[numMergeableSegments + 1];
// -1 is definitely the minimum value, because Math.log(1) is 0.
maxLevels[numMergeableSegments] = -1.0f;
for (int i = numMergeableSegments - 1; i >= 0; i--) {
maxLevels[i] = Math.max(levels.get(i).level, maxLevels[i + 1]);
}
int start = 0;
while (start < numMergeableSegments) {
// Find max level of all segments not already
// quantized.
float maxLevel = levels.get(start).level;
for (int i = 1 + start; i < numMergeableSegments; i++) {
final float level = levels.get(i).level;
if (level > maxLevel) {
maxLevel = level;
}
}
float maxLevel = maxLevels[start];
// Now search backwards for the rightmost segment that
// falls into this level: