Skip to content

Commit 22b4384

Browse files
committed
perf(model-api): INode.tryResolveChildLink was slow on the model server
The meta-model is not available on the model server and INode.concept was throwing exceptions that where caught in .tryResolveChildLink, but because the method is called a lot the overhead of these exceptions was significant.
1 parent 4ecbbc9 commit 22b4384

File tree

3 files changed

+11
-16
lines changed

3 files changed

+11
-16
lines changed

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

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ interface INode {
4444
*/
4545
val concept: IConcept?
4646

47+
fun tryGetConcept(): IConcept? = getConceptReference()?.tryResolve()
48+
4749
/**
4850
* Role of this node in its parent node if it exists,or null otherwise.
4951
*/
@@ -329,11 +331,7 @@ fun INode.resolveProperty(role: String): IProperty {
329331
* or null, if this concept has no child link or an exception was thrown during concept resolution
330332
*/
331333
fun INode.tryResolveChildLink(role: String): IChildLink? {
332-
val c = try {
333-
this.concept ?: return null
334-
} catch (e: RuntimeException) {
335-
return null
336-
}
334+
val c = this.tryGetConcept() ?: return null
337335
val allLinks = c.getAllChildLinks()
338336
return allLinks.find { it.key(this) == role }
339337
?: allLinks.find { it.getSimpleName() == role }
@@ -350,11 +348,7 @@ fun INode.resolveChildLinkOrFallback(role: String?): IChildLink {
350348
* or null, if this node has no reference link or an exception was thrown during concept resolution
351349
*/
352350
fun INode.tryResolveReferenceLink(role: String): IReferenceLink? {
353-
val c = try {
354-
this.concept ?: return null
355-
} catch (e: RuntimeException) {
356-
return null
357-
}
351+
val c = this.tryGetConcept() ?: return null
358352
val allLinks = c.getAllReferenceLinks()
359353
return allLinks.find { it.key(this) == role }
360354
?: allLinks.find { it.getSimpleName() == role }
@@ -370,11 +364,7 @@ fun INode.resolveReferenceLinkOrFallback(role: String): IReferenceLink {
370364
* or null, if this node has no concept or an exception was thrown during concept resolution
371365
*/
372366
fun INode.tryResolveProperty(role: String): IProperty? {
373-
val c = try {
374-
this.concept ?: return null
375-
} catch (e: RuntimeException) {
376-
return null
377-
}
367+
val c = this.tryGetConcept() ?: return null
378368
val allLinks = c.getAllProperties()
379369
return allLinks.find { it.key(this) == role }
380370
?: allLinks.find { it.getSimpleName() == role }
@@ -417,4 +407,3 @@ fun INode.getContainmentLink() = if (this is INodeEx) {
417407
fun INode.getRoot(): INode = parent?.getRoot() ?: this
418408
fun INode.isInstanceOf(superConcept: IConcept?): Boolean = concept.let { it != null && it.isSubConceptOf(superConcept) }
419409
fun INode.isInstanceOfSafe(superConcept: IConcept): Boolean = tryGetConcept()?.isSubConceptOf(superConcept) ?: false
420-
fun INode.tryGetConcept(): IConcept? = getConceptReference()?.tryResolve()

mps-model-adapters/src/main/kotlin/org/modelix/model/mpsadapters/IDefaultNodeAdapter.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ interface IDefaultNodeAdapter : IDeprecatedNodeDefaults {
3232
override val isValid: Boolean
3333
get() = true
3434

35+
override fun tryGetConcept(): IConcept? = concept
36+
3537
override fun getConceptReference(): IConceptReference? {
3638
return concept?.getReference()
3739
}

mps-model-adapters/src/main/kotlin/org/modelix/model/mpsadapters/MPSNode.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ data class MPSNode(val node: SNode) : IDeprecatedNodeDefaults {
4444
override val parent: INode?
4545
get() = node.parent?.let { MPSNode(it) } ?: node.model?.let { MPSModelAsNode(it) }
4646

47+
override fun tryGetConcept(): IConcept {
48+
return MPSConcept(node.concept)
49+
}
50+
4751
override fun getConceptReference(): ConceptReference {
4852
return concept.getReference() as ConceptReference
4953
}

0 commit comments

Comments
 (0)