Skip to content

Commit

Permalink
Cache short decimal type instances
Browse files Browse the repository at this point in the history
When profiling Iceberg planning on TPC-DS data set, it was found out that 10% of
time spend inside `TableStatisticsMaker` is actually spent inside
`ShortDecimalType` constructor.
  • Loading branch information
findepi committed Oct 10, 2022
1 parent 17bf26c commit 96a009e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static DecimalType createDecimalType(int precision, int scale)
}

if (precision <= MAX_SHORT_PRECISION) {
return new ShortDecimalType(precision, scale);
return ShortDecimalType.getInstance(precision, scale);
}
return new LongDecimalType(precision, scale);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static io.trino.spi.function.OperatorType.LESS_THAN;
import static io.trino.spi.function.OperatorType.LESS_THAN_OR_EQUAL;
import static io.trino.spi.function.OperatorType.XX_HASH_64;
import static io.trino.spi.type.Decimals.MAX_SHORT_PRECISION;
import static io.trino.spi.type.TypeOperatorDeclaration.extractOperatorDeclaration;
import static java.lang.invoke.MethodHandles.lookup;

Expand All @@ -42,7 +43,23 @@ final class ShortDecimalType
{
private static final TypeOperatorDeclaration TYPE_OPERATOR_DECLARATION = extractOperatorDeclaration(ShortDecimalType.class, lookup(), long.class);

ShortDecimalType(int precision, int scale)
private static final ShortDecimalType[][] INSTANCES;

static {
INSTANCES = new ShortDecimalType[MAX_SHORT_PRECISION][MAX_SHORT_PRECISION + 1];
for (int precision = 1; precision <= MAX_SHORT_PRECISION; precision++) {
for (int scale = 0; scale <= precision; scale++) {
INSTANCES[precision - 1][scale] = new ShortDecimalType(precision, scale);
}
}
}

static ShortDecimalType getInstance(int precision, int scale)
{
return INSTANCES[precision - 1][scale];
}

private ShortDecimalType(int precision, int scale)
{
super(precision, scale, long.class);
checkArgument(0 < precision && precision <= Decimals.MAX_SHORT_PRECISION, "Invalid precision: %s", precision);
Expand Down

0 comments on commit 96a009e

Please sign in to comment.