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
Function pointers was introduced in C# 9 and can be used as allocation-free alternatives of delegates. SpanAction<T, TState> and ReadOnlySpanAction<T, TSate> can be effectively replaced with such pointers.
The purpose would be to avoid yet another allocation and while I would expect that this is not necessarily used in a hot-path, the string.Create API exists as a performance optimization already and so having an overload that allows you to avoid an additional allocation seems to be inline with the original goal of the API (a low cost and "official" way to create a string of a known length and set the contents).
@tannergooding , maybe it's reasonable to expand proposal and look at all public APIs expecting these delegates as input parameters and add overloads with function pointers.
I don't think that's as worthwhile. Most APIs taking a delegate aren't as performance oriented/low-level and so a function pointer might seem out of place.
The purpose would be to avoid yet another allocation
Just to make sure everyone is on the same page, though, we're talking about one allocation total, not per invocation. The scenarios where a function pointer could be used (no capture) are the same scenarios for which the C# compiler will automatically cache the delegate, so there will only be an allocation the first time it's used and then never again (other than if there's a race to initialize it, but that's a one-time hit as well).
On this microbenchmark, which is arguably the absolute best case (one-character string being initialized with a known single character value):
so, best case, a 13% improvement on throughput. Most uses involve lots more work than this, but even ones that are just a tad-bit larger see a significant drop in that already relatively-small difference, e.g. here's an example from one use in ASP.NET that just fills the target buffer with some bitshifting from a long and lookups in a string:
And on top of that, all such APIs are allocating new strings, so these benchmark results don't reflect well the further cost that both incur of the impact on GC and how that affects the whole app.
From my perspective, this isn't worth starting a trend of creating unsafe pointer-based overloads for such APIs.
My $.02.
Reacted by Tanner Gooding, Günther Foidl, Joe4evr, SingleAccretion, Zhenwei Wu, Petr Onderka and Jan Kotas
Activity
Dotnet-GitSync-Bot commentedon Dec 3, 2020
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.
Joe4evr commentedon Dec 3, 2020
I don't see that much use for this overload, considering that its only purpose is being an
unsafeequivalent of what already exists.That's not a risk at all, that's basic code hygiene. The real risk is that any call-site requires
unsafe.tannergooding commentedon Dec 3, 2020
The purpose would be to avoid yet another allocation and while I would expect that this is not necessarily used in a hot-path, the
string.CreateAPI exists as a performance optimization already and so having an overload that allows you to avoid an additional allocation seems to be inline with the original goal of the API (a low cost and "official" way to create a string of a known length and set the contents).It might be worth prototyping and seeing if it shows any benefits for the locations already using
String.Createsuch asASP.NET Core,ReadOnlySequence,HttpListener, and a few of theEncodingAPIs: https://source.dot.net/#System.Private.CoreLib/String.cs,7dd9a20e8a84bf21,referencessakno commentedon Dec 3, 2020
@tannergooding , maybe it's reasonable to expand proposal and look at all public APIs expecting these delegates as input parameters and add overloads with function pointers.
tannergooding commentedon Dec 3, 2020
I don't think that's as worthwhile. Most APIs taking a delegate aren't as performance oriented/low-level and so a function pointer might seem out of place.
I believe it's a case by case basis where
function pointersmight be beneficial and there are likely a few places where generic specialized interfaces would be a better fit.There are also likely a number of places where
function pointersare the wrong approach and where generic specialization over an interface would win out: https://source.dot.net/#System.Private.CoreLib/SpanHelpers.BinarySearch.cs,7ffa10c3faafe048, particularly where predicates and simple comparisons are involved.stephentoub commentedon Dec 3, 2020
Just to make sure everyone is on the same page, though, we're talking about one allocation total, not per invocation. The scenarios where a function pointer could be used (no capture) are the same scenarios for which the C# compiler will automatically cache the delegate, so there will only be an allocation the first time it's used and then never again (other than if there's a race to initialize it, but that's a one-time hit as well).
On this microbenchmark, which is arguably the absolute best case (one-character string being initialized with a known single character value):
on my machine I get:
so, best case, a 13% improvement on throughput. Most uses involve lots more work than this, but even ones that are just a tad-bit larger see a significant drop in that already relatively-small difference, e.g. here's an example from one use in ASP.NET that just fills the target buffer with some bitshifting from a long and lookups in a string:
and which yields on my machine:
for only a 6% gain.
And on top of that, all such APIs are allocating new strings, so these benchmark results don't reflect well the further cost that both incur of the impact on GC and how that affects the whole app.
From my perspective, this isn't worth starting a trend of creating unsafe pointer-based overloads for such APIs.
My $.02.
stephentoub commentedon Mar 14, 2022
Given #45540 (comment), I'm going to close this issue. Thank you for the suggestion.