Skip to content

Commit

Permalink
Seal private nested classes to avoid unneeded virtual methods
Browse files Browse the repository at this point in the history
Fixes #19066

Also removed `GetTypeInfo` when it is not needed
  • Loading branch information
ajcvickers committed Nov 26, 2019
1 parent 93d5bfb commit f72f32e
Show file tree
Hide file tree
Showing 135 changed files with 316 additions and 301 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public virtual void BuildModel_AdventureWorks()
builder.Build(new SqlConnection(AdventureWorksFixture.ConnectionString));
}

private class ColdStartEnabledTests : MarshalByRefObject
private sealed class ColdStartEnabledTests : MarshalByRefObject
{
public virtual void CreateAndDisposeUnusedContext(int count)
public void CreateAndDisposeUnusedContext(int count)
{
for (var i = 0; i < count; i++)
{
Expand All @@ -80,7 +80,7 @@ public virtual void CreateAndDisposeUnusedContext(int count)
}
}

public virtual void InitializeAndQuery_AdventureWorks(int count)
public void InitializeAndQuery_AdventureWorks(int count)
{
for (var i = 0; i < count; i++)
{
Expand All @@ -91,7 +91,7 @@ public virtual void InitializeAndQuery_AdventureWorks(int count)
}
}

public virtual void InitializeAndSaveChanges_AdventureWorks(int count)
public void InitializeAndSaveChanges_AdventureWorks(int count)
{
for (var i = 0; i < count; i++)
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private static TService GetService<TService>(IInfrastructure<IServiceProvider> d
/// <returns> <c>true</c> if the Cosmos provider is being used. </returns>
public static bool IsCosmos([NotNull] this DatabaseFacade database)
=> database.ProviderName.Equals(
typeof(CosmosOptionsExtension).GetTypeInfo().Assembly.GetName().Name,
typeof(CosmosOptionsExtension).Assembly.GetName().Name,
StringComparison.Ordinal);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal
{
public partial class CosmosShapedQueryCompilingExpressionVisitor
{
private class CosmosProjectionBindingRemovingExpressionVisitor : ExpressionVisitor
private sealed class CosmosProjectionBindingRemovingExpressionVisitor : ExpressionVisitor
{
private static readonly MethodInfo _getItemMethodInfo
= typeof(JObject).GetTypeInfo().GetRuntimeProperties()
= typeof(JObject).GetRuntimeProperties()
.Single(pi => pi.Name == "Item" && pi.GetIndexParameters()[0].ParameterType == typeof(string))
.GetMethod;

private static readonly PropertyInfo _jTokenTypePropertyInfo
= typeof(JToken).GetTypeInfo().GetRuntimeProperties()
= typeof(JToken).GetRuntimeProperties()
.Single(mi => mi.Name == nameof(JToken.Type));

private static readonly MethodInfo _jTokenToObjectMethodInfo
= typeof(JToken).GetTypeInfo().GetRuntimeMethods()
= typeof(JToken).GetRuntimeMethods()
.Single(mi => mi.Name == nameof(JToken.ToObject) && mi.GetParameters().Length == 0);

private static readonly MethodInfo _toObjectMethodInfo
= typeof(CosmosProjectionBindingRemovingExpressionVisitor).GetTypeInfo().GetRuntimeMethods()
= typeof(CosmosProjectionBindingRemovingExpressionVisitor).GetRuntimeMethods()
.Single(mi => mi.Name == nameof(SafeToObject));

private static readonly MethodInfo _collectionAccessorAddMethodInfo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal
{
public partial class CosmosShapedQueryCompilingExpressionVisitor
{
private class InExpressionValuesExpandingExpressionVisitor : ExpressionVisitor
private sealed class InExpressionValuesExpandingExpressionVisitor : ExpressionVisitor
{
private readonly ISqlExpressionFactory _sqlExpressionFactory;
private readonly IReadOnlyDictionary<string, object> _parametersValues;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal
{
public partial class CosmosShapedQueryCompilingExpressionVisitor
{
private class JObjectInjectingExpressionVisitor : ExpressionVisitor
private sealed class JObjectInjectingExpressionVisitor : ExpressionVisitor
{
private int _currentEntityIndex;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal
{
public partial class CosmosShapedQueryCompilingExpressionVisitor
{
private class QueryingEnumerable<T> : IEnumerable<T>, IAsyncEnumerable<T>
private sealed class QueryingEnumerable<T> : IEnumerable<T>, IAsyncEnumerable<T>
{
private readonly CosmosQueryContext _cosmosQueryContext;
private readonly ISqlExpressionFactory _sqlExpressionFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public virtual SqlExpression Translate(Expression expression)
return null;
}

private class SqlTypeMappingVerifyingExpressionVisitor : ExpressionVisitor
private sealed class SqlTypeMappingVerifyingExpressionVisitor : ExpressionVisitor
{
protected override Expression VisitExtension(Expression node)
{
Expand Down
8 changes: 4 additions & 4 deletions src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ private FeedIterator CreateQuery(
return container.GetItemQueryStreamIterator(queryDefinition);
}

private class DocumentEnumerable : IEnumerable<JObject>
private sealed class DocumentEnumerable : IEnumerable<JObject>
{
private readonly CosmosClientWrapper _cosmosClient;
private readonly string _containerId;
Expand All @@ -472,7 +472,7 @@ public DocumentEnumerable(

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

private class Enumerator : IEnumerator<JObject>
private sealed class Enumerator : IEnumerator<JObject>
{
private FeedIterator _query;
private ResponseMessage _responseMessage;
Expand Down Expand Up @@ -570,7 +570,7 @@ public void Dispose()
}
}

private class DocumentAsyncEnumerable : IAsyncEnumerable<JObject>
private sealed class DocumentAsyncEnumerable : IAsyncEnumerable<JObject>
{
private readonly CosmosClientWrapper _cosmosClient;
private readonly string _containerId;
Expand All @@ -589,7 +589,7 @@ public DocumentAsyncEnumerable(
public IAsyncEnumerator<JObject> GetAsyncEnumerator(CancellationToken cancellationToken = default)
=> new AsyncEnumerator(this, cancellationToken);

private class AsyncEnumerator : IAsyncEnumerator<JObject>
private sealed class AsyncEnumerator : IAsyncEnumerator<JObject>
{
private FeedIterator _query;
private ResponseMessage _responseMessage;
Expand Down
12 changes: 6 additions & 6 deletions src/EFCore.Design/Design/Internal/DbContextOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ private IDictionary<Type, Func<DbContext>> FindContextTypes()
// Look for IDesignTimeDbContextFactory implementations
_reporter.WriteVerbose(DesignStrings.FindingContextFactories);
var contextFactories = _startupAssembly.GetConstructibleTypes()
.Where(t => typeof(IDesignTimeDbContextFactory<DbContext>).GetTypeInfo().IsAssignableFrom(t));
.Where(t => typeof(IDesignTimeDbContextFactory<DbContext>).IsAssignableFrom(t));
foreach (var factory in contextFactories)
{
_reporter.WriteVerbose(DesignStrings.FoundContextFactory(factory.ShortDisplayName()));
var manufacturedContexts =
from i in factory.ImplementedInterfaces
where i.GetTypeInfo().IsGenericType
where i.IsGenericType
&& i.GetGenericTypeDefinition() == typeof(IDesignTimeDbContextFactory<>)
select i.GenericTypeArguments[0];
foreach (var context in manufacturedContexts)
Expand Down Expand Up @@ -188,10 +188,10 @@ where i.GetTypeInfo().IsGenericType
var types = _startupAssembly.GetConstructibleTypes()
.Concat(_assembly.GetConstructibleTypes())
.ToList();
var contextTypes = types.Where(t => typeof(DbContext).GetTypeInfo().IsAssignableFrom(t)).Select(
var contextTypes = types.Where(t => typeof(DbContext).IsAssignableFrom(t)).Select(
t => t.AsType())
.Concat(
types.Where(t => typeof(Migration).GetTypeInfo().IsAssignableFrom(t))
types.Where(t => typeof(Migration).IsAssignableFrom(t))
.Select(t => t.GetCustomAttribute<DbContextAttribute>()?.ContextType)
.Where(t => t != null))
.Distinct();
Expand Down Expand Up @@ -242,8 +242,8 @@ public virtual ContextInfo GetContextInfo([CanBeNull] string contextType)

private Func<DbContext> FindContextFactory(Type contextType)
{
var factoryInterface = typeof(IDesignTimeDbContextFactory<>).MakeGenericType(contextType).GetTypeInfo();
var factory = contextType.GetTypeInfo().Assembly.GetConstructibleTypes()
var factoryInterface = typeof(IDesignTimeDbContextFactory<>).MakeGenericType(contextType);
var factory = contextType.Assembly.GetConstructibleTypes()
.FirstOrDefault(t => factoryInterface.IsAssignableFrom(t));
return factory == null ? (Func<DbContext>)null : (() => CreateContextFromFactory(factory.AsType()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private void ConfigureUserServices(IServiceCollection services)
_reporter.WriteVerbose(DesignStrings.FindingDesignTimeServices(_startupAssembly.GetName().Name));

var designTimeServicesType = _startupAssembly.GetLoadableDefinedTypes()
.Where(t => typeof(IDesignTimeServices).GetTypeInfo().IsAssignableFrom(t)).Select(t => t.AsType())
.Where(t => typeof(IDesignTimeServices).IsAssignableFrom(t)).Select(t => t.AsType())
.FirstOrDefault();
if (designTimeServicesType == null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Design/Design/Internal/MigrationsOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private void EnsureMigrationsAssembly(IServiceProvider services)
var options = services.GetRequiredService<IDbContextOptions>();
var contextType = services.GetRequiredService<ICurrentDbContext>().Context.GetType();
var migrationsAssemblyName = RelationalOptionsExtension.Extract(options).MigrationsAssembly
?? contextType.GetTypeInfo().Assembly.GetName().Name;
?? contextType.Assembly.GetName().Name;
if (assemblyName.Name != migrationsAssemblyName
&& assemblyName.FullName != migrationsAssemblyName)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public SnapshotModelProcessor([NotNull] IOperationReporter operationReporter)
_operationReporter = operationReporter;
_relationalNames = new HashSet<string>(
typeof(RelationalAnnotationNames)
.GetTypeInfo()
.GetRuntimeFields()
.Where(p => p.Name != nameof(RelationalAnnotationNames.Prefix))
.Select(p => ((string)p.GetValue(null)).Substring(RelationalAnnotationNames.Prefix.Length - 1)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ private void GenerateInversePropertyAttribute(INavigation navigation)
}
}

private class AttributeWriter
private sealed class AttributeWriter
{
private readonly string _attributeName;
private readonly List<string> _parameters = new List<string>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static class InMemoryDatabaseFacadeExtensions
/// <returns> <c>true</c> if the in-memory database is being used. </returns>
public static bool IsInMemory([NotNull] this DatabaseFacade database)
=> database.ProviderName.Equals(
typeof(InMemoryOptionsExtension).GetTypeInfo().Assembly.GetName().Name,
typeof(InMemoryOptionsExtension).Assembly.GetName().Name,
StringComparison.Ordinal);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public InMemoryExpressionTranslatingExpressionVisitor(
_entityProjectionFindingExpressionVisitor = new EntityProjectionFindingExpressionVisitor();
}

private class EntityProjectionFindingExpressionVisitor : ExpressionVisitor
private sealed class EntityProjectionFindingExpressionVisitor : ExpressionVisitor
{
private bool _found;

Expand Down
6 changes: 3 additions & 3 deletions src/EFCore.InMemory/Query/Internal/InMemoryQueryExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private static readonly ConstructorInfo _valueBufferConstructor
= typeof(ValueBuffer).GetConstructors().Single(ci => ci.GetParameters().Length == 1);

private static readonly PropertyInfo _valueBufferCountMemberInfo
= typeof(ValueBuffer).GetTypeInfo().GetProperty(nameof(ValueBuffer.Count));
= typeof(ValueBuffer).GetProperty(nameof(ValueBuffer.Count));

private readonly List<Expression> _valueBufferSlots = new List<Expression>();

Expand Down Expand Up @@ -157,7 +157,7 @@ public virtual int AddSubqueryProjection(ShapedQueryExpression shapedQueryExpres
return AddToProjection(serverQueryExpression);
}

private class ShaperRemappingExpressionVisitor : ExpressionVisitor
private sealed class ShaperRemappingExpressionVisitor : ExpressionVisitor
{
private readonly IDictionary<ProjectionMember, Expression> _projectionMapping;

Expand Down Expand Up @@ -941,7 +941,7 @@ public virtual void Print(ExpressionPrinter expressionPrinter)
}
}

private class NullableReadValueExpressionVisitor : ExpressionVisitor
private sealed class NullableReadValueExpressionVisitor : ExpressionVisitor
{
protected override Expression VisitMethodCall(MethodCallExpression methodCallExpression)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ protected override ShapedQueryExpression TranslateSelectMany(
return null;
}

private class DefaultIfEmptyFindingExpressionVisitor : ExpressionVisitor
private sealed class DefaultIfEmptyFindingExpressionVisitor : ExpressionVisitor
{
private bool _defaultIfEmpty;

Expand Down Expand Up @@ -801,7 +801,7 @@ private Expression RemapLambdaBody(ShapedQueryExpression shapedQueryExpression,
internal Expression ExpandWeakEntities(InMemoryQueryExpression queryExpression, Expression lambdaBody)
=> _weakEntityExpandingExpressionVisitor.Expand(queryExpression, lambdaBody);

private class WeakEntityExpandingExpressionVisitor : ExpressionVisitor
private sealed class WeakEntityExpandingExpressionVisitor : ExpressionVisitor
{
private InMemoryQueryExpression _queryExpression;
private readonly InMemoryExpressionTranslatingExpressionVisitor _expressionTranslator;
Expand All @@ -811,7 +811,7 @@ public WeakEntityExpandingExpressionVisitor(InMemoryExpressionTranslatingExpress
_expressionTranslator = expressionTranslator;
}

public virtual Expression Expand(InMemoryQueryExpression queryExpression, Expression lambdaBody)
public Expression Expand(InMemoryQueryExpression queryExpression, Expression lambdaBody)
{
_queryExpression = queryExpression;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.EntityFrameworkCore.InMemory.Query.Internal
{
public partial class InMemoryShapedQueryCompilingExpressionVisitor
{
private class CustomShaperCompilingExpressionVisitor : ExpressionVisitor
private sealed class CustomShaperCompilingExpressionVisitor : ExpressionVisitor
{
private readonly bool _tracking;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.EntityFrameworkCore.InMemory.Query.Internal
{
public partial class InMemoryShapedQueryCompilingExpressionVisitor
{
private class InMemoryProjectionBindingRemovingExpressionVisitor : ExpressionVisitor
private sealed class InMemoryProjectionBindingRemovingExpressionVisitor : ExpressionVisitor
{
private readonly IDictionary<ParameterExpression, (IDictionary<IProperty, int> IndexMap, ParameterExpression valueBuffer)>
_materializationContextBindings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Microsoft.EntityFrameworkCore.InMemory.Query.Internal
{
public partial class InMemoryShapedQueryCompilingExpressionVisitor
{
private class QueryingEnumerable<T> : IAsyncEnumerable<T>, IEnumerable<T>
private sealed class QueryingEnumerable<T> : IAsyncEnumerable<T>, IEnumerable<T>
{
private readonly QueryContext _queryContext;
private readonly IEnumerable<ValueBuffer> _innerEnumerable;
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Metadata/Internal/Sequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ void IConventionSequence.SetClrType(Type clrType, bool fromDataAnnotation)
void IConventionSequence.SetIsCyclic(bool? cyclic, bool fromDataAnnotation)
=> SetIsCyclic(cyclic, fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention);

private class SequenceData
private sealed class SequenceData
{
public string Name { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ public static class MigrationExtensions
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public static string GetId([NotNull] this Migration migration)
=> migration.GetType().GetTypeInfo().GetCustomAttribute<MigrationAttribute>()?.Id;
=> migration.GetType().GetCustomAttribute<MigrationAttribute>()?.Id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public MigrationsAssembly(

var assemblyName = RelationalOptionsExtension.Extract(options)?.MigrationsAssembly;
Assembly = assemblyName == null
? _contextType.GetTypeInfo().Assembly
? _contextType.Assembly
: Assembly.Load(new AssemblyName(assemblyName));

_idGenerator = idGenerator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ private static IEnumerable<IProperty> GetSortedProperties(IEntityType entityType
.Concat(entityType.GetDirectlyDerivedTypes().SelectMany(GetSortedProperties));
}

private class PropertyInfoEqualityComparer : IEqualityComparer<PropertyInfo>
private sealed class PropertyInfoEqualityComparer : IEqualityComparer<PropertyInfo>
{
private PropertyInfoEqualityComparer()
{
Expand Down Expand Up @@ -2236,7 +2236,7 @@ private static ReferentialAction ToReferentialAction(DeleteBehavior deleteBehavi
return result;
}

private class EntryMapping
private sealed class EntryMapping
{
public HashSet<IUpdateEntry> SourceEntries { get; } = new HashSet<IUpdateEntry>();
public HashSet<IUpdateEntry> TargetEntries { get; } = new HashSet<IUpdateEntry>();
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Relational/Query/Internal/BufferedDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ public override Task<bool> ReadAsync(CancellationToken cancellationToken)
return _currentResultSet.ReadAsync(cancellationToken);
}

private class BufferedDataRecord
private sealed class BufferedDataRecord
{
private int _currentRowNumber = -1;
private int _rowCount;
Expand Down
Loading

0 comments on commit f72f32e

Please sign in to comment.