Skip to content

Commit

Permalink
Use JDK Math.clamp
Browse files Browse the repository at this point in the history
  • Loading branch information
electrum authored and wendigo committed Jan 11, 2024
1 parent 017f759 commit a54fb03
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static io.airlift.slice.SizeOf.sizeOfObjectArray;
import static io.trino.operator.FlatHash.sumExact;
import static java.lang.Math.addExact;
import static java.lang.Math.clamp;
import static java.lang.Math.max;
import static java.lang.Math.subtractExact;
import static java.util.Objects.checkIndex;
Expand Down Expand Up @@ -117,7 +118,7 @@ public byte[] allocate(byte[] pointer, int pointerOffset, int size)
// allocate enough space for 32 values of the current size, or double the current chunk size, whichever is larger
int newSize = Ints.saturatedCast(max(size * 32L, openChunk.length * 2L));
// constrain to be between min and max chunk size
newSize = Ints.constrainToRange(newSize, MIN_CHUNK_SIZE, MAX_CHUNK_SIZE);
newSize = clamp(newSize, MIN_CHUNK_SIZE, MAX_CHUNK_SIZE);
// jumbo rows get a separate allocation
newSize = max(newSize, size);
openChunk = new byte[newSize];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package io.trino.operator.aggregation;

import com.google.common.base.Throwables;
import com.google.common.primitives.Ints;
import io.trino.operator.VariableWidthData;
import io.trino.spi.TrinoException;
import io.trino.spi.block.BlockBuilder;
Expand All @@ -35,6 +34,7 @@
import static io.trino.operator.VariableWidthData.EMPTY_CHUNK;
import static io.trino.operator.VariableWidthData.POINTER_SIZE;
import static io.trino.spi.StandardErrorCode.GENERIC_INSUFFICIENT_RESOURCES;
import static java.lang.Math.clamp;
import static java.lang.Math.multiplyExact;
import static java.nio.ByteOrder.LITTLE_ENDIAN;
import static java.util.Objects.checkIndex;
Expand Down Expand Up @@ -215,7 +215,7 @@ public void setMaxGroupId(int maxGroupId)

