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
Proposal: Memory and Span friendly overloads in Ping class #34856
I'm using Ping class for path MTU discovery using one of the proving method described here. My implementation sending packets with different MTU in a loop. Each iteration requires allocation of byte array in the heap. As a result, it produces a lot of garbage. It would be great to have Memory- and Span-friendly overloads that allow to reuse the same buffer.
Thank you for the issue. It looks like you are proposing several new APIs for the Ping class. Before you can submit a PR, these new APIs need to be reviewed and approved with our API review process.
@sakno could you give a sample usage? How is this better than just pooling the buffers you pass in? Are you trying to pass views over one large buffer?
@danmosemsft , the example in my gist which is specified in the original post. Path MTU discovery algorithm uses half-interval search between lowest and highest possible MTU values. As a result, it's not possible to pre-allocate byte arrays. ArrayPool<byte> can't help me because I need precise length of the array. These overloads can help me because then I able to use ArrayPool<byte> and allocate buffer once and pass slices of the same array between iterations.
@svick , usage of ValueTask is useless due to request-reply nature of ICMP Echo. When using sockets, sending requests and waiting for the reply in 99,9999% cases will never be completed synchronously. Under the hood, the current implementation uses Socket.ReceiveFromAsync which has Task return type. Of course, you can derive from SocketAsyncEventArgs and use ManualResetValueTaskSourceCore to organize pool of such instances. IMHO, this approach adds a lot of complexity. Holding pool of SocketAsyncEventArgs probable have the same memory pressure as Task allocation.
@saknoValueTask-returning Socket.ReceiveFromAsync has been accepted (#33418), so it's likely it will be in .Net 5. And while using ManualResetValueTaskSourceCore manually does add some complexity, if the return type is Task, you close that door forever. Also, if #13633 is enabled, implementing allocation-avoiding ValueTask methods becomes trivial.
@svick , I think that ReceiveMessageFromAsync with ValueTask can be completed synchronously only if the incoming packet has been received and already placed to OS packet scheduler. In case of ICMP echo it will always be false just because CPU is faster that network and at the time of ReceiveMessageFromAsync call, the response is not yet received.
In other words, ReceiveMessageFromAsync can be completed synchronously if you are receiving stream of packets (RTP or UDP) and speed of such reading is equal or less than speed of packets arrival.
In such scenario, I don't think the perf overhead of an allocation matters.
In general, I wonder how common is the scenario where the overloads would benefit. I am a bit torn between adding more APIs for very corner-case scenarios vs. easy to add. @wfurt@scalablecory what are your thoughts?
Triage: We would love to see some more data how important this is in the scenarios -- even if it is mostly for Windows / elevated Linux. @sakno can you share how much it showed up in your cases? Which OS do you use? Windows or elevated Linux?
I agree with @scalablecory that CancellationToken should be preferred over timeout. According with my work in #35461 I see no blockers, even for unprivileged user on Linux. However, it requires a lot of work.
Activity
Dotnet-GitSync-Bot commentedon Apr 11, 2020
I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label.
sakno commentedon Apr 11, 2020
I'm ready to fix that and create pull request if you will mark this issue with up-for-grabs label.
davidsh commentedon Apr 11, 2020
Thank you for the issue. It looks like you are proposing several new APIs for the Ping class. Before you can submit a PR, these new APIs need to be reviewed and approved with our API review process.
davidsh commentedon Apr 11, 2020
@scalablecory
sakno commentedon Apr 11, 2020
@davidsh , I need to wait for approve, right?
davidsh commentedon Apr 11, 2020
Yes. It needs to be formally reviewed and marked as "api-approved".
danmoseley commentedon Apr 12, 2020
@sakno could you give a sample usage? How is this better than just pooling the buffers you pass in? Are you trying to pass views over one large buffer?
sakno commentedon Apr 12, 2020
@danmosemsft , the example in my gist which is specified in the original post. Path MTU discovery algorithm uses half-interval search between lowest and highest possible MTU values. As a result, it's not possible to pre-allocate byte arrays.
ArrayPool<byte>can't help me because I need precise length of the array. These overloads can help me because then I able to useArrayPool<byte>and allocate buffer once and pass slices of the same array between iterations.svick commentedon Apr 12, 2020
SendAsyncoverloads, when we haveSendPingAsync?SendPingAsyncreturnValueTask<PingReply>? It has the potential to avoidTask-related allocations.sakno commentedon Apr 12, 2020
@svick , usage of
ValueTaskis useless due to request-reply nature of ICMP Echo. When using sockets, sending requests and waiting for the reply in 99,9999% cases will never be completed synchronously. Under the hood, the current implementation usesSocket.ReceiveFromAsyncwhich hasTaskreturn type. Of course, you can derive fromSocketAsyncEventArgsand useManualResetValueTaskSourceCoreto organize pool of such instances. IMHO, this approach adds a lot of complexity. Holding pool ofSocketAsyncEventArgsprobable have the same memory pressure asTaskallocation.svick commentedon Apr 13, 2020
@sakno
ValueTask-returningSocket.ReceiveFromAsynchas been accepted (#33418), so it's likely it will be in .Net 5. And while usingManualResetValueTaskSourceCoremanually does add some complexity, if the return type isTask, you close that door forever. Also, if #13633 is enabled, implementing allocation-avoidingValueTaskmethods becomes trivial.sakno commentedon Apr 13, 2020
@svick , I think that
ReceiveMessageFromAsyncwithValueTaskcan be completed synchronously only if the incoming packet has been received and already placed to OS packet scheduler. In case of ICMP echo it will always be false just because CPU is faster that network and at the time ofReceiveMessageFromAsynccall, the response is not yet received.In other words,
ReceiveMessageFromAsynccan be completed synchronously if you are receiving stream of packets (RTP or UDP) and speed of such reading is equal or less than speed of packets arrival.karelz commentedon Apr 13, 2020
Just a heads up that Ping on Linux will spin off a new process per call if you are not admin:
runtime/src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Unix.cs
Lines 25 to 29 in f498a31
In such scenario, I don't think the perf overhead of an allocation matters.
In general, I wonder how common is the scenario where the overloads would benefit. I am a bit torn between adding more APIs for very corner-case scenarios vs. easy to add. @wfurt @scalablecory what are your thoughts?
scalablecory commentedon Apr 13, 2020
I'd be okay with some spannified overloads here for consistency with rest of BCL.
I would remove the
SendAsyncoverloads, and consider (if implementation allows) removing thetimeoutparameter in favor of aCancellationToken.karelz commentedon May 14, 2020
Triage: We would love to see some more data how important this is in the scenarios -- even if it is mostly for Windows / elevated Linux.
@sakno can you share how much it showed up in your cases? Which OS do you use? Windows or elevated Linux?
sakno commentedon May 14, 2020
@karelz , my primary OS is Linux.
I agree with @scalablecory that
CancellationTokenshould be preferred over timeout. According with my work in #35461 I see no blockers, even for unprivileged user on Linux. However, it requires a lot of work.GSPP commentedon Jul 3, 2020
Should the timeout parameters be
TimeSpan? If not it might be better to put the unit in the parameter name (e.g.timeoutMilliseconds).sakno commentedon Jul 3, 2020
@GSPP , the proposed API is updated. Thanks!
deeprobin commentedon Jun 8, 2022
Any things that need to be clarified or can we mark this as ready-for-review?