Skip to content

Conversation

@lighting9999
Copy link

@lighting9999 lighting9999 commented Nov 29, 2025

Change SetUnmanagedStructures void to use Span <byte> and Redo CreatePingReplyFromIcmpEchoReply void to Performance Optimization.

Change TaskCompletionSource initialization to use RunContinuationsAsynchronously option and remove unnecessary sourceAddr.Clear() call.
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Nov 29, 2025
@stephentoub
Copy link
Member

What is this PR addressing?

@stephentoub stephentoub added the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label Nov 29, 2025
Clear the sourceAddr span before sending the echo request.
@lighting9999
Copy link
Author

Waiting to all Performance Optimization.

@stephentoub
Copy link
Member

stephentoub commented Nov 29, 2025

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.
@lighting9999
Copy link
Author

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);
    
}

Copy link
Member

@huoyaoyuan huoyaoyuan left a 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.

Comment on lines +353 to 359
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();
}
Copy link
Member

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,
Copy link
Member

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.

Comment on lines +312 to +313
Span<byte> destination = new Span<byte>(_requestBuffer.DangerousGetHandle().ToPointer(), buffer.Length);
buffer.CopyTo(destination);
Copy link
Member

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how to change?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Net community-contribution Indicates that the PR has been added by a community member NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants