Skip to content
This repository has been archived by the owner on Nov 29, 2017. It is now read-only.

Commit

Permalink
Performance: DbDatabaseMappingExtensions.GetEntityTypeMapping was spe…
Browse files Browse the repository at this point in the history
…nding a sizeable amount of time doing Linq to Objects in this method. A simple rewrite to use simple iterators helps boosting up the performance.
  • Loading branch information
unknown authored and ajcvickers committed Apr 24, 2013
1 parent 8fcbe42 commit c4401df
Showing 1 changed file with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,20 @@ public static IEnumerable<StorageEntityTypeMapping> GetEntityTypeMappings(
DebugCheck.NotNull(databaseMapping);
DebugCheck.NotNull(entityType);

return (from esm in databaseMapping.EntityContainerMappings.Single().EntitySetMappings
from etm in esm.EntityTypeMappings
where etm.EntityType == entityType
select etm);
// please don't convert this section of code to a Linq expression since
// it is performance sensitive, especially for larger models.
var mappings = new List<StorageEntityTypeMapping>();
foreach (var esm in databaseMapping.EntityContainerMappings.Single().EntitySetMappings)
{
foreach (var etm in esm.EntityTypeMappings)
{
if (etm.EntityType == entityType)
{
mappings.Add(etm);
}
}
}
return mappings;
}

public static StorageEntityTypeMapping GetEntityTypeMapping(
Expand All @@ -110,14 +120,23 @@ public static StorageEntityTypeMapping GetEntityTypeMapping(
DebugCheck.NotNull(databaseMapping);
DebugCheck.NotNull(entityType);

var mappings = (from esm in databaseMapping.EntityContainerMappings.Single().EntitySetMappings
from etm in esm.EntityTypeMappings
where etm.GetClrType() == entityType
select etm);
// please don't convert this section of code to a Linq expression since
// it is performance sensitive, especially for larger models.
var mappings = new List<StorageEntityTypeMapping>();
foreach (var esm in databaseMapping.EntityContainerMappings.Single().EntitySetMappings)
{
foreach (var etm in esm.EntityTypeMappings)
{
if (etm.GetClrType() == entityType)
{
mappings.Add(etm);
}
}
}

if (mappings.Count() <= 1)
if (mappings.Count <= 1)
{
return mappings.SingleOrDefault();
return mappings.FirstOrDefault();
}

// Return the property mapping
Expand Down

0 comments on commit c4401df

Please sign in to comment.