Skip to content

Commit 28e44b2

Browse files
committed
fix(model-server): caching of computed VersionDelta
When there are multiple clients connected to the same branch of a repository they all will receive the same delta immediately after a change. Even a tiny cache size has a huge effect.
1 parent c37921b commit 28e44b2

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

model-server/src/main/kotlin/org/modelix/model/server/handlers/RepositoriesManager.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
package org.modelix.model.server.handlers
1515

1616
import kotlinx.datetime.Clock
17+
import org.apache.commons.collections4.map.LRUMap
1718
import org.modelix.model.VersionMerger
1819
import org.modelix.model.api.IBranch
1920
import org.modelix.model.api.IReadTransaction
2021
import org.modelix.model.api.ITree
2122
import org.modelix.model.api.IdGeneratorDummy
2223
import org.modelix.model.api.PBranch
24+
import org.modelix.model.api.runSynchronized
2325
import org.modelix.model.lazy.BranchReference
2426
import org.modelix.model.lazy.CLTree
2527
import org.modelix.model.lazy.CLVersion
@@ -147,10 +149,18 @@ class RepositoriesManager(val client: LocalModelClient) {
147149
?: throw IllegalStateException("No version found for branch '${branch.branchName}' in repository '${branch.repositoryId}'")
148150
}
149151

152+
private val deltaCache = LRUMap<Pair<String, String?>, Lazy<Map<String, String>>>(10)
150153
fun computeDelta(versionHash: String, baseVersionHash: String?): Map<String, String> {
151-
val version = CLVersion(versionHash, client.storeCache)
152-
val baseVersion = baseVersionHash?.let { CLVersion(it, client.storeCache) }
153-
return version.computeDelta(baseVersion)
154+
return runSynchronized(deltaCache) {
155+
deltaCache.getOrPut(versionHash to baseVersionHash) {
156+
// lazy { ... } allows to run the computation without locking deltaCache
157+
lazy {
158+
val version = CLVersion(versionHash, client.storeCache)
159+
val baseVersion = baseVersionHash?.let { CLVersion(it, client.storeCache) }
160+
version.computeDelta(baseVersion)
161+
}
162+
}
163+
}.value
154164
}
155165

156166
private fun branchKey(branch: BranchReference): String {

0 commit comments

Comments
 (0)