-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Update Ping.Windows.cs for async task handling #122054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Change TaskCompletionSource initialization to use RunContinuationsAsynchronously option and remove unnecessary sourceAddr.Clear() call.
src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs
Show resolved
Hide resolved
src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs
Outdated
Show resolved
Hide resolved
|
What is this PR addressing? |
Clear the sourceAddr span before sending the echo request.
|
Waiting to all Performance Optimization. |
How is this an optimization? If anything, it's most likely a regression, as it's forcibly adding an additional thread pool hop. |
Refactor unmanaged memory handling for async ICMP echo.
|
deadlock Example: public async Task CheckNetworkConnectivity()
{
var ping = new Ping();
var tasks = new List<Task<PingReply>>();
for (int i = 0; i < 500; i++)
{
tasks.Add(ping.SendPingAsync("192.168.1.1"));
}
var results = await Task.WhenAll(tasks);
} |
src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs
Outdated
Show resolved
Hide resolved
Refactor Ping reply handling to improve early return on failure and optimize data extraction.
src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs
Outdated
Show resolved
Hide resolved
Removed the CopyDataFromUnmanagedMemory method to simplify code.
huoyaoyuan
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please do not make random changes just for changing. The content of the pull request looks like the result of telling AI to "optimize some thing". There is little or no real benefit, if not negative.
src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.Windows.cs
Outdated
Show resolved
Hide resolved
| static byte[] CreateBuffer(IntPtr data, int dataSize) | ||
| { | ||
| rtt = 0; | ||
| options = null; | ||
| buffer = Array.Empty<byte>(); | ||
| Span<byte> bufferSpan = stackalloc byte[dataSize]; | ||
| Marshal.Copy(data, bufferSpan, 0, dataSize); | ||
|
|
||
| return bufferSpan.ToArray(); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method is pure regression. It's still allocating a new array, and copying the data twice with a temporary span.
| PingOptions? options; | ||
| byte[] buffer; | ||
| var (rtt, options, buffer) = ipStatus == IPStatus.Success | ||
| ? (reply.roundTripTime, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using ?: here is much less readable.
| Span<byte> destination = new Span<byte>(_requestBuffer.DangerousGetHandle().ToPointer(), buffer.Length); | ||
| buffer.CopyTo(destination); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While the change is valid, we may do this better with the SafeLocalAllocHandle type itself, as a part of the reduce unsafe theme.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how to change?
Change
SetUnmanagedStructuresvoid to useSpan <byte>and RedoCreatePingReplyFromIcmpEchoReplyvoid to Performance Optimization.