Remove allocations from Dns.* (dotnet/corefx#41061)
authorStephen Toub <stoub@microsoft.com>
Tue, 17 Sep 2019 12:20:14 +0000 (08:20 -0400)
committerGitHub <noreply@github.com>
Tue, 17 Sep 2019 12:20:14 +0000 (08:20 -0400)
commit0b5abf840284890db2f0d1f19c95b95e43e175cf
treede5a16d5c579dd82c83012954d7c5ad3243dabbb
parentebce975828caaa72fcd3aad4f4833a73f7296777
Remove allocations from Dns.* (dotnet/corefx#41061)

This started as an effort to reduce the size of System.Net.NameResolution.dll when publishing a trimmed app.  It's not that big to begin with, but it's carrying around a copy of all of the IAsyncResult helper types, because the Get*Async methods are currently wrappers for the Begin/End* methods.

This PR inverts that, wrapping the Begin/End* methods instead around the Get*Async methods, using the same TaskToApm helper we use in other places in corefx for the same purpose.  This makes the Get*Async methods faster and lighterweight, but it does increase the number/amount of allocation in the Begin/End* APIs.  Since these are considered legacy, I normally would consider that a good trade, however we still use these Begin/End methods in a few places in System.Net.Sockets, and I didn't want to regress those use cases.

So, this also then trims some additional fat, which helps the Get*Async cases even further, and gets the Begin/End* to be even better than before the change.  This includes not allocating an IPHostEntry when we're just going to unwrap it and return its addresses, computing the exact IPAddress[] size we need rather than using a List<> to grow it and ToArray to create the actual array, avoiding creating the HostName if we don't need it, and avoiding an unnecessary SafeHandle allocation.

As part of this, I also noticed that we had some bugs in how some of our interop structures on Windows were defined.  In particular, fields that in the native types were size_t were defined as int rather than IntPtr in the managed code.  It appears we were saved by padding, but I fixed it regardless.

And as long as I was changing pretty much everything else, where I was touching code I also cleaned up some legacy style stuff.

Commit migrated from https://github.com/dotnet/corefx/commit/a55e95cbd9fca7ce5b3ad34c583642a35f42cd63
26 files changed:
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.FreeLibrary.cs
src/libraries/Common/src/Interop/Windows/WinSock/AddressInfo.cs [deleted file]
src/libraries/Common/src/Interop/Windows/WinSock/AddressInfoEx.cs [deleted file]
src/libraries/Common/src/Interop/Windows/WinSock/AddressInfoHints.cs
src/libraries/Common/src/Interop/Windows/WinSock/Interop.GetAddrInfoExW.cs
src/libraries/Common/src/Interop/Windows/WinSock/Interop.GetAddrInfoW.cs
src/libraries/Common/src/Interop/Windows/WinSock/Interop.SocketConstructorFlags.cs
src/libraries/Common/src/Interop/Windows/WinSock/Interop.freeaddrinfo.cs [deleted file]
src/libraries/Common/src/Interop/Windows/WinSock/SafeFreeAddrInfo.cs [deleted file]
src/libraries/Common/src/System/Net/DebugSafeHandle.cs
src/libraries/Common/tests/System/Net/Configuration.Http.cs
src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj
src/libraries/System.Net.NameResolution/src/System/Net/Dns.cs [moved from src/libraries/System.Net.NameResolution/src/System/Net/DNS.cs with 51% similarity]
src/libraries/System.Net.NameResolution/src/System/Net/DnsResolveAsyncResult.cs [deleted file]
src/libraries/System.Net.NameResolution/src/System/Net/IPHostEntry.cs
src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Unix.cs
src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Windows.cs
src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionUtilities.cs [deleted file]
src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostByAddressTest.cs
src/libraries/System.Net.NameResolution/tests/FunctionalTests/GetHostEntryTest.cs
src/libraries/System.Net.NameResolution/tests/FunctionalTests/TestSettings.cs
src/libraries/System.Net.NameResolution/tests/PalTests/Fakes/IPAddressFakeExtensions.cs
src/libraries/System.Net.NameResolution/tests/PalTests/NameResolutionPalTests.cs
src/libraries/System.Net.NameResolution/tests/PalTests/System.Net.NameResolution.Pal.Tests.csproj
src/libraries/System.Net.NameResolution/tests/UnitTests/Fakes/FakeNameResolutionPal.cs
src/libraries/System.Net.NameResolution/tests/UnitTests/System.Net.NameResolution.Unit.Tests.csproj