New Async Methods and Performance

Possible duplicate:
Is there any performance difference between Begin * and Async * for sockets in .NET?

Possible duplicate:
What is the difference between BeginConnect and ConnectAsync?

There are several new asynchronous methods in socket classes in .NET (for example, Socket.ReceiveAsync ).

I try to understand their purpose. As I understand it, they were created to avoid creating a new IAsyncResult object for each operation.

Let's say that I have to create a high-performance HTTP server. Then I need to create a Request or Response object for each operation. And the request or response objects, of course, have some properties, which can be other classes (or just primitives + string). And I may have to retrieve information from the database (more objects to create).

My point is that quite a lot of objects can be created for each request / response. Are AsyncResult objects so heavy that it will affect the performance of a full server? Or does MS mean that I have to use a flyweight pattern (reusing request / response objects instead of allocating new ones) for all my objects on the server?

Please enlighten me.

Update

From MSDN about new Async methods:

The main feature of these improvements is the prevention of re-allocation and synchronization of objects during high-performance asynchronous input / output sockets. The Begin / End design pattern, which is currently implemented by the System.Net.Sockets.Socket class, requires that a System.IAsyncResult object be assigned for each asynchronous socket operation.

Source: http://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.aspx

Update2

This question is not a duplicate. I do not ask about the difference. I know the difference well. If they added methods to reduce allocations and work for the GC, should I do the same in my protocol level on top of socket processing? those. use a fly template for objects like HttpReqest , etc.

+6
source share
2 answers

It is good that during one socket each start / end operation will allocate a new synchronization object.

For example, your logic:

 // 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 start / end call, instead, each operation calls several begin / end, which will create a new synchronization object.

But all these steps are for only one client / server interaction.

Thus, your request / response object will be only one throughout the life of the socket, in which case you better use the new Async method and leave the request / response object as only one object.

When the server processes 1000 requests at the same time, it will certainly affect performance. Even if the sync object accepts 100 bytes, but the distribution / redistribution, the use of the gc processor will all affect 1000 simultaneous operations.

However, there are well-developed algorithms for memory management, if your server runs continuously for a long time, this will certainly cause fragmentation and slow down the work.

+2
source

There was no direct answer to your question, but there were some good presentations on asynchronous analysis. You will need to create a login to view them

DEV324 C # and the Future of Visual Basic: Async Made Simple Wednesday, May 18th.

DEV335 Improving Microsoft ASP.NET Application Performance with Asynchronous Pages and Actions Thursday, May 19th.

0
source

Source: https://habr.com/ru/post/889690/


All Articles