-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Changes from 3 commits
64793f6
ad77941
f1f4bb8
1d71f0f
dc1ed35
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// 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; | ||
|
||
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 class 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(IConcurrencyDetector concurrencyDetector) | ||
=> _concurrencyDetector = concurrencyDetector; | ||
|
||
/// <inheritdoc /> | ||
public void Dispose() => _concurrencyDetector.ExitCriticalSection(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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) | ||
{ | ||
|
@@ -60,10 +51,16 @@ public virtual IDisposable EnterCriticalSection() | |
} | ||
|
||
_refCount++; | ||
return _disposer; | ||
return new ConcurrencyDetectorCriticalSectionDisposer(this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. struct construction is cheap There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
|
||
|
@@ -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(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't a struct, so we will still be allocating. What's the purpose of this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, that was a typo (it's 3AM here). Changing to readonly struct.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(maybe concurrency-related programming at 3AM isn't such a good idea?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it was an intentional bug like what Arthur suggested to include in every PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah of course, that's what it was. Congrats for winning the prize @AndriySvyryd!