Skip to content

Bad speculation in JIT_MonExit_Portable #34800

Closed

Description

@benaadams

There is a high misprediction rate for the common path in JIT_MonExit_Portable

image

With the common path action == AwareLock::LeaveHelperAction_None not being arranged as the common path in asm.

action = obj->LeaveObjMonitorHelper(GetThread());
if (action == AwareLock::LeaveHelperAction_None)
{
return;
}

image

I've tried rearranging this in the C++; however it seems quite resistant to ordering change; perhaps PGO is incorrectly enforcing the order here?

Aside: I assume it needs to be a cmpxchg rather than just a volatile write because it may have changed from a lock to a lock+hashcode or perhaps signaling? Would there be any scope to having a lock object type that doesn't allow a hashcode or signaling, so it could just be a volatile write to unlock a non-recursive non-signaled lock? (as JIT_MonExit_Portable is quite expensive in the code I'm looking at as 3rd highest cost method)

Activity

  1. added
    area-CodeGen-coreclr
    untriaged
    on Apr 10, 2020
  2. added and removed
    area-CodeGen-coreclr
    on Apr 10, 2020
  3. jkotas commented on Apr 10, 2020

    @jkotas
    Member

    Would there be any scope to having a lock object type that doesn't allow a hashcode or signaling

    Do you mean having a first class lock type (e.g. System.Threading.Lock) that is a simple lock without the syncblock baggage? I think that would be useful.

  4. benaadams commented on Apr 10, 2020

    @benaadams
    MemberAuthor

    Do you mean having a first class lock type (e.g. System.Threading.Lock) that is a simple lock without the syncblock baggage?

    Exactly. Have raised issue #34812

  5. added this to the 5.0 milestone on Apr 10, 2020
  6. removed
    untriaged
    on Apr 10, 2020
  7. modified the milestones: 5.0.0, 6.0.0 on Jul 22, 2020
  8. modified the milestones: 6.0.0, 7.0.0 on Jul 21, 2021
  9. modified the milestones: 7.0.0, Future on Jul 19, 2022
  10. kouvel commented on Sep 8, 2022

    @kouvel
    Contributor

    The first image seems to indicate that the bad speculation is mostly caused by machine clears and not branch misprediction. I'm not sure what exactly would be causing those, perhaps just the cmpxchg or false sharing with other data? In .NET 6 too I don't see the ideal code ordering. Was this happening on a benchmark?

    Aside: I assume it needs to be a cmpxchg rather than just a volatile write because it may have changed from a lock to a lock+hashcode or perhaps signaling? Would there be any scope to having a lock object type that doesn't allow a hashcode or signaling, so it could just be a volatile write to unlock a non-recursive non-signaled lock?

    The fast paths for exiting AwareLock and LowLevelLock also currently use CAS to determine if a waiter needs to be signaled. It may be possible to use a volatile write with another full memory barrier instead.

  11. self-assigned this
    on Jan 6, 2026
  12. VSadov commented on Jan 7, 2026

    @VSadov
    Member

    As of #118371 the entire AwareLock deal is gone.

    Object locking is done entirely in managed code now and does not need special helpers for unframed/framed code paths. It is highly unlikely that the same pattern/issue is still present after all those changes.

  13. locked and limited conversation to collaborators on Feb 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions