From 5f7e5d99ce5e8d06c517ea19f1d444645f95b521 Mon Sep 17 00:00:00 2001 From: CyrilFerlicot Date: Tue, 14 May 2024 16:42:35 +0200 Subject: [PATCH] Speed up #source and #target on associations It is common to manipulate the sources and targets of associations in moose but in case we need them extensively it can take some time to get them because we compute them from the meta description. In this change I'm adding a cache to get the source and target selectors which speed up quite a bit the code. I had a piece of code taking 200ms that got down to 28ms with this change. Out of the 28ms, only 2ms are now spent in getting the source and target --- src/Famix-Traits/FamixTAssociation.trait.st | 12 ------- src/Moose-Core/Object.extension.st | 9 +++++ .../TAssociationMetaLevelDependency.trait.st | 36 +++++++++++++++++++ 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/Famix-Traits/FamixTAssociation.trait.st b/src/Famix-Traits/FamixTAssociation.trait.st index 5ac4592e..a672f289 100644 --- a/src/Famix-Traits/FamixTAssociation.trait.st +++ b/src/Famix-Traits/FamixTAssociation.trait.st @@ -146,12 +146,6 @@ FamixTAssociation >> source: aSource [ self perform: (self sourceSelector , ':') with: aSource ] -{ #category : #accessing } -FamixTAssociation >> sourceSelector [ - ^ (self mooseDescription allComplexProperties detect: #isSource) - implementingSelector -] - { #category : #accessing } FamixTAssociation >> target [ ^ self targetSelector value: self @@ -161,9 +155,3 @@ FamixTAssociation >> target [ FamixTAssociation >> target: aTarget [ self perform: (self targetSelector , ':') with: aTarget ] - -{ #category : #accessing } -FamixTAssociation >> targetSelector [ - ^ (self mooseDescription allComplexProperties detect: #isTarget) - implementingSelector -] diff --git a/src/Moose-Core/Object.extension.st b/src/Moose-Core/Object.extension.st index 5d759b9a..f129551b 100644 --- a/src/Moose-Core/Object.extension.st +++ b/src/Moose-Core/Object.extension.st @@ -1,5 +1,14 @@ Extension { #name : #Object } +{ #category : #'*Moose-Core' } +Object >> allComplexPropertiesIn: aMetamodel [ + "All complex properties described in the metamodel. + A complex property is a property whose type is not a primitive." + + aMetamodel ifNil: [ ^ OrderedCollection new ]. + ^ aMetamodel descriptionOf: self class instanceSide ifPresent: #allComplexProperties ifAbsent: [ OrderedCollection new ] +] + { #category : #'*Moose-Core' } Object >> allDeclaredPropertiesIn: aMetamodel [ "All properties described in the metamodel" diff --git a/src/Moose-Query/TAssociationMetaLevelDependency.trait.st b/src/Moose-Query/TAssociationMetaLevelDependency.trait.st index 8ce5d472..94da6f8d 100644 --- a/src/Moose-Query/TAssociationMetaLevelDependency.trait.st +++ b/src/Moose-Query/TAssociationMetaLevelDependency.trait.st @@ -17,6 +17,12 @@ TAssociationMetaLevelDependency classSide >> annotation [ ^ self ] +{ #category : #accessing } +TAssociationMetaLevelDependency classSide >> privateSourceSelectorIn: aMetamodel [ + + ^ ((self allComplexPropertiesIn: aMetamodel) detect: #isSource) implementingSelector +] + { #category : #private } TAssociationMetaLevelDependency classSide >> privateSourceTypesIn: aMetamodel [ "I return the classes that could be my source" @@ -26,6 +32,12 @@ TAssociationMetaLevelDependency classSide >> privateSourceTypesIn: aMetamodel [ thenCollect: #implementingType ] +{ #category : #accessing } +TAssociationMetaLevelDependency classSide >> privateTargetSelectorIn: aMetamodel [ + + ^ ((self allComplexPropertiesIn: aMetamodel) detect: #isTarget) implementingSelector +] + { #category : #private } TAssociationMetaLevelDependency classSide >> privateTargetTypesIn: aMetamodel [ "I return the classes that could be my target" @@ -35,6 +47,12 @@ TAssociationMetaLevelDependency classSide >> privateTargetTypesIn: aMetamodel [ thenCollect: #implementingType ] +{ #category : #accessing } +TAssociationMetaLevelDependency classSide >> sourceSelectorIn: aMetamodel [ + + ^ aMetamodel additionalProperty: #sourceSelector for: self ifAbsentPut: [ self privateSourceSelectorIn: aMetamodel ] +] + { #category : #accessing } TAssociationMetaLevelDependency classSide >> sourceTypesIn: aMetamodel [ @@ -42,6 +60,12 @@ TAssociationMetaLevelDependency classSide >> sourceTypesIn: aMetamodel [ ] +{ #category : #accessing } +TAssociationMetaLevelDependency classSide >> targetSelectorIn: aMetamodel [ + + ^ aMetamodel additionalProperty: #targetSelector for: self ifAbsentPut: [ self privateTargetSelectorIn: aMetamodel ] +] + { #category : #accessing } TAssociationMetaLevelDependency classSide >> targetTypesIn: aMetamodel [ @@ -49,11 +73,23 @@ TAssociationMetaLevelDependency classSide >> targetTypesIn: aMetamodel [ ] +{ #category : #accessing } +TAssociationMetaLevelDependency >> sourceSelector [ + + ^ self class sourceSelectorIn: self metamodel +] + { #category : #accessing } TAssociationMetaLevelDependency >> sourceTypes [ ^ self class sourceTypesIn: self metamodel ] +{ #category : #accessing } +TAssociationMetaLevelDependency >> targetSelector [ + + ^ self class targetSelectorIn: self metamodel +] + { #category : #accessing } TAssociationMetaLevelDependency >> targetTypes [ ^ self class targetTypesIn: self metamodel