The socket classes in .NET have got some new async methods (like Socket.ReceiveAsync
).
I'm trying to understand their purpose. As I understand it, they were created to avoid making a new IAsyncResult
object for each operation.
Let's say that I were to create a high performance HTTP server. Then I need to create a Request or a Response object for each operation. And the request or response objects certainly have some properties that might be other classes too (or just primitives + string). And I might have to fetch information from a database (more objects to create).
My point is that quite many objects can be created per request/reply. Is the AsyncResult
objects so heavy that it would affect performance of a complete server? Or do MS mean that I should use flyweight pattern (reusing request/reply objects instead of allocating new ones) for all my objects in the server ?
Please enlighten me.
From MSDN about the new Async methods:
The main feature of these enhancements is the avoidance of the repeated allocation and synchronization of objects during high-volume asynchronous socket I/O. The Begin/End design pattern currently implemented by the System.Net.Sockets.Socket class requires a System.IAsyncResult object be allocated for each asynchronous socket operation
Source: http://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.aspx
This question is not a duplicate. I'm not asking about the difference. I'm well aware of the difference. If they added the methods to reduce allocations and the work for the GC, should I do the same in my protocol layer on top of the socket handling? i.e. should I use the flyweight pattern for objects like HttpReqest
etc.
Well in one socket lifetime each begin/end operation will allocate new sync object.
For example, your logic is,
// GET /default.aspx HTTP/1.1<cr-lf>
ReadLine for Http Verb and Version
// Headers
ReadLine till you get empty line and process header
// Data
Read or process mime data
Now if you notice, we will never read everything in one begin/end call instead each operation will invoke multiple begin/end which will create new sync object.
But these all steps are for only one client/server communication.
So your request/response object will be only one throughout the lifetime of socket and in this case you are better off using new Async method and leave your request/response object as just one single object.
When you have a server processing 1000 requests simultaneously this will certainly impact performance. Even if sync object takes 100 bytes but allocation/reallocation, gc processor usage all will affect for 1000 simultaneous operations.
However well designed algorithms exist for memory management, if your server will run continuously for long time, it will certainly cause fragmentation and it does slow down.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With