Double-wait operations during POST

Using C # HttpClient for POST data, hypothetically, I am also concerned about the returned content. I am optimizing my application and trying to understand the impact of the performance of two waiting calls in the same way. This question arose from the following code snippet,

public static async Task<string> AsyncRequest(string URL, string data = null) { using (var client = new HttpClient()) { var post = await client.PostAsync(URL, new StringContent(data, Encoding.UTF8, "application/json")).ConfigureAwait(false); post.EnsureSuccessStatusCode(); var response = await post.Content.ReadAsStringAsync(); return response; } } 

Suppose I have error handling :) I know that calls are expensive, so double waiting has caught my attention.

  • Upon completion of the first wait, the POST response in memory will be more efficient to return the result directly, for example var response = post.Content.ReadAsStringAsync().Result;
  • What are the performance considerations when making two pending / asynchronous calls with the same method?
  • Will the above code be the result of a wait thread (2 threads) or 1 thread for a returned task that will handle both call waiting?
+6
source share
1 answer

I know that calls are expensive, so double attention is waiting for me.

Why did you say they are expensive? The state apparatus created by the compiler is a highly optimized beast that ensures that it does not inflate memory. Depending on the specifics, for example, where the TaskAwaiter returned from Task is a struct and not a class , so it will not be allocated on the heap. As @usr points out, and this is very good, the cost of sending a request over the wire will cause any state machine to be inactive.

it would be more efficient to return the result directly, for example var response = post.Content.ReadAsStringAsync().Result;

Marking your async method is enough for the compiler to create the state machine. Stack variables will already be raised to the created state. As soon as your first await hits, the rest of your code turns into a continuation anyway. Using post.Content.ReadAsStringAsync().Result; are more likely to lock your code and then save you memory usage or make your code a micro millisecond faster.

What are the performance considerations when creating two wait / asynchronous calls to the same method?

What you should ask yourself in terms of performance is this: will concurrency be a problem in my application, what makes it worthwhile to use asynchronous operations?

async shines in places where a large number of consumers will amaze you, and you want to make sure that you have enough resources to handle these requests. I see that people ask many times: "Why doesn't this asynchronous code make my code faster?". This will not lead to noticeable changes unless you are under heavy traffic heavy enough to, for example, underline your IIS thread pool, where it really benefits from asynchrony.

Will the above code lead to a thread on hold (2 threads) or 1 thread for a returned task that will handle both call waiting?

Depends on your environment. When your first await hits, you explicitly tell it not to marshal any synchronization context with ConfigureAwait(false) . For example, if you use this from a user interface thread, then any code after PostAsync will run in the workflow thread of the thread. Again, this should not bother you, these are micro-optimizations from which you will not see any benefit.

+7
source

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


All Articles