-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Do not overallocate SocketAddress.Buffer #78860
Changes from 6 commits
98a1549
6815398
7392b08
ddb50fc
c62917c
6363fe3
aceb23f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,7 +92,13 @@ public SocketAddress(AddressFamily family, int size) | |
} | ||
|
||
InternalSize = size; | ||
Buffer = new byte[(size / IntPtr.Size + 2) * IntPtr.Size]; | ||
#if !SYSTEM_NET_PRIMITIVES_DLL && WINDOWS | ||
// WSARecvFrom needs a pinned pointer to the 32bit socket address size: https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsarecvfrom | ||
// Allocate IntPtr.Size extra bytes at the end of Buffer ensuring IntPtr.Size alignment, so we don't need to pin anything else. | ||
// The following forumla will extend 'size' to the alignment boundary then add IntPtr.Size more bytes. | ||
size = (size + IntPtr.Size - 1) / IntPtr.Size * IntPtr.Size + IntPtr.Size; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. int Extend(int size) => (size + IntPtr.Size - 1) / IntPtr.Size * IntPtr.Size + IntPtr.Size;
for (int i = 10; i < 30; i++) Console.Write($"{i,2} | ");
Console.WriteLine();
for (int i = 10; i < 30; i++) Console.Write($"{Extend(i),2} | "); Will print:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, the old code did
Sensible address structure would already be aligned IMHO and we really just need to put |
||
#endif | ||
Buffer = new byte[size]; | ||
|
||
SocketAddressPal.SetAddressFamily(Buffer, family); | ||
} | ||
|
@@ -171,20 +177,23 @@ internal IPEndPoint GetIPEndPoint() | |
return new IPEndPoint(GetIPAddress(), GetPort()); | ||
} | ||
|
||
#if !SYSTEM_NET_PRIMITIVES_DLL && WINDOWS | ||
// For ReceiveFrom we need to pin address size, using reserved Buffer space. | ||
internal void CopyAddressSizeIntoBuffer() | ||
{ | ||
Buffer[Buffer.Length - IntPtr.Size] = unchecked((byte)(InternalSize)); | ||
Buffer[Buffer.Length - IntPtr.Size + 1] = unchecked((byte)(InternalSize >> 8)); | ||
Buffer[Buffer.Length - IntPtr.Size + 2] = unchecked((byte)(InternalSize >> 16)); | ||
Buffer[Buffer.Length - IntPtr.Size + 3] = unchecked((byte)(InternalSize >> 24)); | ||
int addressSizeOffset = GetAddressSizeOffset(); | ||
Buffer[addressSizeOffset] = unchecked((byte)(InternalSize)); | ||
Buffer[addressSizeOffset + 1] = unchecked((byte)(InternalSize >> 8)); | ||
Buffer[addressSizeOffset + 2] = unchecked((byte)(InternalSize >> 16)); | ||
Buffer[addressSizeOffset + 3] = unchecked((byte)(InternalSize >> 24)); | ||
} | ||
|
||
// Can be called after the above method did work. | ||
internal int GetAddressSizeOffset() | ||
{ | ||
return Buffer.Length - IntPtr.Size; | ||
} | ||
#endif | ||
|
||
public override bool Equals(object? comparand) => | ||
comparand is SocketAddress other && | ||
|
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.
nit: forumla -> formula