Skip to content

Commit

Permalink
Memoize ResolvedFunction <-> QualifiedName conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
wendigo authored and electrum committed Apr 12, 2022
1 parent e212f54 commit f0b48b8
Showing 1 changed file with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Splitter;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import io.airlift.compress.zstd.ZstdCompressor;
import io.airlift.compress.zstd.ZstdDecompressor;
import io.airlift.json.JsonCodec;
import io.airlift.json.JsonCodecFactory;
import io.airlift.json.ObjectMapperProvider;
import io.trino.collect.cache.NonEvictableLoadingCache;
import io.trino.spi.type.Type;
import io.trino.spi.type.TypeId;
import io.trino.spi.type.TypeSignature;
Expand All @@ -43,7 +46,7 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.io.BaseEncoding.base32Hex;
import static io.trino.metadata.ResolvedFunction.ResolvedFunctionDecoder.serialize;
import static io.trino.collect.cache.SafeCaches.buildNonEvictableCache;
import static java.lang.Math.toIntExact;
import static java.nio.ByteBuffer.allocate;
import static java.util.Locale.ENGLISH;
Expand Down Expand Up @@ -129,7 +132,7 @@ public static boolean isResolved(QualifiedName name)

public QualifiedName toQualifiedName()
{
return ResolvedFunctionDecoder.serialize(this);
return ResolvedFunctionDecoder.toQualifiedName(this);
}

public static String extractFunctionName(QualifiedName qualifiedName)
Expand Down Expand Up @@ -178,6 +181,12 @@ public static class ResolvedFunctionDecoder
private static final JsonCodec<ResolvedFunction> SERIALIZE_JSON_CODEC = new JsonCodecFactory().jsonCodec(ResolvedFunction.class);
private static final ThreadLocalCompressorDecompressor COMPRESSOR_DECOMPRESSOR = new ThreadLocalCompressorDecompressor(ZstdCompressor::new, ZstdDecompressor::new);

private final NonEvictableLoadingCache<QualifiedName, ResolvedFunction> resolvedFunctions = buildNonEvictableCache(
CacheBuilder.newBuilder().maximumSize(1024), CacheLoader.from(this::deserialize));

private static final NonEvictableLoadingCache<ResolvedFunction, QualifiedName> qualifiedNames = buildNonEvictableCache(
CacheBuilder.newBuilder().maximumSize(1024), CacheLoader.from(ResolvedFunctionDecoder::serialize));

private final JsonCodec<ResolvedFunction> jsonCodec;

public ResolvedFunctionDecoder(Function<TypeId, Type> typeLoader)
Expand All @@ -197,7 +206,12 @@ public Optional<ResolvedFunction> fromQualifiedName(QualifiedName qualifiedName)
return Optional.empty();
}

return Optional.of(deserialize(qualifiedName));
return Optional.of(resolvedFunctions.getUnchecked(qualifiedName));
}

public static QualifiedName toQualifiedName(ResolvedFunction function)
{
return qualifiedNames.getUnchecked(function);
}

private ResolvedFunction deserialize(QualifiedName qualifiedName)
Expand Down

0 comments on commit f0b48b8

Please sign in to comment.