c# - Double await operations during POST -


using c# httpclient post data, hypothetically i'm concerned returned content. i'm optimizing app , trying understand performance impact of 2 await calls in same method. question popped 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;     } } 

assume have error handling in there :) know await calls expensive double await caught attention.

  • after first await completes post response in memory more efficient return result directly, var response = post.content.readasstringasync().result;
  • what performance considerations when making 2 await/async calls in same method?
  • will above code result in thread per await (2 threads), or 1 thread returned task handle both await calls?

i know await calls expensive double await caught attention.

why they're expensive? compiler generated state-machine highly optimized beast makes sure doesn't bloat memory. down specifics, example, taskawaiter returned task struct , not class won't allocated on heap. @usr points out, , right, cost of sending request over-the-wire turn state-machine neglectable.

would more efficient return result directly, var response = post.content.readasstringasync().result;

marking method async enough compiler generate state machine. stack variables lifted state-machine created. once first await hit, rest of code turns continuation anyway. using post.content.readasstringasync().result; more cause deadlock code rather save memory consumption or make code micro-millisecond faster.

what performance considerations when making 2 await/async calls in same method?

what should asking performance perspective - concurrency going issue within application makes worth using asynchronous operations?

async shines in places large amount of consumers hitting you, , want make sure have enough available resources process requests. see people ask many times "why isn't async code making code go faster?". won't make noticeable change unless going under heavy traffic, heavy enough example, you'll stressing out iis thread-pool benefit asynchrony.

will above code result in thread per await (2 threads), or 1 thread returned task handle both await calls?

depends on environment. when first await hit, explicitly tell not marshal synchronization context configureawait(false). if you're running ui thread, example, code after postasync running on thread-pool worker thread. again, shouldn't concern you, these micro-optimizations won't seeing benefit from.


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -