Skip to content

Commit a7b0400

Browse files
committed
refactor: merge CL* and CP* classes
Reduces memory consumption and GC pressure.
1 parent 739934d commit a7b0400

File tree

19 files changed

+867
-795
lines changed

19 files changed

+867
-795
lines changed

model-api/src/commonMain/kotlin/org/modelix/model/api/ITree.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ interface ITree {
139139
*/
140140
fun setReferenceTarget(sourceId: Long, role: String, target: INodeReference?): ITree
141141

142+
fun setReferenceTarget(sourceId: Long, role: String, targetId: Long): ITree =
143+
setReferenceTarget(sourceId, role, LocalPNodeReference(targetId))
144+
142145
/**
143146
* Returns all reference roles for the given node in this tree.
144147
*

model-client/src/commonTest/kotlin/org/modelix/model/HamtTest.kt

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515

1616
package org.modelix.model
1717

18-
import org.modelix.model.lazy.CLHamtInternal
19-
import org.modelix.model.lazy.CLHamtNode
2018
import org.modelix.model.lazy.KVEntryReference
2119
import org.modelix.model.lazy.ObjectStoreCache
20+
import org.modelix.model.persistent.CPHamtInternal
21+
import org.modelix.model.persistent.CPHamtNode
2222
import org.modelix.model.persistent.CPNode
2323
import org.modelix.model.persistent.MapBaseStore
2424
import kotlin.random.Random
@@ -32,31 +32,31 @@ class HamtTest {
3232
val expectedMap: MutableMap<Long, Long> = HashMap()
3333
val store = MapBaseStore()
3434
val storeCache = ObjectStoreCache(store)
35-
var hamt: CLHamtNode? = CLHamtInternal.createEmpty(storeCache)
35+
var hamt: CPHamtNode? = CPHamtInternal.createEmpty()
3636
for (i in 0..999) {
3737
if (expectedMap.isEmpty() || rand.nextBoolean()) {
3838
// add entry
3939
val key = rand.nextInt(1000).toLong()
4040
val value = rand.nextLong()
41-
hamt = hamt!!.put(key, createEntry(value))
41+
hamt = hamt!!.put(key, createEntry(value), storeCache)
4242
expectedMap[key] = value
4343
} else {
4444
val keys: List<Long> = ArrayList(expectedMap.keys)
4545
val key = keys[rand.nextInt(keys.size)]
4646
if (rand.nextBoolean()) {
4747
// remove entry
48-
hamt = hamt!!.remove(key)
48+
hamt = hamt!!.remove(key, storeCache)
4949
expectedMap.remove(key)
5050
} else {
5151
// replace entry
5252
val value = rand.nextLong()
53-
hamt = hamt!!.put(key, createEntry(value))
53+
hamt = hamt!!.put(key, createEntry(value), storeCache)
5454
expectedMap[key] = value
5555
}
5656
}
5757
storeCache.clearCache()
5858
for ((key, value) in expectedMap) {
59-
assertEquals(value, hamt!![key]!!.getValue(storeCache).id)
59+
assertEquals(value, hamt!!.get(key, storeCache)!!.getValue(storeCache).id)
6060
}
6161
}
6262
}
@@ -79,22 +79,22 @@ class HamtTest {
7979
fun test_random_case_causing_outofbounds_on_js() {
8080
val store = MapBaseStore()
8181
val storeCache = ObjectStoreCache(store)
82-
var hamt: CLHamtNode? = CLHamtInternal.createEmpty(storeCache)
82+
var hamt: CPHamtNode? = CPHamtInternal.createEmpty()
8383
var getId = { e: KVEntryReference<CPNode>? -> e!!.getValue(storeCache).id }
8484

85-
hamt = hamt!!.put(965L, createEntry(-6579471327666419615))
86-
hamt = hamt!!.put(949L, createEntry(4912341421267007347))
87-
assertEquals(4912341421267007347, getId(hamt!![949L]))
88-
hamt = hamt!!.put(260L, createEntry(4166750678024106842))
89-
assertEquals(4166750678024106842, getId(hamt!![260L]))
90-
hamt = hamt!!.put(794L, createEntry(5492533034562136353))
91-
hamt = hamt!!.put(104L, createEntry(-6505928823483070382))
92-
hamt = hamt!!.put(47L, createEntry(3122507882718949737))
93-
hamt = hamt!!.put(693L, createEntry(-2086105010854963537))
85+
hamt = hamt!!.put(965L, createEntry(-6579471327666419615), storeCache)
86+
hamt = hamt!!.put(949L, createEntry(4912341421267007347), storeCache)
87+
assertEquals(4912341421267007347, getId(hamt!!.get(949L, storeCache)))
88+
hamt = hamt!!.put(260L, createEntry(4166750678024106842), storeCache)
89+
assertEquals(4166750678024106842, getId(hamt!!.get(260L, storeCache)))
90+
hamt = hamt!!.put(794L, createEntry(5492533034562136353), storeCache)
91+
hamt = hamt!!.put(104L, createEntry(-6505928823483070382), storeCache)
92+
hamt = hamt!!.put(47L, createEntry(3122507882718949737), storeCache)
93+
hamt = hamt!!.put(693L, createEntry(-2086105010854963537), storeCache)
9494
storeCache.clearCache()
9595
// assertEquals(69239088, (hamt!!.getData() as CPHamtInternal).bitmap)
9696
// assertEquals(6, (hamt!!.getData() as CPHamtInternal).children.count())
97-
assertEquals(-2086105010854963537, getId(hamt!![693L]))
97+
assertEquals(-2086105010854963537, getId(hamt!!.get(693L, storeCache)))
9898
}
9999

100100
/**
@@ -105,7 +105,7 @@ class HamtTest {
105105
@Test
106106
fun insertionOrderTest() {
107107
val store = ObjectStoreCache(MapBaseStore())
108-
val emptyMap = CLHamtInternal.createEmpty(store)
108+
val emptyMap = CPHamtInternal.createEmpty()
109109

110110
val rand = Random(123456789L)
111111
val entries = HashMap<Long, KVEntryReference<CPNode>>()
@@ -120,10 +120,10 @@ class HamtTest {
120120
var expectedHash: String? = null
121121

122122
for (i in 1..10) {
123-
var map: CLHamtNode = emptyMap
124-
entries.entries.shuffled(rand).forEach { map = map.put(it.key, it.value)!! }
125-
keysToRemove.forEach { map = map.remove(it)!! }
126-
val hash = map.getData().hash
123+
var map: CPHamtNode = emptyMap
124+
entries.entries.shuffled(rand).forEach { map = map.put(it.key, it.value, store)!! }
125+
keysToRemove.forEach { map = map.remove(it, store)!! }
126+
val hash = map.hash
127127
if (i == 1) {
128128
expectedHash = hash
129129
} else {

model-client/src/commonTest/kotlin/org/modelix/model/TreeDiffTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,22 @@ class TreeDiffTest {
9090

9191
private fun logicalDiff(oldTree: CLTree, newTree: CLTree): DiffData {
9292
val diffData: DiffData = DiffData()
93-
val newNodes = newTree.getDescendants(ITree.ROOT_ID, true).associateBy { it.id }
94-
val oldNodes = oldTree.getDescendants(ITree.ROOT_ID, true).associateBy { it.id }
93+
val newNodes = newTree.getDescendants(ITree.ROOT_ID, true).map { it.getData() }.associateBy { it.id }
94+
val oldNodes = oldTree.getDescendants(ITree.ROOT_ID, true).map { it.getData() }.associateBy { it.id }
9595

9696
for (newNode in newNodes.values) {
9797
val oldNode = oldNodes[newNode.id]
9898
if (oldNode == null) {
9999
diffData.addedNodes.add(newNode.id)
100100
} else {
101101
if (oldNode.roleInParent != newNode.roleInParent) diffData.changedContainments.add(newNode.id)
102-
for (role in (newNode.getData().propertyRoles + oldNode.getData().propertyRoles).distinct()) {
103-
if (newNode.getData().getPropertyValue(role) != oldNode.getData().getPropertyValue(role)) {
102+
for (role in (newNode.propertyRoles + oldNode.propertyRoles).distinct()) {
103+
if (newNode.getPropertyValue(role) != oldNode.getPropertyValue(role)) {
104104
diffData.changedRoles.add(RoleInNode(newNode.id, role))
105105
}
106106
}
107-
for (role in (newNode.getData().referenceRoles + oldNode.getData().referenceRoles).distinct()) {
108-
if (newNode.getData().getReferenceTarget(role) != oldNode.getData().getReferenceTarget(role)) {
107+
for (role in (newNode.referenceRoles + oldNode.referenceRoles).distinct()) {
108+
if (newNode.getReferenceTarget(role) != oldNode.getReferenceTarget(role)) {
109109
diffData.changedRoles.add(RoleInNode(newNode.id, role))
110110
}
111111
}

0 commit comments

Comments
 (0)