You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Marshal.AllocHGlobal/FreeHGlobal is ~150x slower in .NET than legacy mono on device (tvOS) #58939
here is also NativeMemory.Alloc which should be faster than AllocHGlobal
On Unix systems Marshal.AllocHGlobal is just wrapper around NativeMemory.Alloc. On legacy Mono it was an icall, now it's a P/Invoke. It doesn't have the SuppressGCTransition attribute applied so it goes through the expensive GC transitions.
I think malloc is an interesting case where common implementations try to ensure it is as fast as possible. If you look at the various implementations available, most only take locks or do syscalls in the rare edge case, not in the common path.
That is, these calls that make it "incompatible" with SuppressGCTransition largely only happen when the underlying heap needs to be created or expanded. Otherwise, small allocations avoid all of this as do many medium sized allocations.
It would perhaps be interesting to see if there was something we could do that could help support this kind of scenario.
I think the root cause of this problem is the high overhead implementation strategy used for PInvoke transitions on tvOS. This high overhead is a problem for every other PInvoke. For example, globalization PInvokes will hit it too.
Reacted by Egor Bogatov, Filip Navara and SupinePandora43
I agree that this is not generally fixable for .NET 6 timeframe. Something like PR #59029 may help a bit while being backportable. PR #58992 is exploring a more radical solution (but definitely not ready for prime time).
Activity
EgorBo commentedon Sep 10, 2021
Unrelated, just a note: there is also
NativeMemory.Allocwhich should be faster than AllocHGlobalfilipnavara commentedon Sep 10, 2021
On Unix systems
Marshal.AllocHGlobalis just wrapper aroundNativeMemory.Alloc. On legacy Mono it was an icall, now it's a P/Invoke. It doesn't have theSuppressGCTransitionattribute applied so it goes through the expensive GC transitions.jkotas commentedon Sep 10, 2021
malloc/freecan take locks or it can end up calling OS syscalls likemmap. It would not be appropriate to mark it withSuppressGCTransition.filipnavara commentedon Sep 10, 2021
I agree it would be a correctness issue to do so. I am still thinking about benchmarking the impact though.
Depending on particular use case there may be other ways to allocate the memory which are less costly (eg. stackalloc for small allocations).
tannergooding commentedon Sep 10, 2021
I think
mallocis an interesting case where common implementations try to ensure it is as fast as possible. If you look at the various implementations available, most only take locks or do syscalls in the rare edge case, not in the common path.That is, these calls that make it "incompatible" with
SuppressGCTransitionlargely only happen when the underlying heap needs to be created or expanded. Otherwise, small allocations avoid all of this as do many medium sized allocations.It would perhaps be interesting to see if there was something we could do that could help support this kind of scenario.
EgorBo commentedon Sep 10, 2021
like emitting it as
? where someLimit is some small value that won't cause any mmap under the hood
jkotas commentedon Sep 10, 2021
You cannot tell. Any
malloccall, no matter how small the request block size is, can end up taking locks or calling mmap.EgorBo commentedon Sep 10, 2021
Right, just discovered that in glibc sources 👍
jkotas commentedon Sep 10, 2021
I think the root cause of this problem is the high overhead implementation strategy used for PInvoke transitions on tvOS. This high overhead is a problem for every other PInvoke. For example, globalization PInvokes will hit it too.
[tests] Add perf test for calling Marshal.AllocHGlobal/FreeHGlobal.
7 remaining items
marek-safar commentedon Sep 13, 2021
/cc @vargaz
vargaz commentedon Sep 13, 2021
Fixing/improving this would require risky changes so this will unlikely to be fixed for 6.0.
filipnavara commentedon Sep 13, 2021
I agree that this is not generally fixable for .NET 6 timeframe. Something like PR #59029 may help a bit while being backportable. PR #58992 is exploring a more radical solution (but definitely not ready for prime time).
[release/6.0-rc2] [MonoVM] Reduce P/Invoke GC transition asserts in r…
SupinePandora43 commentedon Apr 12, 2022
What about CoreCLR ARM64?
akoeplinger commentedon Aug 12, 2022
Moving to 8.0
mono_threads_enter_gc_safe_region_unbalancedperformance regression macios#14835