From 6fa98d20b8b6d2af2c40140ef97d2132b1acff29 Mon Sep 17 00:00:00 2001 From: Brice Dutheil Date: Mon, 20 Mar 2023 13:46:13 +0100 Subject: [PATCH] fix: reduce the lock contention on InternalVcsService (#114) --- .../lppedd/cc/vcs/InternalVcsService.kt | 26 +++++-------------- 1 file changed, 6 insertions(+), 20 deletions(-) diff --git a/src/main/kotlin/com/github/lppedd/cc/vcs/InternalVcsService.kt b/src/main/kotlin/com/github/lppedd/cc/vcs/InternalVcsService.kt index eb082fc..768425d 100644 --- a/src/main/kotlin/com/github/lppedd/cc/vcs/InternalVcsService.kt +++ b/src/main/kotlin/com/github/lppedd/cc/vcs/InternalVcsService.kt @@ -18,9 +18,6 @@ import com.intellij.vcs.log.visible.filters.VcsLogFilterObject import org.jetbrains.annotations.ApiStatus.* import java.util.* import java.util.Collections.newSetFromMap -import java.util.concurrent.locks.ReentrantReadWriteLock -import kotlin.concurrent.read -import kotlin.concurrent.write import kotlin.io.path.notExists /** @@ -35,11 +32,11 @@ internal class InternalVcsService(private val project: Project) : VcsService { private val vcsLogMultiRepoJoiner = VcsLogMultiRepoJoiner() private val subscribedVcsLogProviders = newSetFromMap(IdentityHashMap(16)) + @Volatile private var cachedCurrentUser: Collection = emptyList() - private val cachedCurrentUserLock = ReentrantReadWriteLock() + @Volatile private var cachedCommits: Collection = emptyList() - private val cachedCommitsLock = ReentrantReadWriteLock() override fun refresh() { val vcsLogProviders = getVcsLogProviders() @@ -66,28 +63,17 @@ internal class InternalVcsService(private val project: Project) : VcsService { } } - override fun getCurrentUsers(): Collection = - cachedCurrentUserLock.read { - cachedCurrentUser - } + override fun getCurrentUsers(): Collection = cachedCurrentUser - override fun getOrderedTopCommits(): Collection = - cachedCommitsLock.read { - cachedCommits - } + override fun getOrderedTopCommits(): Collection = cachedCommits override fun addListener(listener: VcsListener) { refreshListeners.add(listener) } private fun refreshCachedValues() { - cachedCurrentUserLock.write { - cachedCurrentUser = fetchCurrentUsers() - } - - cachedCommitsLock.write { - cachedCommits = fetchCommits(sortBy = VcsCommitMetadata::getCommitTime) - } + cachedCurrentUser = fetchCurrentUsers() + cachedCommits = fetchCommits(sortBy = VcsCommitMetadata::getCommitTime) } private fun fetchCurrentUsers(): Set =