do Socket.Receive/SendAsync(Memory<byte>) allocate memory on each call? #104475
-
Specifically I am referring to allocating new AwaitableSocketAsyncEventArgs. When I read this in the Socket.Tasks file it looked like there would be some reuse: However, It seems every use sets it to null and do nothing with the previous value (as its null). This pattern in ConnectAsync is the same in multiple places, including ReceiveAsync. Sets it to null and if the previous value was null creates a new instance (but does not set the field): Sets it to null and if the previous was not null dispose it: Sets it to null if the previous value matched the current instance + disposes the current instance: runtime/src/libraries/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs Lines 1032 to 1036 in 8bcbe18 Copies the existing value of the field to another instance: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You did read the |
Beta Was this translation helpful? Give feedback.
You did read the
Interlocked.CompareExchange
wrong. The last parameter inInterlocked.CompareExchange(ref cache, this, null)
is the value compared against. Therefore, it sets cache to this if the value of cache is null (and disposes the current instance if there is already some non-null instance in cache).