Skip to content

Commit

Permalink
perf: Blink / AddOnly SortedFirstOrLast, use explicit comparison oper…
Browse files Browse the repository at this point in the history
…ator (#5818)

This is handling a subset of SortedFirstOrLast operators; the general
case which goes through
`io.deephaven.engine.table.impl.by.SortedFirstOrLastChunkedOperator` /
`SegmentedSortedArray` is a larger change that should land
independently.

The logic is identical, but this provides the JVM a more specific target
to optimize.
  • Loading branch information
devinrsmith authored Jul 21, 2024
1 parent 02d2508 commit ce45558
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,16 @@ private boolean addChunk(@NotNull final ByteChunk<? extends Values> values,
for (int ii = newDestination ? 1 : 0; ii < length; ++ii) {
final long index = indices.get(start + ii);
final byte value = values.get(start + ii);
final int comparison = ByteComparisons.compare(value, bestValue);
// @formatter:off
final boolean better =
( isFirst && (comparison < 0 || (comparison == 0 && index < bestIndex))) ||
(!isFirst && (comparison > 0 || (comparison == 0 && index > bestIndex))) ;
// @formatter:on
final boolean better;
if (isFirst) {
better = index < bestIndex
? ByteComparisons.leq(value, bestValue)
: ByteComparisons.lt(value, bestValue);
} else {
better = index > bestIndex
? ByteComparisons.geq(value, bestValue)
: ByteComparisons.gt(value, bestValue);
}
if (better) {
bestIndex = index;
bestValue = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,10 @@ private boolean addChunk(@NotNull final ByteChunk<? extends Values> values,
for (int ii = newDestination ? 1 : 0; ii < length; ++ii) {
final int chunkPos = start + ii;
final byte value = values.get(chunkPos);
final int comparison = ByteComparisons.compare(value, bestValue);
// @formatter:off
// No need to compare relative row keys. A stream's logical row set is always monotonically increasing.
final boolean better =
( isFirst && comparison < 0) ||
(!isFirst && comparison >= 0) ;
// @formatter:on
final boolean better = isFirst
? ByteComparisons.lt(value, bestValue)
: ByteComparisons.geq(value, bestValue);
if (better) {
bestChunkPos = chunkPos;
bestValue = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,16 @@ private boolean addChunk(@NotNull final CharChunk<? extends Values> values,
for (int ii = newDestination ? 1 : 0; ii < length; ++ii) {
final long index = indices.get(start + ii);
final char value = values.get(start + ii);
final int comparison = CharComparisons.compare(value, bestValue);
// @formatter:off
final boolean better =
( isFirst && (comparison < 0 || (comparison == 0 && index < bestIndex))) ||
(!isFirst && (comparison > 0 || (comparison == 0 && index > bestIndex))) ;
// @formatter:on
final boolean better;
if (isFirst) {
better = index < bestIndex
? CharComparisons.leq(value, bestValue)
: CharComparisons.lt(value, bestValue);
} else {
better = index > bestIndex
? CharComparisons.geq(value, bestValue)
: CharComparisons.gt(value, bestValue);
}
if (better) {
bestIndex = index;
bestValue = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,10 @@ private boolean addChunk(@NotNull final CharChunk<? extends Values> values,
for (int ii = newDestination ? 1 : 0; ii < length; ++ii) {
final int chunkPos = start + ii;
final char value = values.get(chunkPos);
final int comparison = CharComparisons.compare(value, bestValue);
// @formatter:off
// No need to compare relative row keys. A stream's logical row set is always monotonically increasing.
final boolean better =
( isFirst && comparison < 0) ||
(!isFirst && comparison >= 0) ;
// @formatter:on
final boolean better = isFirst
? CharComparisons.lt(value, bestValue)
: CharComparisons.geq(value, bestValue);
if (better) {
bestChunkPos = chunkPos;
bestValue = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,16 @@ private boolean addChunk(@NotNull final DoubleChunk<? extends Values> values,
for (int ii = newDestination ? 1 : 0; ii < length; ++ii) {
final long index = indices.get(start + ii);
final double value = values.get(start + ii);
final int comparison = DoubleComparisons.compare(value, bestValue);
// @formatter:off
final boolean better =
( isFirst && (comparison < 0 || (comparison == 0 && index < bestIndex))) ||
(!isFirst && (comparison > 0 || (comparison == 0 && index > bestIndex))) ;
// @formatter:on
final boolean better;
if (isFirst) {
better = index < bestIndex
? DoubleComparisons.leq(value, bestValue)
: DoubleComparisons.lt(value, bestValue);
} else {
better = index > bestIndex
? DoubleComparisons.geq(value, bestValue)
: DoubleComparisons.gt(value, bestValue);
}
if (better) {
bestIndex = index;
bestValue = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,10 @@ private boolean addChunk(@NotNull final DoubleChunk<? extends Values> values,
for (int ii = newDestination ? 1 : 0; ii < length; ++ii) {
final int chunkPos = start + ii;
final double value = values.get(chunkPos);
final int comparison = DoubleComparisons.compare(value, bestValue);
// @formatter:off
// No need to compare relative row keys. A stream's logical row set is always monotonically increasing.
final boolean better =
( isFirst && comparison < 0) ||
(!isFirst && comparison >= 0) ;
// @formatter:on
final boolean better = isFirst
? DoubleComparisons.lt(value, bestValue)
: DoubleComparisons.geq(value, bestValue);
if (better) {
bestChunkPos = chunkPos;
bestValue = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,16 @@ private boolean addChunk(@NotNull final FloatChunk<? extends Values> values,
for (int ii = newDestination ? 1 : 0; ii < length; ++ii) {
final long index = indices.get(start + ii);
final float value = values.get(start + ii);
final int comparison = FloatComparisons.compare(value, bestValue);
// @formatter:off
final boolean better =
( isFirst && (comparison < 0 || (comparison == 0 && index < bestIndex))) ||
(!isFirst && (comparison > 0 || (comparison == 0 && index > bestIndex))) ;
// @formatter:on
final boolean better;
if (isFirst) {
better = index < bestIndex
? FloatComparisons.leq(value, bestValue)
: FloatComparisons.lt(value, bestValue);
} else {
better = index > bestIndex
? FloatComparisons.geq(value, bestValue)
: FloatComparisons.gt(value, bestValue);
}
if (better) {
bestIndex = index;
bestValue = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,10 @@ private boolean addChunk(@NotNull final FloatChunk<? extends Values> values,
for (int ii = newDestination ? 1 : 0; ii < length; ++ii) {
final int chunkPos = start + ii;
final float value = values.get(chunkPos);
final int comparison = FloatComparisons.compare(value, bestValue);
// @formatter:off
// No need to compare relative row keys. A stream's logical row set is always monotonically increasing.
final boolean better =
( isFirst && comparison < 0) ||
(!isFirst && comparison >= 0) ;
// @formatter:on
final boolean better = isFirst
? FloatComparisons.lt(value, bestValue)
: FloatComparisons.geq(value, bestValue);
if (better) {
bestChunkPos = chunkPos;
bestValue = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,16 @@ private boolean addChunk(@NotNull final IntChunk<? extends Values> values,
for (int ii = newDestination ? 1 : 0; ii < length; ++ii) {
final long index = indices.get(start + ii);
final int value = values.get(start + ii);
final int comparison = IntComparisons.compare(value, bestValue);
// @formatter:off
final boolean better =
( isFirst && (comparison < 0 || (comparison == 0 && index < bestIndex))) ||
(!isFirst && (comparison > 0 || (comparison == 0 && index > bestIndex))) ;
// @formatter:on
final boolean better;
if (isFirst) {
better = index < bestIndex
? IntComparisons.leq(value, bestValue)
: IntComparisons.lt(value, bestValue);
} else {
better = index > bestIndex
? IntComparisons.geq(value, bestValue)
: IntComparisons.gt(value, bestValue);
}
if (better) {
bestIndex = index;
bestValue = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,10 @@ private boolean addChunk(@NotNull final IntChunk<? extends Values> values,
for (int ii = newDestination ? 1 : 0; ii < length; ++ii) {
final int chunkPos = start + ii;
final int value = values.get(chunkPos);
final int comparison = IntComparisons.compare(value, bestValue);
// @formatter:off
// No need to compare relative row keys. A stream's logical row set is always monotonically increasing.
final boolean better =
( isFirst && comparison < 0) ||
(!isFirst && comparison >= 0) ;
// @formatter:on
final boolean better = isFirst
? IntComparisons.lt(value, bestValue)
: IntComparisons.geq(value, bestValue);
if (better) {
bestChunkPos = chunkPos;
bestValue = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,16 @@ private boolean addChunk(@NotNull final LongChunk<? extends Values> values,
for (int ii = newDestination ? 1 : 0; ii < length; ++ii) {
final long index = indices.get(start + ii);
final long value = values.get(start + ii);
final int comparison = LongComparisons.compare(value, bestValue);
// @formatter:off
final boolean better =
( isFirst && (comparison < 0 || (comparison == 0 && index < bestIndex))) ||
(!isFirst && (comparison > 0 || (comparison == 0 && index > bestIndex))) ;
// @formatter:on
final boolean better;
if (isFirst) {
better = index < bestIndex
? LongComparisons.leq(value, bestValue)
: LongComparisons.lt(value, bestValue);
} else {
better = index > bestIndex
? LongComparisons.geq(value, bestValue)
: LongComparisons.gt(value, bestValue);
}
if (better) {
bestIndex = index;
bestValue = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,10 @@ private boolean addChunk(@NotNull final LongChunk<? extends Values> values,
for (int ii = newDestination ? 1 : 0; ii < length; ++ii) {
final int chunkPos = start + ii;
final long value = values.get(chunkPos);
final int comparison = LongComparisons.compare(value, bestValue);
// @formatter:off
// No need to compare relative row keys. A stream's logical row set is always monotonically increasing.
final boolean better =
( isFirst && comparison < 0) ||
(!isFirst && comparison >= 0) ;
// @formatter:on
final boolean better = isFirst
? LongComparisons.lt(value, bestValue)
: LongComparisons.geq(value, bestValue);
if (better) {
bestChunkPos = chunkPos;
bestValue = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,16 @@ private boolean addChunk(@NotNull final ObjectChunk<Object, ? extends Values> va
for (int ii = newDestination ? 1 : 0; ii < length; ++ii) {
final long index = indices.get(start + ii);
final Object value = values.get(start + ii);
final int comparison = ObjectComparisons.compare(value, bestValue);
// @formatter:off
final boolean better =
( isFirst && (comparison < 0 || (comparison == 0 && index < bestIndex))) ||
(!isFirst && (comparison > 0 || (comparison == 0 && index > bestIndex))) ;
// @formatter:on
final boolean better;
if (isFirst) {
better = index < bestIndex
? ObjectComparisons.leq(value, bestValue)
: ObjectComparisons.lt(value, bestValue);
} else {
better = index > bestIndex
? ObjectComparisons.geq(value, bestValue)
: ObjectComparisons.gt(value, bestValue);
}
if (better) {
bestIndex = index;
bestValue = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,10 @@ private boolean addChunk(@NotNull final ObjectChunk<Object, ? extends Values> va
for (int ii = newDestination ? 1 : 0; ii < length; ++ii) {
final int chunkPos = start + ii;
final Object value = values.get(chunkPos);
final int comparison = ObjectComparisons.compare(value, bestValue);
// @formatter:off
// No need to compare relative row keys. A stream's logical row set is always monotonically increasing.
final boolean better =
( isFirst && comparison < 0) ||
(!isFirst && comparison >= 0) ;
// @formatter:on
final boolean better = isFirst
? ObjectComparisons.lt(value, bestValue)
: ObjectComparisons.geq(value, bestValue);
if (better) {
bestChunkPos = chunkPos;
bestValue = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,16 @@ private boolean addChunk(@NotNull final ShortChunk<? extends Values> values,
for (int ii = newDestination ? 1 : 0; ii < length; ++ii) {
final long index = indices.get(start + ii);
final short value = values.get(start + ii);
final int comparison = ShortComparisons.compare(value, bestValue);
// @formatter:off
final boolean better =
( isFirst && (comparison < 0 || (comparison == 0 && index < bestIndex))) ||
(!isFirst && (comparison > 0 || (comparison == 0 && index > bestIndex))) ;
// @formatter:on
final boolean better;
if (isFirst) {
better = index < bestIndex
? ShortComparisons.leq(value, bestValue)
: ShortComparisons.lt(value, bestValue);
} else {
better = index > bestIndex
? ShortComparisons.geq(value, bestValue)
: ShortComparisons.gt(value, bestValue);
}
if (better) {
bestIndex = index;
bestValue = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,10 @@ private boolean addChunk(@NotNull final ShortChunk<? extends Values> values,
for (int ii = newDestination ? 1 : 0; ii < length; ++ii) {
final int chunkPos = start + ii;
final short value = values.get(chunkPos);
final int comparison = ShortComparisons.compare(value, bestValue);
// @formatter:off
// No need to compare relative row keys. A stream's logical row set is always monotonically increasing.
final boolean better =
( isFirst && comparison < 0) ||
(!isFirst && comparison >= 0) ;
// @formatter:on
final boolean better = isFirst
? ShortComparisons.lt(value, bestValue)
: ShortComparisons.geq(value, bestValue);
if (better) {
bestChunkPos = chunkPos;
bestValue = value;
Expand Down

0 comments on commit ce45558

Please sign in to comment.