Closed antonfirsov closed 3 months ago
Hey Anton, For the first part, I was actually using socket directly for the longest time. I just recently changed the implementation to use UdpClient directly because there is actually a memory leak when using Socket directly under full framework only (net4.8 for example). see #192 That scared me a bit because I couldn't really figure out a good fix or workaround either.
Those changes are not release yet, so if you have an idea, let me know!
Regarding part 2) Yeah I think you are right, we should change it to use the shared array pool.
Regarding a PR, yeah go for it, I'm eager to see what you have in mind.
I noticed some opportunities to significantly reduce the GC allocations done by the library, particularly:
UdpClient
allocates a new array on every receive, which is responsible for the vast majority of allocations done by the lib. UsingSocket
instead would enable pooling the receive buffer or even usingstackalloc
.PooledBytes
is using a custom array pool. UsingArrayPool<byte>.Shared
would enable better recycling of arrays between DnsClient.NET and other libraries including the BCL which uses the shared pool exclusively. Moreover, with some minor refactoringPooledBytes
could be made a struct itself.According to my experiments the improvement potential is huge.
If you are interested taking the suggested changes, I can create a PR in the coming weeks.