Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No boxing for structs #22030

Merged
merged 7 commits into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand Down Expand Up @@ -117,7 +118,7 @@ void IPrintableExpression.Print(ExpressionPrinter expressionPrinter)
}
}

private readonly struct CommandCacheKey
private readonly struct CommandCacheKey : IEquatable<CommandCacheKey>
{
private readonly SelectExpression _selectExpression;
private readonly IReadOnlyDictionary<string, object> _parameterValues;
Expand All @@ -129,11 +130,10 @@ public CommandCacheKey(SelectExpression selectExpression, IReadOnlyDictionary<st
}

public override bool Equals(object obj)
=> obj != null
&& obj is CommandCacheKey commandCacheKey
=> obj is CommandCacheKey commandCacheKey
&& Equals(commandCacheKey);

private bool Equals(CommandCacheKey commandCacheKey)
public bool Equals(CommandCacheKey commandCacheKey)
{
if (!ReferenceEquals(_selectExpression, commandCacheKey._selectExpression))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public override object GenerateCacheKey(Expression query, bool async)
/// not used in application code.
/// </para>
/// </summary>
protected readonly struct RelationalCompiledQueryCacheKey
protected readonly struct RelationalCompiledQueryCacheKey : IEquatable<RelationalCompiledQueryCacheKey>
{
private readonly CompiledQueryCacheKey _compiledQueryCacheKey;
private readonly bool _useRelationalNulls;
Expand Down Expand Up @@ -119,11 +119,19 @@ public RelationalCompiledQueryCacheKey(
/// otherwise <see langword="false"/>.
/// </returns>
public override bool Equals(object obj)
=> !(obj is null)
&& obj is RelationalCompiledQueryCacheKey key
=> obj is RelationalCompiledQueryCacheKey key
&& Equals(key);

private bool Equals(RelationalCompiledQueryCacheKey other)
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">
/// An object to compare with this object.
/// </param>
/// <returns>
/// <see langword="true" /> if the current object is equal to the <paramref name="other" /> parameter; otherwise, <see langword="false" />.
/// </returns>
public bool Equals(RelationalCompiledQueryCacheKey other)
=> _compiledQueryCacheKey.Equals(other._compiledQueryCacheKey)
&& _useRelationalNulls == other._useRelationalNulls
&& _querySplittingBehavior == other._querySplittingBehavior
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public TypedRelationalValueBufferFactoryFactory([NotNull] RelationalValueBufferF
/// </summary>
protected virtual RelationalValueBufferFactoryDependencies Dependencies { get; }

private readonly struct CacheKey
private readonly struct CacheKey : IEquatable<CacheKey>
{
public CacheKey(IReadOnlyList<TypeMaterializationInfo> materializationInfo)
=> TypeMaterializationInfo = materializationInfo;
Expand All @@ -82,7 +82,7 @@ public CacheKey(IReadOnlyList<TypeMaterializationInfo> materializationInfo)
public override bool Equals(object obj)
=> obj is CacheKey cacheKey && Equals(cacheKey);

private bool Equals(CacheKey other)
public bool Equals(CacheKey other)
=> TypeMaterializationInfo.SequenceEqual(other.TypeMaterializationInfo);

public override int GetHashCode()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public override object GenerateCacheKey(Expression query, bool async)
GenerateCacheKeyCore(query, async),
_sqlServerConnection.IsMultipleActiveResultSetsEnabled);

private readonly struct SqlServerCompiledQueryCacheKey
private readonly struct SqlServerCompiledQueryCacheKey : IEquatable<SqlServerCompiledQueryCacheKey>
{
private readonly RelationalCompiledQueryCacheKey _relationalCompiledQueryCacheKey;
private readonly bool _multipleActiveResultSetsEnabled;
Expand All @@ -61,11 +61,10 @@ public SqlServerCompiledQueryCacheKey(
}

public override bool Equals(object obj)
=> !(obj is null)
&& obj is SqlServerCompiledQueryCacheKey sqlServerCompiledQueryCacheKey
=> obj is SqlServerCompiledQueryCacheKey sqlServerCompiledQueryCacheKey
&& Equals(sqlServerCompiledQueryCacheKey);

private bool Equals(SqlServerCompiledQueryCacheKey other)
public bool Equals(SqlServerCompiledQueryCacheKey other)
=> _relationalCompiledQueryCacheKey.Equals(other._relationalCompiledQueryCacheKey)
&& _multipleActiveResultSetsEnabled == other._multipleActiveResultSetsEnabled;

Expand Down
22 changes: 13 additions & 9 deletions src/EFCore/Query/CompiledQueryCacheKeyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected CompiledQueryCacheKey GenerateCacheKeyCore([NotNull] Expression query,
/// not used in application code.
/// </para>
/// </summary>
protected readonly struct CompiledQueryCacheKey
protected readonly struct CompiledQueryCacheKey : IEquatable<CompiledQueryCacheKey>
roji marked this conversation as resolved.
Show resolved Hide resolved
{
private readonly Expression _query;
private readonly IModel _model;
Expand Down Expand Up @@ -112,15 +112,19 @@ public CompiledQueryCacheKey(
/// <see langword="true"/> if the object is a <see cref="CompiledQueryCacheKey" /> and is for the same query, otherwise <see langword="false"/>.
/// </returns>
public override bool Equals(object obj)
{
if (obj is null
|| !(obj is CompiledQueryCacheKey))
{
return false;
}

var other = (CompiledQueryCacheKey)obj;
=> obj is CompiledQueryCacheKey other && Equals(other);

/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">
/// An object to compare with this object.
/// </param>
/// <returns>
/// <see langword="true" /> if the current object is equal to the <paramref name="other" /> parameter; otherwise, <see langword="false" />.
/// </returns>
public bool Equals(CompiledQueryCacheKey other)
{
return ReferenceEquals(_model, other._model)
&& _queryTrackingBehavior == other._queryTrackingBehavior
&& _async == other._async
Expand Down
13 changes: 11 additions & 2 deletions src/EFCore/Storage/ValueBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Microsoft.EntityFrameworkCore.Storage
/// not used in application code.
/// </para>
/// </summary>
public readonly struct ValueBuffer
public readonly struct ValueBuffer : IEquatable<ValueBuffer>
{
/// <summary>
/// A buffer with no values in it.
Expand Down Expand Up @@ -79,7 +79,16 @@ public override bool Equals(object obj)
&& obj is ValueBuffer buffer
&& Equals(buffer);

private bool Equals(ValueBuffer other)
/// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">
/// An object to compare with this object.
/// </param>
/// <returns>
/// <see langword="true" /> if the current object is equal to the <paramref name="other" /> parameter; otherwise, <see langword="false" />.
/// </returns>
public bool Equals(ValueBuffer other)
{
if (_values.Length != other._values.Length)
{
Expand Down
8 changes: 3 additions & 5 deletions src/EFCore/ValueGeneration/ValueGeneratorCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ValueGeneratorCache([NotNull] ValueGeneratorCacheDependencies dependencie
private readonly ConcurrentDictionary<CacheKey, ValueGenerator> _cache
= new ConcurrentDictionary<CacheKey, ValueGenerator>();

private readonly struct CacheKey
private readonly struct CacheKey : IEquatable<CacheKey>
{
public CacheKey(IProperty property, IEntityType entityType, Func<IProperty, IEntityType, ValueGenerator> factory)
{
Expand All @@ -53,13 +53,11 @@ public CacheKey(IProperty property, IEntityType entityType, Func<IProperty, IEnt

public Func<IProperty, IEntityType, ValueGenerator> Factory { get; }

private bool Equals(CacheKey other)
public bool Equals(CacheKey other)
=> Property.Equals(other.Property) && EntityType.Equals(other.EntityType);

public override bool Equals(object obj)
{
return obj is null ? false : obj is CacheKey cacheKey && Equals(cacheKey);
}
=> obj is CacheKey cacheKey && Equals(cacheKey);

public override int GetHashCode() => HashCode.Combine(Property, EntityType);
}
Expand Down