Skip to content

Commit

Permalink
Refactored GetConstraints to use IEnumerable to reduce allocations
Browse files Browse the repository at this point in the history
Also made configurationSource a private field
  • Loading branch information
Muppets committed May 14, 2019
1 parent a1d83c4 commit 67c8e9b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ public static bool RemoveCheckConstraint(
/// Returns all <see cref="ICheckConstraint" /> contained in the entity type.
/// </summary>
/// <param name="entityType"> The entity type to get the check constraints for. </param>
public static IReadOnlyList<ICheckConstraint> GetCheckConstraints([NotNull] this IEntityType entityType)
public static IEnumerable<ICheckConstraint> GetCheckConstraints([NotNull] this IEntityType entityType)
=> CheckConstraint.GetCheckConstraints(entityType);
}
}
22 changes: 8 additions & 14 deletions src/EFCore.Relational/Metadata/Internal/CheckConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace Microsoft.EntityFrameworkCore.Metadata.Internal
/// </summary>
public class CheckConstraint : IConventionCheckConstraint
{
private ConfigurationSource _configurationSource;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand All @@ -38,7 +40,7 @@ public CheckConstraint(
EntityType = entityType;
Name = name;
Sql = sql;
ConfigurationSource = configurationSource;
_configurationSource = configurationSource;

var dataDictionary = GetAnnotationsDictionary(EntityType);
if (dataDictionary == null)
Expand All @@ -63,11 +65,11 @@ public CheckConstraint(
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static IReadOnlyList<CheckConstraint> GetCheckConstraints([NotNull] IEntityType entityType)
public static IEnumerable<CheckConstraint> GetCheckConstraints([NotNull] IEntityType entityType)
{
Check.NotNull(entityType, nameof(entityType));

return GetAnnotationsDictionary(entityType)?.Values.ToList() ?? new List<CheckConstraint>();
return GetAnnotationsDictionary(entityType)?.Values ?? Enumerable.Empty<CheckConstraint>();
}

/// <summary>
Expand Down Expand Up @@ -133,15 +135,7 @@ public static bool RemoveCheckConstraint(
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual ConfigurationSource ConfigurationSource { get; private set; }

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual ConfigurationSource GetConfigurationSource() => ConfigurationSource;
public virtual ConfigurationSource GetConfigurationSource() => _configurationSource;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -150,8 +144,8 @@ public static bool RemoveCheckConstraint(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual void UpdateConfigurationSource(ConfigurationSource configurationSource)
{
ConfigurationSource = configurationSource.Max(ConfigurationSource);
{
_configurationSource = configurationSource.Max(_configurationSource);
}

private static Dictionary<string, CheckConstraint> GetAnnotationsDictionary(IEntityType entityType)
Expand Down

0 comments on commit 67c8e9b

Please sign in to comment.