Optimize synchronization for resolution anchors

Use volatile state instead of synchronized access to it.
This should be enough as the component's state is only read
by the component users and never changed.
This commit is contained in:
Pavel Kirpichenkov
2020-06-03 14:35:32 +03:00
parent 175fe163af
commit 986b13c3a1
@@ -24,16 +24,17 @@ import org.jetbrains.kotlin.resolve.ResolutionAnchorProvider
@State(name = "KotlinIdeAnchorService", storages = [Storage("anchors.xml")]) @State(name = "KotlinIdeAnchorService", storages = [Storage("anchors.xml")])
class KotlinIdeResolutionAnchorService( class KotlinIdeResolutionAnchorService(
val project: Project val project: Project
) : ResolutionAnchorProvider, ) : ResolutionAnchorProvider,
PersistentStateComponent<KotlinIdeResolutionAnchorService.State> { PersistentStateComponent<KotlinIdeResolutionAnchorService.State> {
data class State( data class State(
var moduleNameToAnchorName: Map<String, String> = emptyMap() var moduleNameToAnchorName: Map<String, String> = emptyMap()
) )
private val logger = logger<KotlinIdeResolutionAnchorService>() private val logger = logger<KotlinIdeResolutionAnchorService>()
@JvmField @JvmField
@Volatile
var myState: State = State() var myState: State = State()
private fun buildMapping(): Map<ModuleInfo, ModuleInfo> { private fun buildMapping(): Map<ModuleInfo, ModuleInfo> {
@@ -52,7 +53,7 @@ class KotlinIdeResolutionAnchorService(
module to anchor module to anchor
}.toMap() }.toMap()
} }
private fun notFoundModule(moduleName: String): Nothing? { private fun notFoundModule(moduleName: String): Nothing? {
logger.warn("Module <${moduleName}> not found in project model") logger.warn("Module <${moduleName}> not found in project model")
return null return null
@@ -63,15 +64,12 @@ class KotlinIdeResolutionAnchorService(
buildMapping() buildMapping()
} }
@Synchronized
override fun getState(): State = myState override fun getState(): State = myState
@Synchronized
override fun loadState(state: State) { override fun loadState(state: State) {
XmlSerializerUtil.copyBean(state, myState) XmlSerializerUtil.copyBean(state, myState)
} }
@Synchronized
override fun getResolutionAnchor(moduleDescriptor: ModuleDescriptor): ModuleDescriptor? { override fun getResolutionAnchor(moduleDescriptor: ModuleDescriptor): ModuleDescriptor? {
if (!project.libraryToSourceAnalysisEnabled) return null if (!project.libraryToSourceAnalysisEnabled) return null
val moduleInfo = moduleDescriptor.moduleInfo ?: return null val moduleInfo = moduleDescriptor.moduleInfo ?: return null