Skip to content

Commit 8d101fb

Browse files
committed
Remove count var
1 parent 20f75c1 commit 8d101fb

File tree

1 file changed

+12
-16
lines changed
  • x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/aggregation/blockhash

1 file changed

+12
-16
lines changed

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/aggregation/blockhash/LongTopNBlockHash.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ public final class LongTopNBlockHash extends BlockHash {
4141
private LongTopNUniqueSort topValues;
4242
private final LongHash seenUniqueValues;
4343
/**
44-
* Helper field to keep track of the last top value, and avoid expensive searches.
44+
* Helper field to keep track of the last top value, and avoid expensive accesses.
4545
*/
4646
private long lastTopValue;
47-
private int valuesInTop;
4847

4948
/**
5049
* Have we seen any {@code null} values?
@@ -66,7 +65,6 @@ public LongTopNBlockHash(int channel, boolean asc, boolean nullsFirst, int limit
6665
this.topValues = new LongTopNUniqueSort(blockFactory.bigArrays(), asc ? SortOrder.ASC : SortOrder.DESC, limit);
6766
this.seenUniqueValues = new LongHash(1, blockFactory.bigArrays());
6867
this.lastTopValue = asc ? Long.MAX_VALUE : Long.MIN_VALUE;
69-
this.valuesInTop = 0;
7068

7169
assert limit > 0 : "LongTopNBlockHash requires a limit greater than 0";
7270
}
@@ -107,8 +105,8 @@ private boolean acceptNull() {
107105
if (nullsFirst) {
108106
hasNull = true;
109107
migrateToSmallTop();
110-
if (valuesInTop == limit - 1) {
111-
if (valuesInTop == 0) {
108+
if (topValues.getCount() == limit - 1) {
109+
if (topValues.getCount() == 0) {
112110
lastTopValue = asc ? Long.MAX_VALUE : Long.MIN_VALUE;
113111
} else {
114112
lastTopValue = topValues.getWorstValue();
@@ -117,7 +115,7 @@ private boolean acceptNull() {
117115
return true;
118116
}
119117

120-
if (valuesInTop < limit) {
118+
if (topValues.getCount() < limit) {
121119
hasNull = true;
122120
return true;
123121
}
@@ -141,17 +139,16 @@ private boolean acceptValue(long value) {
141139

142140
topValues.collect(value);
143141

144-
valuesInTop = Math.min(valuesInTop + 1, limit - (hasNull && nullsFirst ? 1 : 0));
145-
if (valuesInTop == limit) {
142+
if (topValues.getCount() == limit) {
146143
lastTopValue = topValues.getWorstValue();
147-
} else if (valuesInTop == 1 || isBetterThan(lastTopValue, value)) {
144+
} else if (topValues.getCount() == 1 || isBetterThan(lastTopValue, value)) {
148145
lastTopValue = value;
149146
}
150147

151148
// Full top and null, there's an extra value/null we must remove
152-
if (valuesInTop == limit && hasNull) {
149+
if (topValues.getCount() == limit && hasNull) {
153150
if (nullsFirst) {
154-
if (valuesInTop == 1) {
151+
if (topValues.getCount() == 1) {
155152
lastTopValue = asc ? Long.MAX_VALUE : Long.MIN_VALUE;
156153
} else {
157154
lastTopValue = topValues.getWorstValue();
@@ -172,7 +169,6 @@ private void migrateToSmallTop() {
172169
assert topValues.getLimit() == limit : "The top values can't be migrated twice";
173170

174171
topValues.reduceLimitByOne();
175-
valuesInTop = topValues.getCount();
176172
}
177173

178174
private boolean isBetterThan(long value, long other) {
@@ -200,7 +196,7 @@ private boolean isInTop(long value) {
200196
* Returns true if there are {@code limit} values in the blockhash; false otherwise.
201197
*/
202198
private boolean isTopComplete() {
203-
return valuesInTop >= limit - (hasNull ? 1 : 0);
199+
return topValues.getCount() >= limit - (hasNull ? 1 : 0);
204200
}
205201

206202
/**
@@ -296,7 +292,7 @@ private IntBlock lookup(LongBlock block) {
296292
@Override
297293
public LongBlock[] getKeys() {
298294
if (hasNull) {
299-
final long[] keys = new long[valuesInTop + 1];
295+
final long[] keys = new long[topValues.getCount() + 1];
300296
int keysIndex = 1;
301297
for (int i = 1; i < hash.size() + 1; i++) {
302298
long value = hash.get(i - 1);
@@ -309,7 +305,7 @@ public LongBlock[] getKeys() {
309305
return new LongBlock[] {
310306
blockFactory.newLongArrayBlock(keys, keys.length, null, nulls, Block.MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING) };
311307
}
312-
final long[] keys = new long[valuesInTop];
308+
final long[] keys = new long[topValues.getCount()];
313309
int keysIndex = 0;
314310
for (int i = 0; i < hash.size(); i++) {
315311
long value = hash.get(i);
@@ -323,7 +319,7 @@ public LongBlock[] getKeys() {
323319
@Override
324320
public IntVector nonEmpty() {
325321
int nullOffset = hasNull ? 1 : 0;
326-
final int[] ids = new int[valuesInTop + nullOffset];
322+
final int[] ids = new int[topValues.getCount() + nullOffset];
327323
int idsIndex = nullOffset;
328324
// TODO: Can we instead iterate the top and take the ids from the hash? To avoid checking unused values
329325
for (int i = 1; i < hash.size() + 1; i++) {

0 commit comments

Comments
 (0)