Repository navigation
Description
This is a follow up from (RIP) #55273, specifically this comment from @jkotas.
Opening this issue for tracking and avoid that getting lost.
Overview
Consider this snippet:
static unsafe void A(ref byte r)
{
fixed (void* p = &r)
{
B(p);
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
static unsafe void B(void* p)
{
}This currently results in:
sub rsp, 0x28
mov [rsp+0x20], rcx
call 0x00007ffaecb70028
xor eax, eax
mov [rsp+0x20], rax
add rsp, 0x28
retCurrently, all pinned locals are always stored on the stack. This makes pinning not really ideal for hot paths.
It would be nice if the JIT added support for using a register to store pinned locals, when possible.
As mentioned by @tannergooding, the register would need to be cleared when out of scope to stop tracking.
The method A from above could then become something like this:
mov rbx, rcx
call 0x00007ffaecb70028
xor rbx, rbx
retHere I just used rbx to store the pinned local (just picked the first callee-saved register). I do realize there's plenty of work to make this work and all the various GC data structures need to be updated accordingly to enable tracking, this is the general idea.
Goes without saying that this optimization could bring some nice codegen/perf wins in interop-heavy code 😄
Additionally, given this could be used to restore the RuntimeHelpers.GetHashCode optimization by porting the happy path to C# (as I did in #55273, but possibly without the GC hole ahah), it would automatically speedup virtually every dictionary out there using random reference types as keys. Or, any other data structure that would call GetHashCode at some point on an object that didn't override the default object.GetHashCode implementation.
category:cq
theme:pinning
Activity
tannergooding commentedon Jan 5, 2022
I'm not super convinced of the wins here myself.
I think that being able to elide the
pinin the case that thepinned valueis coming from the stack would be better: #40553The "friendly signature" for something like
BOOL QueryPerformanceFrequency(LARGE_INTEGER* lpFrequency)isbool QueryPerformanceFrequency(out long frequency), in which case the typical usage is likelyif (QueryPerformanceFrequency(out var frequency)). In that scenario, theouthas to be pinned, even though the P/Invoke wrapper, once inlined, could see that pinning a stack local isn't required.Regular pinning is effectively a
stack spillhowever, and in the terms of "typical" method calls, isn't going to be much more expensive than the spilling or register shuffling that already typically happens to meet the callee/caller saved register requirements. Further, in the case ofP/Invoke, this spill is nothing compared to the transition stub which already spills basically everything to ensure that any GC tracked data is not in a register (see #54107 (comment), which shows the disassembly for such a transition).Sergio0694 commentedon Jan 5, 2022
I can still think of many cases where the stack spill for the pinning would be the only one. Like, this would be the case for
RuntimeHelpers.GetHashCode, which otherwise is all entirely inlined and with all locals enregistered. I would imagine that'd be nice to optimize given the speedup it could give downstream to all users indirectly relying on it for hashcode-based data structures. Also, there could be plenty of other cases where the pinning is the biggest overhead in a method (eg.ComPtr<T>.GetPinnableReference()comes to mind, which would otherwise basically be free if the pinned local was enregistered. All in all I guess I'm just saying, #40553 would sure be nice to have too, but I don't see why this one would be mutually exclusive. Wouldn't it be nice to have both? 😄JulieLeeMSFT commentedon Jan 25, 2022
cc @kunalspathak.
Sergio0694 commentedon Jun 3, 2022
For reference, I did try to use
fixedas a follow up to #55273, ie:The performance though was way worse than the current one:
The asm in particular was... Interesting:
Looks like there's lots of room for improvements to the codegen in this case? 🤔
I will also say: I still think an
Unsafe.AtomicAddByteOffsetAndReadintrinsic would be useful.It'd both solve this case (no need to use
fixedat all), and it'd potentially be useful in other scenarios as well.As in, the code above could then just be:
SupinePandora43 commentedon Jun 3, 2022
@Sergio0694 more like
Unsafe.AtomicSubstractByteOffsetAndReadSergio0694 commentedon Jun 3, 2022
I mean yes, that was just an example (plus you'd get the same anyway with a negative offset).
My point was just "some API that can atomically subtract and read with GC tracking and no pinning" 🙂
Of course, such an API would only be allowed to read primitives, or even just
int/nuint, doesn't really matter.jakobbotsch commentedon Dec 12, 2022
This would likely fix #35748 too.
VSadov commentedon Feb 28, 2024
I'd like to +1 this.
In NativeAOT we do have
GetHashcodeimplemented in managed code. Also thin locks.All operations with
ObjectHeaderneed to pin and some code paths are otherwise fairly simple. Releasing a thin lock, for example is just a few checks for rare cases and then setting a bit in the header.The part that pinning introduces stack locals hurts the scenario somewhat.
VSadov commentedon Feb 28, 2024
Also see: #97997 , that reminded me of this issue.