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

Eliminate boxing when entering critical sections #21230

Merged
merged 5 commits into from
Jun 12, 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
@@ -0,0 +1,29 @@
// 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 JetBrains.Annotations;

namespace Microsoft.EntityFrameworkCore.Infrastructure
{
/// <summary>
/// A <see cref="IDisposable" /> returned by an <see cref="IConcurrencyDetector" />, which will exit the ongoing
/// critical section when disposed.
/// </summary>
public readonly struct ConcurrencyDetectorCriticalSectionDisposer : IDisposable
{
private readonly IConcurrencyDetector _concurrencyDetector;

/// <summary>
/// Constructs a new <see cref="ConcurrencyDetectorCriticalSectionDisposer" />.
/// </summary>
/// <param name="concurrencyDetector">
/// The <see cref="IConcurrencyDetector" /> on which the critical section will be exited.
/// </param>
public ConcurrencyDetectorCriticalSectionDisposer([NotNull] IConcurrencyDetector concurrencyDetector)
=> _concurrencyDetector = concurrencyDetector;

/// <inheritdoc />
public void Dispose() => _concurrencyDetector.ExitCriticalSection();
}
}
9 changes: 7 additions & 2 deletions src/EFCore/Infrastructure/IConcurrencyDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ namespace Microsoft.EntityFrameworkCore.Infrastructure
public interface IConcurrencyDetector
{
/// <summary>
/// Call to enter the critical section.
/// Enters a critical section.
/// </summary>
/// <returns> A disposer that will exit the critical section when disposed. </returns>
IDisposable EnterCriticalSection();
ConcurrencyDetectorCriticalSectionDisposer EnterCriticalSection();

/// <summary>
/// Exits the critical section.
/// </summary>
void ExitCriticalSection();
}
}
31 changes: 9 additions & 22 deletions src/EFCore/Internal/ConcurrencyDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ namespace Microsoft.EntityFrameworkCore.Internal
/// </summary>
public class ConcurrencyDetector : IConcurrencyDetector
{
private readonly IDisposable _disposer;
private int _inCriticalSection;
private static readonly AsyncLocal<bool> _threadHasLock = new AsyncLocal<bool>();
private int _refCount;
Expand All @@ -37,15 +36,7 @@ public class ConcurrencyDetector : IConcurrencyDetector
/// 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 ConcurrencyDetector() => _disposer = new Disposer(this);

/// <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 IDisposable EnterCriticalSection()
public virtual ConcurrencyDetectorCriticalSectionDisposer EnterCriticalSection()
{
if (Interlocked.CompareExchange(ref _inCriticalSection, 1, 0) == 1)
{
Expand All @@ -60,10 +51,16 @@ public virtual IDisposable EnterCriticalSection()
}

_refCount++;
return _disposer;
return new ConcurrencyDetectorCriticalSectionDisposer(this);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this not create a new struct every time? (rather than reusing like how previous did)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct construction is cheap

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does, but creating a struct (on the stack) is free - especially as here it's just wrapping a pointer. So it's the same as allocating a long, or a reference on the stack.

}

private void ExitCriticalSection()
/// <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 void ExitCriticalSection()
{
Check.DebugAssert(_inCriticalSection == 1, "Expected to be in a critical section");

Expand All @@ -73,15 +70,5 @@ private void ExitCriticalSection()
_inCriticalSection = 0;
}
}

private readonly struct Disposer : IDisposable
{
private readonly ConcurrencyDetector _concurrencyDetector;

public Disposer(ConcurrencyDetector concurrencyDetector)
=> _concurrencyDetector = concurrencyDetector;

public void Dispose() => _concurrencyDetector.ExitCriticalSection();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,12 @@ private static void TestMultipleScoped(Action<EntityFrameworkServicesBuilder> tr

private class FakeConcurrencyDetector : IConcurrencyDetector
{
public IDisposable EnterCriticalSection()
ConcurrencyDetectorCriticalSectionDisposer IConcurrencyDetector.EnterCriticalSection()
{
throw new NotImplementedException();
}

public Task<IDisposable> EnterCriticalSectionAsync(CancellationToken cancellationToken)
public void ExitCriticalSection()
{
throw new NotImplementedException();
}
Expand Down