Skip to content

Commit c572ff6

Browse files
author
Oleksandr Dzhychko
authored
Merge pull request #568 from modelix/improve-model-client-memory-usage
perf(model-datastructure): use a memory efficient map for MapBasedStore
2 parents 2bcb533 + f0a3f4d commit c572ff6

File tree

7 files changed

+19
-38
lines changed

7 files changed

+19
-38
lines changed

bulk-model-sync-lib/build.gradle.kts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ kotlin {
2121
}
2222
}
2323

24-
val jvmMain by getting {
25-
dependencies {
26-
implementation(libs.trove4j)
27-
}
28-
}
29-
3024
val commonTest by getting {
3125
dependencies {
3226
implementation(project(":model-api"))

bulk-model-sync-lib/src/commonMain/kotlin/org/modelix/model/sync/bulk/ModelImporter.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.modelix.model.sync.bulk
1818

1919
import mu.KotlinLogging
20+
import org.modelix.kotlin.utils.createMemoryEfficientMap
2021
import org.modelix.model.api.ConceptReference
2122
import org.modelix.model.api.INode
2223
import org.modelix.model.api.INodeReference
@@ -256,8 +257,8 @@ class ModelImporter(
256257
}
257258
}
258259

259-
private fun buildExistingIndex(): MemoryEfficientMap<String, INodeReference> {
260-
val localOriginalIdToExisting = MemoryEfficientMap<String, INodeReference>()
260+
private fun buildExistingIndex(): MutableMap<String, INodeReference> {
261+
val localOriginalIdToExisting = createMemoryEfficientMap<String, INodeReference>()
261262
root.getDescendants(true).forEach { node ->
262263
node.originalId()?.let { localOriginalIdToExisting[it] = node.reference }
263264
}

kotlin-utils/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ kotlin {
3030
}
3131
val jvmMain by getting {
3232
dependencies {
33+
implementation(libs.trove4j)
3334
}
3435
}
3536
val jvmTest by getting {
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,18 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.modelix.model.sync.bulk
17+
package org.modelix.kotlin.utils
1818

1919
/**
20-
* Built-in maps like [HashMap] are not the most memory efficient way of map.
21-
* A common issue is that entry objects for every item in the table are created.
22-
* [MemoryEfficientMap] is an internal implementation that we can use
23-
* when the memory overhead becomes too big.
20+
* Creates a mutable map with less memory overhead.
21+
* This is an internal API.
2422
*
23+
* Built-in maps like [HashMap] are not the not very memory efficient.
24+
* A common issue is that entry objects for every item in the table are created.
2525
* Java implementation is optimized to not create entry objects by using a map implementation from another library.
2626
* The JS implementation is not optimized yet because we did not invest time in finding a suitable library.
2727
*
28-
* [MemoryEfficientMap] is an internal abstraction.
29-
* The API is therefore kept minimal
28+
* We did not look into performance implications for storing and retrieving data.
29+
* Therefore, the memory efficient maps are used sparingly for only the very big maps.
3030
*/
31-
expect class MemoryEfficientMap<KeyT, ValueT>() {
32-
operator fun set(key: KeyT, value: ValueT)
33-
operator fun get(key: KeyT): ValueT?
34-
}
31+
expect fun <K, V> createMemoryEfficientMap(): MutableMap<K, V>
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.modelix.model.sync.bulk
17+
package org.modelix.kotlin.utils
1818

19-
import gnu.trove.map.TMap
20-
import gnu.trove.map.hash.THashMap
21-
22-
actual class MemoryEfficientMap<KeyT, ValueT> {
23-
private val map: TMap<KeyT, ValueT> = THashMap()
24-
25-
actual operator fun set(key: KeyT, value: ValueT) = map.set(key, value)
26-
27-
actual operator fun get(key: KeyT) = map[key]
28-
}
19+
actual fun <K, V> createMemoryEfficientMap(): MutableMap<K, V> = HashMap()
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,8 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.modelix.model.sync.bulk
17+
package org.modelix.kotlin.utils
1818

19-
actual class MemoryEfficientMap<KeyT, ValueT> {
20-
private val map: MutableMap<KeyT, ValueT> = mutableMapOf()
19+
import gnu.trove.map.hash.THashMap
2120

22-
actual operator fun set(key: KeyT, value: ValueT) = map.set(key, value)
23-
24-
actual operator fun get(key: KeyT) = map[key]
25-
}
21+
actual fun <K, V> createMemoryEfficientMap(): MutableMap<K, V> = THashMap()

model-datastructure/src/commonMain/kotlin/org/modelix/model/persistent/MapBasedStore.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
package org.modelix.model.persistent
1717

18+
import org.modelix.kotlin.utils.createMemoryEfficientMap
1819
import org.modelix.model.IKeyListener
1920
import org.modelix.model.IKeyValueStore
2021
import org.modelix.model.lazy.IBulkQuery
@@ -25,7 +26,7 @@ import org.modelix.model.lazy.NonBulkQuery
2526
open class MapBaseStore : MapBasedStore()
2627

2728
open class MapBasedStore : IKeyValueStore {
28-
private val map: MutableMap<String?, String?> = HashMap()
29+
private val map: MutableMap<String?, String?> = createMemoryEfficientMap()
2930
override fun get(key: String): String? {
3031
return map[key]
3132
}

0 commit comments

Comments
 (0)