From 1f67bb99ebd5a4fc55f3ebb1dfcee9bf67819925 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Habinshuti?= Date: Thu, 30 Nov 2023 15:03:35 +0300 Subject: [PATCH] Slight performance improvement for `IEdmNavigationSource.EntityType()` (#2808) * Simplify EntityType() method * Optimize Type property in CsdlSemanticsEntitySet and Singleton --- .../Csdl/Semantics/CsdlSemanticsEntitySet.cs | 12 +++++++- .../Csdl/Semantics/CsdlSemanticsSingleton.cs | 12 +++++++- .../ExtensionMethods/ExtensionMethods.cs | 29 +------------------ 3 files changed, 23 insertions(+), 30 deletions(-) diff --git a/src/Microsoft.OData.Edm/Csdl/Semantics/CsdlSemanticsEntitySet.cs b/src/Microsoft.OData.Edm/Csdl/Semantics/CsdlSemanticsEntitySet.cs index a5e0f77639..f9c66e98c9 100644 --- a/src/Microsoft.OData.Edm/Csdl/Semantics/CsdlSemanticsEntitySet.cs +++ b/src/Microsoft.OData.Edm/Csdl/Semantics/CsdlSemanticsEntitySet.cs @@ -19,9 +19,19 @@ public CsdlSemanticsEntitySet(CsdlSemanticsEntityContainer container, CsdlEntity { } + private IEdmType type; + public override IEdmType Type { - get { return new EdmCollectionType(new EdmEntityTypeReference(this.typeCache.GetValue(this, ComputeElementTypeFunc, null), false)); } + get + { + if (type == null) + { + type = new EdmCollectionType(new EdmEntityTypeReference(this.typeCache.GetValue(this, ComputeElementTypeFunc, null), false)); + } + + return type; + } } public override EdmContainerElementKind ContainerElementKind diff --git a/src/Microsoft.OData.Edm/Csdl/Semantics/CsdlSemanticsSingleton.cs b/src/Microsoft.OData.Edm/Csdl/Semantics/CsdlSemanticsSingleton.cs index 04e8dfd0bc..bdb7589fbb 100644 --- a/src/Microsoft.OData.Edm/Csdl/Semantics/CsdlSemanticsSingleton.cs +++ b/src/Microsoft.OData.Edm/Csdl/Semantics/CsdlSemanticsSingleton.cs @@ -19,9 +19,19 @@ public CsdlSemanticsSingleton(CsdlSemanticsEntityContainer container, CsdlSingle { } + private IEdmType type; + public override IEdmType Type { - get { return this.typeCache.GetValue(this, ComputeElementTypeFunc, null); } + get + { + if (type == null) + { + type = this.typeCache.GetValue(this, ComputeElementTypeFunc, null); + } + + return type; + } } public override EdmContainerElementKind ContainerElementKind diff --git a/src/Microsoft.OData.Edm/ExtensionMethods/ExtensionMethods.cs b/src/Microsoft.OData.Edm/ExtensionMethods/ExtensionMethods.cs index a69c3bf04e..8c5cb2ca96 100644 --- a/src/Microsoft.OData.Edm/ExtensionMethods/ExtensionMethods.cs +++ b/src/Microsoft.OData.Edm/ExtensionMethods/ExtensionMethods.cs @@ -2822,34 +2822,7 @@ public static string FullNavigationSourceName(this IEdmNavigationSource navigati /// The entity type of the navigation source. public static IEdmEntityType EntityType(this IEdmNavigationSource navigationSource) { - var entitySetBase = navigationSource as IEdmEntitySetBase; - if (entitySetBase != null) - { - IEdmCollectionType collectionType = entitySetBase.Type as IEdmCollectionType; - - if (collectionType != null) - { - return collectionType.ElementType.Definition as IEdmEntityType; - } - - var unknownEntitySet = entitySetBase as IEdmUnknownEntitySet; - if (unknownEntitySet != null) - { - // Handle missing navigation target for nullable - // singleton navigation property. - return unknownEntitySet.Type as IEdmEntityType; - } - - return null; - } - - var singleton = navigationSource as IEdmSingleton; - if (singleton != null) - { - return singleton.Type as IEdmEntityType; - } - - return null; + return navigationSource?.Type.AsElementType() as IEdmEntityType; } #endregion