Skip to content

Commit

Permalink
Reduce internal states of Block (elastic#106145)
Browse files Browse the repository at this point in the history
Currently, all Blocks inherit from AbstractBlock, resulting in every 
Block having fields such as positionCount, firstValueIndexes, and 
nullsMask, which are specific to array blocks.. This change moves these
fields to AbstractArrayBlock and removes AbstractBlock.
  • Loading branch information
dnhatn authored Mar 9, 2024
1 parent 9953b12 commit 5db2b06
Show file tree
Hide file tree
Showing 24 changed files with 261 additions and 276 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,18 @@ final class BooleanArrayBlock extends AbstractArrayBlock implements BooleanBlock
positionCount,
firstValueIndexes,
nulls,
mvOrdering,
blockFactory
mvOrdering
);
}

private BooleanArrayBlock(
BooleanArrayVector vector,
BooleanArrayVector vector, // stylecheck
int positionCount,
int[] firstValueIndexes,
BitSet nulls,
MvOrdering mvOrdering,
BlockFactory blockFactory
MvOrdering mvOrdering
) {
super(positionCount, firstValueIndexes, nulls, mvOrdering, blockFactory);
super(positionCount, firstValueIndexes, nulls, mvOrdering);
this.vector = vector;
assert firstValueIndexes == null
? vector.getPositionCount() == getPositionCount()
Expand Down Expand Up @@ -115,8 +113,7 @@ public BooleanBlock expand() {
expandedPositionCount,
null,
shiftNullsToExpandedPositions(),
MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING,
blockFactory()
MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING
);
blockFactory().adjustBreaker(expanded.ramBytesUsedOnlyBlock() - bitSetRamUsedEstimate);
// We need to incRef after adjusting any breakers, otherwise we might leak the vector if the breaker trips.
Expand Down Expand Up @@ -160,10 +157,14 @@ public String toString() {

@Override
public void allowPassingToDifferentDriver() {
super.allowPassingToDifferentDriver();
vector.allowPassingToDifferentDriver();
}

@Override
public BlockFactory blockFactory() {
return vector.blockFactory();
}

@Override
public void closeInternal() {
blockFactory().adjustBreaker(-ramBytesUsedOnlyBlock());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,18 @@ public BooleanBigArrayBlock(
positionCount,
firstValueIndexes,
nulls,
mvOrdering,
blockFactory
mvOrdering
);
}

private BooleanBigArrayBlock(
BooleanBigArrayVector vector,
BooleanBigArrayVector vector, // stylecheck
int positionCount,
int[] firstValueIndexes,
BitSet nulls,
MvOrdering mvOrdering,
BlockFactory blockFactory
MvOrdering mvOrdering
) {
super(positionCount, firstValueIndexes, nulls, mvOrdering, blockFactory);
super(positionCount, firstValueIndexes, nulls, mvOrdering);
this.vector = vector;
assert firstValueIndexes == null
? vector.getPositionCount() == getPositionCount()
Expand Down Expand Up @@ -116,8 +114,7 @@ public BooleanBlock expand() {
expandedPositionCount,
null,
shiftNullsToExpandedPositions(),
MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING,
blockFactory()
MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING
);
blockFactory().adjustBreaker(expanded.ramBytesUsedOnlyBlock() - bitSetRamUsedEstimate);
// We need to incRef after adjusting any breakers, otherwise we might leak the vector if the breaker trips.
Expand Down Expand Up @@ -161,10 +158,14 @@ public String toString() {

@Override
public void allowPassingToDifferentDriver() {
super.allowPassingToDifferentDriver();
vector.allowPassingToDifferentDriver();
}

@Override
public BlockFactory blockFactory() {
return vector.blockFactory();
}

@Override
public void closeInternal() {
blockFactory().adjustBreaker(-ramBytesUsedOnlyBlock());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public final class BooleanVectorBlock extends AbstractVectorBlock implements Boo
* @param vector considered owned by the current block; must not be used in any other {@code Block}
*/
BooleanVectorBlock(BooleanVector vector) {
super(vector.getPositionCount(), vector.blockFactory());
this.vector = vector;
}

Expand All @@ -36,7 +35,7 @@ public boolean getBoolean(int valueIndex) {
}

@Override
public int getTotalValueCount() {
public int getPositionCount() {
return vector.getPositionCount();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,18 @@ final class BytesRefArrayBlock extends AbstractArrayBlock implements BytesRefBlo
positionCount,
firstValueIndexes,
nulls,
mvOrdering,
blockFactory
mvOrdering
);
}

private BytesRefArrayBlock(
BytesRefArrayVector vector,
BytesRefArrayVector vector, // stylecheck
int positionCount,
int[] firstValueIndexes,
BitSet nulls,
MvOrdering mvOrdering,
BlockFactory blockFactory
MvOrdering mvOrdering
) {
super(positionCount, firstValueIndexes, nulls, mvOrdering, blockFactory);
super(positionCount, firstValueIndexes, nulls, mvOrdering);
this.vector = vector;
assert firstValueIndexes == null
? vector.getPositionCount() == getPositionCount()
Expand Down Expand Up @@ -119,8 +117,7 @@ public BytesRefBlock expand() {
expandedPositionCount,
null,
shiftNullsToExpandedPositions(),
MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING,
blockFactory()
MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING
);
blockFactory().adjustBreaker(expanded.ramBytesUsedOnlyBlock() - bitSetRamUsedEstimate);
// We need to incRef after adjusting any breakers, otherwise we might leak the vector if the breaker trips.
Expand Down Expand Up @@ -164,10 +161,14 @@ public String toString() {

@Override
public void allowPassingToDifferentDriver() {
super.allowPassingToDifferentDriver();
vector.allowPassingToDifferentDriver();
}

@Override
public BlockFactory blockFactory() {
return vector.blockFactory();
}

@Override
public void closeInternal() {
blockFactory().adjustBreaker(-ramBytesUsedOnlyBlock());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public final class BytesRefVectorBlock extends AbstractVectorBlock implements By
* @param vector considered owned by the current block; must not be used in any other {@code Block}
*/
BytesRefVectorBlock(BytesRefVector vector) {
super(vector.getPositionCount(), vector.blockFactory());
this.vector = vector;
}

Expand All @@ -37,7 +36,7 @@ public BytesRef getBytesRef(int valueIndex, BytesRef dest) {
}

@Override
public int getTotalValueCount() {
public int getPositionCount() {
return vector.getPositionCount();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,18 @@ final class DoubleArrayBlock extends AbstractArrayBlock implements DoubleBlock {
positionCount,
firstValueIndexes,
nulls,
mvOrdering,
blockFactory
mvOrdering
);
}

private DoubleArrayBlock(
DoubleArrayVector vector,
DoubleArrayVector vector, // stylecheck
int positionCount,
int[] firstValueIndexes,
BitSet nulls,
MvOrdering mvOrdering,
BlockFactory blockFactory
MvOrdering mvOrdering
) {
super(positionCount, firstValueIndexes, nulls, mvOrdering, blockFactory);
super(positionCount, firstValueIndexes, nulls, mvOrdering);
this.vector = vector;
assert firstValueIndexes == null
? vector.getPositionCount() == getPositionCount()
Expand Down Expand Up @@ -115,8 +113,7 @@ public DoubleBlock expand() {
expandedPositionCount,
null,
shiftNullsToExpandedPositions(),
MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING,
blockFactory()
MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING
);
blockFactory().adjustBreaker(expanded.ramBytesUsedOnlyBlock() - bitSetRamUsedEstimate);
// We need to incRef after adjusting any breakers, otherwise we might leak the vector if the breaker trips.
Expand Down Expand Up @@ -160,10 +157,14 @@ public String toString() {

@Override
public void allowPassingToDifferentDriver() {
super.allowPassingToDifferentDriver();
vector.allowPassingToDifferentDriver();
}

@Override
public BlockFactory blockFactory() {
return vector.blockFactory();
}

@Override
public void closeInternal() {
blockFactory().adjustBreaker(-ramBytesUsedOnlyBlock());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,18 @@ public DoubleBigArrayBlock(
positionCount,
firstValueIndexes,
nulls,
mvOrdering,
blockFactory
mvOrdering
);
}

private DoubleBigArrayBlock(
DoubleBigArrayVector vector,
DoubleBigArrayVector vector, // stylecheck
int positionCount,
int[] firstValueIndexes,
BitSet nulls,
MvOrdering mvOrdering,
BlockFactory blockFactory
MvOrdering mvOrdering
) {
super(positionCount, firstValueIndexes, nulls, mvOrdering, blockFactory);
super(positionCount, firstValueIndexes, nulls, mvOrdering);
this.vector = vector;
assert firstValueIndexes == null
? vector.getPositionCount() == getPositionCount()
Expand Down Expand Up @@ -116,8 +114,7 @@ public DoubleBlock expand() {
expandedPositionCount,
null,
shiftNullsToExpandedPositions(),
MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING,
blockFactory()
MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING
);
blockFactory().adjustBreaker(expanded.ramBytesUsedOnlyBlock() - bitSetRamUsedEstimate);
// We need to incRef after adjusting any breakers, otherwise we might leak the vector if the breaker trips.
Expand Down Expand Up @@ -161,10 +158,14 @@ public String toString() {

@Override
public void allowPassingToDifferentDriver() {
super.allowPassingToDifferentDriver();
vector.allowPassingToDifferentDriver();
}

@Override
public BlockFactory blockFactory() {
return vector.blockFactory();
}

@Override
public void closeInternal() {
blockFactory().adjustBreaker(-ramBytesUsedOnlyBlock());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public final class DoubleVectorBlock extends AbstractVectorBlock implements Doub
* @param vector considered owned by the current block; must not be used in any other {@code Block}
*/
DoubleVectorBlock(DoubleVector vector) {
super(vector.getPositionCount(), vector.blockFactory());
this.vector = vector;
}

Expand All @@ -36,7 +35,7 @@ public double getDouble(int valueIndex) {
}

@Override
public int getTotalValueCount() {
public int getPositionCount() {
return vector.getPositionCount();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,18 @@ final class IntArrayBlock extends AbstractArrayBlock implements IntBlock {
positionCount,
firstValueIndexes,
nulls,
mvOrdering,
blockFactory
mvOrdering
);
}

private IntArrayBlock(
IntArrayVector vector,
IntArrayVector vector, // stylecheck
int positionCount,
int[] firstValueIndexes,
BitSet nulls,
MvOrdering mvOrdering,
BlockFactory blockFactory
MvOrdering mvOrdering
) {
super(positionCount, firstValueIndexes, nulls, mvOrdering, blockFactory);
super(positionCount, firstValueIndexes, nulls, mvOrdering);
this.vector = vector;
assert firstValueIndexes == null
? vector.getPositionCount() == getPositionCount()
Expand Down Expand Up @@ -115,8 +113,7 @@ public IntBlock expand() {
expandedPositionCount,
null,
shiftNullsToExpandedPositions(),
MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING,
blockFactory()
MvOrdering.DEDUPLICATED_AND_SORTED_ASCENDING
);
blockFactory().adjustBreaker(expanded.ramBytesUsedOnlyBlock() - bitSetRamUsedEstimate);
// We need to incRef after adjusting any breakers, otherwise we might leak the vector if the breaker trips.
Expand Down Expand Up @@ -160,10 +157,14 @@ public String toString() {

@Override
public void allowPassingToDifferentDriver() {
super.allowPassingToDifferentDriver();
vector.allowPassingToDifferentDriver();
}

@Override
public BlockFactory blockFactory() {
return vector.blockFactory();
}

@Override
public void closeInternal() {
blockFactory().adjustBreaker(-ramBytesUsedOnlyBlock());
Expand Down
Loading

0 comments on commit 5db2b06

Please sign in to comment.