Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Make SpinLock.TryEnter(ref bool) fail faster#9224

Merged
stephentoub merged 1 commit into
dotnet:masterfrom
stephentoub:spinlock_tryenter_perf
Jan 31, 2017
Merged

Make SpinLock.TryEnter(ref bool) fail faster#9224
stephentoub merged 1 commit into
dotnet:masterfrom
stephentoub:spinlock_tryenter_perf

Conversation

@stephentoub
Copy link
Copy Markdown
Member

@stephentoub stephentoub commented Jan 31, 2017

One uses SpinLock.TryEnter(ref bool) when they want to do a quick test to either acquire the lock or know it's held by someone else. We can make the latter path much faster so that it fails faster.

With this change, TryEnter(ref bool) on an already held SpinLock is ~3.2x faster than before. It looks like there may be a small ~2-3% regression when the lock isn't already held, but it's hard to say for sure.

cc: @jkotas, @benaadams, @kouvel

{
TryEnter(0, ref lockTaken);
int observedOwner = m_owner;
if ((observedOwner & LOCK_ID_DISABLE_MASK) == 0 | lockTaken)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Didn't realise you could bitwise a bool 😄

@benaadams
Copy link
Copy Markdown
Member

LGTM and the trade off is a good one

@stephentoub stephentoub changed the title Make SpinLock.TryEnter(ref int) fail faster Make SpinLock.TryEnter(ref bool) fail faster Jan 31, 2017
One uses SpinLock.TryEnter(ref bool) when they want to do a quick test to either acquire the lock or know it's held by someone else.  We can make the latter path much faster so that it fails faster.
@stephentoub stephentoub force-pushed the spinlock_tryenter_perf branch from 0e0ee01 to 7ee26ae Compare January 31, 2017 21:14
@stephentoub stephentoub merged commit 93afcb5 into dotnet:master Jan 31, 2017
@stephentoub stephentoub deleted the spinlock_tryenter_perf branch January 31, 2017 22:54
@karelz karelz modified the milestone: 2.0.0 Aug 28, 2017
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…nter_perf

Make SpinLock.TryEnter(ref bool) fail faster

Commit migrated from dotnet/coreclr@93afcb5
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants