From 3df6eb8ffa4e3f830b254de1dbd3a114d76382fc Mon Sep 17 00:00:00 2001 From: Svetlana Isakova Date: Mon, 28 Apr 2014 16:31:36 +0400 Subject: [PATCH] Refactored ResolutionResultsCacheImpl Used map instead of trace to store data Rewrote it to Kotlin --- .../context/BasicCallResolutionContext.java | 2 +- .../context/ResolutionResultsCacheImpl.java | 114 ------------------ .../context/ResolutionResultsCacheImpl.kt | 73 +++++++++++ .../context/SimpleResolutionContext.java | 2 +- .../expressions/ExpressionTypingContext.java | 2 +- 5 files changed, 76 insertions(+), 117 deletions(-) delete mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/context/ResolutionResultsCacheImpl.java create mode 100644 compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/context/ResolutionResultsCacheImpl.kt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/context/BasicCallResolutionContext.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/context/BasicCallResolutionContext.java index a5e964c28cd..8c2852f809f 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/context/BasicCallResolutionContext.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/context/BasicCallResolutionContext.java @@ -60,7 +60,7 @@ public class BasicCallResolutionContext extends CallResolutionContext> RESOLUTION_RESULTS = Slices.createSimpleSlice(); - public static final WritableSlice TRACE_DELTAS_CACHE = Slices.createSimpleSlice(); - public static final WritableSlice> DEFERRED_COMPUTATION_FOR_CALL = Slices.createSimpleSlice(); - - static { - BasicWritableSlice.initSliceDebugNames(ResolutionResultsCacheImpl.class); - } - - private final DelegatingBindingTrace trace = new DelegatingBindingTrace( - BindingContext.EMPTY, "Internal binding context in resolution results cache"); - - @Override - public void recordResolutionResults( - @NotNull CallKey callKey, - @NotNull OverloadResolutionResultsImpl results - ) { - //noinspection unchecked - trace.record(RESOLUTION_RESULTS, callKey, (OverloadResolutionResultsImpl) results); - } - - @Override - @Nullable - public OverloadResolutionResultsImpl getResolutionResults( - @NotNull CallKey callKey - ) { - //noinspection unchecked - return (OverloadResolutionResultsImpl) trace.get(RESOLUTION_RESULTS, callKey); - } - - @Override - public void recordResolutionTrace(@NotNull CallKey callKey, @NotNull DelegatingBindingTrace delegatingTrace) { - trace.record(TRACE_DELTAS_CACHE, callKey, delegatingTrace); - } - - @Override - @Nullable - public DelegatingBindingTrace getResolutionTrace(@NotNull CallKey callKey) { - return trace.get(TRACE_DELTAS_CACHE, callKey); - } - - @Override - public void recordDeferredComputationForCall( - @NotNull CallKey callKey, - @NotNull CallCandidateResolutionContext deferredComputation - ) { - trace.record(DEFERRED_COMPUTATION_FOR_CALL, callKey, deferredComputation); - } - - @Override - @Nullable - public CallCandidateResolutionContext getDeferredComputation(@Nullable JetExpression expression) { - return getValueTryingAllCallTypes(expression, DEFERRED_COMPUTATION_FOR_CALL); - } - - @Nullable - private T getValueTryingAllCallTypes( - @Nullable JetExpression expression, - @NotNull WritableSlice slice - ) { - if (expression == null) return null; - for (CallType callType : Lists.newArrayList(DEFAULT, ARRAY_GET_METHOD, ARRAY_SET_METHOD, INVOKE)) { - CallKey callKey = CallKey.create(callType, expression); - T context = trace.get(slice, callKey); - if (context != null) { - return context; - } - } - return null; - } - - @NotNull - public static ResolutionResultsCache create() { - return new ResolutionResultsCacheImpl(); - } - - /*package*/ void addData(@NotNull ResolutionResultsCacheImpl cache) { - cache.trace.addAllMyDataTo(this.trace); - } -} diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/context/ResolutionResultsCacheImpl.kt b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/context/ResolutionResultsCacheImpl.kt new file mode 100644 index 00000000000..0fb32456136 --- /dev/null +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/context/ResolutionResultsCacheImpl.kt @@ -0,0 +1,73 @@ +/* + * Copyright 2010-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.jet.lang.resolve.calls.context + +import org.jetbrains.jet.lang.descriptors.CallableDescriptor +import org.jetbrains.jet.lang.psi.CallKey +import org.jetbrains.jet.lang.resolve.calls.results.OverloadResolutionResultsImpl +import org.jetbrains.jet.lang.resolve.DelegatingBindingTrace +import org.jetbrains.jet.lang.psi.JetExpression +import java.util.HashMap +import org.jetbrains.jet.lang.psi.Call.CallType.* + +class ResolutionResultsCacheImpl : ResolutionResultsCache { + private class CachedData( + var results: OverloadResolutionResultsImpl<*>? = null, + var resolutionTrace: DelegatingBindingTrace? = null, + var deferredComputation: CallCandidateResolutionContext<*>? = null + ) + + private val data = HashMap() + + private fun getOrCreateCachedInfo(callKey: CallKey) = data.getOrPut(callKey, { CachedData() }) + + override fun recordResolutionResults(callKey: CallKey, results: OverloadResolutionResultsImpl) { + getOrCreateCachedInfo(callKey).results = results + } + + override fun getResolutionResults(callKey: CallKey): OverloadResolutionResultsImpl? { + return data[callKey]?.results as OverloadResolutionResultsImpl? + } + + override fun recordResolutionTrace(callKey: CallKey, delegatingTrace: DelegatingBindingTrace) { + getOrCreateCachedInfo(callKey).resolutionTrace = delegatingTrace + } + + override fun getResolutionTrace(callKey: CallKey): DelegatingBindingTrace? { + return data[callKey]?.resolutionTrace + } + + override fun recordDeferredComputationForCall(callKey: CallKey, deferredComputation: CallCandidateResolutionContext) { + getOrCreateCachedInfo(callKey).deferredComputation = deferredComputation + } + + override fun getDeferredComputation(expression: JetExpression?): CallCandidateResolutionContext? { + if (expression == null) return null + + for (callType in listOf(DEFAULT, ARRAY_GET_METHOD, ARRAY_SET_METHOD, INVOKE)) { + val deferredComputation = data[CallKey.create(callType, expression)]?.deferredComputation + if (deferredComputation != null) { + return deferredComputation + } + } + return null + } + + fun addData(cache: ResolutionResultsCacheImpl) { + data.putAll(cache.data) + } +} \ No newline at end of file diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/context/SimpleResolutionContext.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/context/SimpleResolutionContext.java index 24241d6d6fc..32d4f228ed5 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/context/SimpleResolutionContext.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/calls/context/SimpleResolutionContext.java @@ -49,7 +49,7 @@ public class SimpleResolutionContext extends ResolutionContext