int currentSize = groupRecordIndex.length;
if (requiredSize > currentSize) {
groupRecordIndex = Arrays.copyOf(groupRecordIndex, Ints.constrainToRange(requiredSize * 2, 1024, MAX_ARRAY_SIZE));
groupRecordIndex = Arrays.copyOf(groupRecordIndex, clamp(requiredSize * 2L, 1024, MAX_ARRAY_SIZE));
Arrays.fill(groupRecordIndex, currentSize, groupRecordIndex.length, -1);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
package io.trino.operator.aggregation.arrayagg;

import com.google.common.primitives.Ints;
import io.trino.operator.aggregation.state.AbstractGroupedAccumulatorState;
import io.trino.spi.block.BlockBuilder;
import io.trino.spi.block.ValueBlock;
Expand All @@ -25,6 +24,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static io.airlift.slice.SizeOf.instanceSize;
import static io.airlift.slice.SizeOf.sizeOf;
import static java.lang.Math.clamp;
import static java.lang.Math.toIntExact;

public final class GroupArrayAggregationState
Expand Down Expand Up @@ -60,7 +60,7 @@ public void ensureCapacity(long maxGroupId)
checkArgument(maxGroupId + 1 < MAX_ARRAY_SIZE, "Maximum array size exceeded");
int requiredSize = toIntExact(maxGroupId + 1);
if (requiredSize > groupHeadPositions.length) {
int newSize = Ints.constrainToRange(requiredSize * 2, 1024, MAX_ARRAY_SIZE);
int newSize = clamp(requiredSize * 2L, 1024, MAX_ARRAY_SIZE);
int oldSize = groupHeadPositions.length;

groupHeadPositions = Arrays.copyOf(groupHeadPositions, newSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package io.trino.operator.aggregation.histogram;

import com.google.common.base.Throwables;
import com.google.common.primitives.Ints;
import io.trino.operator.VariableWidthData;
import io.trino.spi.TrinoException;
import io.trino.spi.block.BlockBuilder;
Expand All @@ -36,6 +35,7 @@
import static io.trino.operator.VariableWidthData.POINTER_SIZE;
import static io.trino.spi.StandardErrorCode.GENERIC_INSUFFICIENT_RESOURCES;
import static io.trino.spi.type.BigintType.BIGINT;
import static java.lang.Math.clamp;
import static java.lang.Math.multiplyExact;
import static java.nio.ByteOrder.LITTLE_ENDIAN;
import static java.util.Objects.checkIndex;
Expand Down Expand Up @@ -167,7 +167,7 @@ public void setMaxGroupId(int maxGroupId)

int currentSize = groupRecordIndex.length;
if (requiredSize > currentSize) {
groupRecordIndex = Arrays.copyOf(groupRecordIndex, Ints.constrainToRange(requiredSize * 2, 1024, MAX_ARRAY_SIZE));
groupRecordIndex = Arrays.copyOf(groupRecordIndex, clamp(requiredSize * 2L, 1024, MAX_ARRAY_SIZE));
Arrays.fill(groupRecordIndex, currentSize, groupRecordIndex.length, -1);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
package io.trino.operator.aggregation.listagg;

import com.google.common.primitives.Ints;
import io.airlift.slice.DynamicSliceOutput;
import io.airlift.slice.SliceOutput;
import io.trino.spi.block.ValueBlock;
Expand All @@ -28,6 +27,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static io.airlift.slice.SizeOf.sizeOf;
import static io.trino.operator.VariableWidthData.POINTER_SIZE;
import static java.lang.Math.clamp;
import static java.lang.Math.toIntExact;
import static java.nio.ByteOrder.LITTLE_ENDIAN;

Expand Down Expand Up @@ -90,7 +90,7 @@ public void ensureCapacity(long maxGroupId)
checkArgument(maxGroupId + 1 < MAX_ARRAY_SIZE, "Maximum array size exceeded");
int requiredSize = toIntExact(maxGroupId + 1);
if (requiredSize > groupHeadPositions.length) {
int newSize = Ints.constrainToRange(requiredSize * 2, 1024, MAX_ARRAY_SIZE);
int newSize = clamp(requiredSize * 2L, 1024, MAX_ARRAY_SIZE);
int oldSize = groupHeadPositions.length;

groupHeadPositions = Arrays.copyOf(groupHeadPositions, newSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package io.trino.operator.aggregation.minmaxn;

import com.google.common.base.Throwables;
import com.google.common.primitives.Ints;
import io.airlift.slice.SizeOf;
import io.trino.operator.VariableWidthData;
import io.trino.spi.block.BlockBuilder;
Expand All @@ -36,6 +35,7 @@
import static io.trino.operator.VariableWidthData.getChunkOffset;
import static io.trino.operator.VariableWidthData.getValueLength;
import static io.trino.operator.VariableWidthData.writePointer;
import static java.lang.Math.clamp;
import static java.util.Objects.requireNonNull;

public final class TypedHeap
Expand Down Expand Up @@ -382,7 +382,7 @@ public static VariableWidthData compactIfNecessary(VariableWidthData data, byte[
if (newSize > 0) {
int openSliceSize = newSize;
if (newSize < MAX_CHUNK_SIZE) {
openSliceSize = Ints.constrainToRange(Ints.saturatedCast(openSliceSize * 2L), MIN_CHUNK_SIZE, MAX_CHUNK_SIZE);
openSliceSize = clamp(openSliceSize * 2L, MIN_CHUNK_SIZE, MAX_CHUNK_SIZE);
}
moveVariableWidthToNewSlice(data, fixedSizeChunk, fixedRecordSize, fixedRecordPointerOffset, indexStart, recordCount, newSlices, openSliceSize, relocateVariableWidthOffsets);
openChunkOffset = newSize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package io.trino.operator.aggregation.multimapagg;

import com.google.common.base.Throwables;
import com.google.common.primitives.Ints;
import io.trino.operator.VariableWidthData;
import io.trino.operator.aggregation.arrayagg.FlatArrayBuilder;
import io.trino.spi.TrinoException;
Expand All @@ -41,6 +40,7 @@
import static io.trino.operator.VariableWidthData.EMPTY_CHUNK;
import static io.trino.operator.VariableWidthData.POINTER_SIZE;
import static io.trino.spi.StandardErrorCode.GENERIC_INSUFFICIENT_RESOURCES;
import static java.lang.Math.clamp;
import static java.lang.Math.multiplyExact;
import static java.nio.ByteOrder.LITTLE_ENDIAN;
import static java.util.Objects.checkIndex;
Expand Down Expand Up @@ -220,7 +220,7 @@ public void setMaxGroupId(int maxGroupId)

int currentSize = groupRecordIndex.length;
if (requiredSize > currentSize) {
groupRecordIndex = Arrays.copyOf(groupRecordIndex, Ints.constrainToRange(requiredSize * 2, 1024, MAX_ARRAY_SIZE));
groupRecordIndex = Arrays.copyOf(groupRecordIndex, clamp(requiredSize * 2L, 1024, MAX_ARRAY_SIZE));
Arrays.fill(groupRecordIndex, currentSize, groupRecordIndex.length, -1);
}
}
Expand Down Expand Up @@ -417,7 +417,7 @@ private int insert(int keyIndex, int groupId, ValueBlock keyBlock, int keyPositi
}

if (nextKeyId >= keyHeadPositions.length) {
int newSize = Ints.constrainToRange(nextKeyId * 2, 1024, MAX_ARRAY_SIZE);
int newSize = clamp(nextKeyId * 2L, 1024, MAX_ARRAY_SIZE);
int oldSize = keyHeadPositions.length;

keyHeadPositions = Arrays.copyOf(keyHeadPositions, newSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import java.io.IOException;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.primitives.Longs.constrainToRange;
import static io.trino.filesystem.azure.AzureUtils.handleAzureException;
import static java.lang.Math.clamp;
import static java.util.Objects.checkFromIndexSize;
import static java.util.Objects.requireNonNull;

Expand Down Expand Up @@ -129,7 +129,7 @@ public long skip(long n)
{
ensureOpen();

long skipSize = constrainToRange(n, 0, fileSize - nextPosition);
long skipSize = clamp(n, 0, fileSize - nextPosition);
nextPosition += skipSize;
return skipSize;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import java.nio.ByteBuffer;
import java.util.OptionalLong;

import static com.google.common.primitives.Longs.constrainToRange;
import static io.trino.filesystem.gcs.GcsUtils.getReadChannel;
import static io.trino.filesystem.gcs.GcsUtils.handleGcsException;
import static java.lang.Math.clamp;
import static java.util.Objects.checkFromIndexSize;
import static java.util.Objects.requireNonNull;

Expand Down Expand Up @@ -134,7 +134,7 @@ public long skip(long n)
throws IOException
{
ensureOpen();
long skipSize = constrainToRange(n, 0, fileSize - nextPosition);
long skipSize = clamp(n, 0, fileSize - nextPosition);
nextPosition += skipSize;
return skipSize;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import static com.google.common.primitives.Ints.constrainToRange;
import static java.lang.Math.clamp;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.System.arraycopy;
Expand Down Expand Up @@ -179,7 +179,7 @@ private void ensureCapacity(int extra)
int target = max(buffer.length, initialBufferSize);
if (target < capacity) {
target += target / 2; // increase 50%
target = constrainToRange(target, capacity, partSize);
target = clamp(target, capacity, partSize);
}
buffer = Arrays.copyOf(buffer, target);
memoryContext.setBytes(buffer.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package io.trino.filesystem.local;

import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import io.trino.filesystem.Location;
import io.trino.filesystem.TrinoInputStream;

Expand All @@ -26,6 +25,7 @@
import java.io.IOException;
import java.io.InputStream;

import static java.lang.Math.clamp;
import static java.util.Objects.requireNonNull;

class LocalInputStream
Expand Down Expand Up @@ -135,7 +135,7 @@ public long skip(long length)
{
ensureOpen();

length = Longs.constrainToRange(length, 0, fileLength - position);
length = clamp(length, 0, fileLength - position);
seek(position + length);
return length;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.primitives.Ints.constrainToRange;
import static io.airlift.slice.SizeOf.instanceSize;
import static io.airlift.slice.SizeOf.sizeOf;
import static java.lang.Math.clamp;

public final class LineBuffer
extends OutputStream
Expand Down Expand Up @@ -112,7 +112,7 @@ private void growBufferIfNecessary(int minFreeSpace)
throw new IOException("Max line length exceeded: " + newLength);
}
if (buffer.length < newLength) {
int newSize = constrainToRange(buffer.length * 2, newLength, maxLength);
int newSize = clamp(buffer.length * 2L, newLength, maxLength);
buffer = Arrays.copyOf(buffer, newSize);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import io.trino.spi.SplitWeight;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.primitives.Doubles.constrainToRange;
import static java.lang.Math.clamp;
import static java.util.Objects.requireNonNull;

public class SizeBasedSplitWeightProvider
Expand All @@ -42,6 +42,6 @@ public SplitWeight calculateSplitWeight(long splitSizeInBytes)
{
double computedWeight = splitSizeInBytes / standardSplitSizeInBytes;
// Clamp the value between the minimum weight and 1.0 (standard weight)
return SplitWeight.fromProportion(constrainToRange(computedWeight, minimumWeight, 1.0));
return SplitWeight.fromProportion(clamp(computedWeight, minimumWeight, 1.0));
}
}

0 comments on commit a54fb03

Please sign in to comment.