i have following async method not have await calls.
public async task<bool> addcust(customer cust) { // doing synchronous operations return true; } following method calls above method , await result.
public async task<bool> parentmethod(customer cust) { var result = await addcust(cust); if(!result) // logic return true; } here caller, await addcust return now, question if keep above code negative impact on performance? if yes, why?
first of all, should use profiler check if there's significant performance hit.
at end of day, await compilation detail (also called syntactic sugar!) compiles code regular tap code (task-asynchronous pattern).
btw, code rewritten to:
public task<bool> addcust(customer cust) { // if need perform sync operations , want // retain asynchronous tap-based method signature should // go way. return task.fromresult(true); } finally, should rename async method , add async suffix. example, addcustasync, since it's naming convention. if developer checks code he/she think it's synchronous operation until he/she realizes returns task...
il comparison
i've implemented case , got resulting il.
actually can check how task.fromresult version has less il code lines async counterpart:
without async
a.test1: il_0000: nop il_0001: ldc.i4.1 il_0002: call system.threading.tasks.task.fromresult il_0007: stloc.0 // cs$1$0000 il_0008: br.s il_000a il_000a: ldloc.0 // cs$1$0000 il_000b: ret with async
a.test2: il_0000: ldloca.s 00 il_0002: ldarg.0 il_0003: stfld userquery+a+<test2>d__0.<>4__this il_0008: ldloca.s 00 il_000a: call system.runtime.compilerservices.asynctaskmethodbuilder<system.boolean>.create il_000f: stfld userquery+a+<test2>d__0.<>t__builder il_0014: ldloca.s 00 il_0016: ldc.i4.m1 il_0017: stfld userquery+a+<test2>d__0.<>1__state il_001c: ldloca.s 00 il_001e: ldfld userquery+a+<test2>d__0.<>t__builder il_0023: stloc.2 il_0024: ldloca.s 02 il_0026: ldloca.s 00 il_0028: call system.runtime.compilerservices.asynctaskmethodbuilder<system.boolean>.start il_002d: ldloca.s 00 il_002f: ldflda userquery+a+<test2>d__0.<>t__builder il_0034: call system.runtime.compilerservices.asynctaskmethodbuilder<system.boolean>.get_task il_0039: stloc.1 il_003a: br.s il_003c il_003c: ldloc.1 il_003d: ret there should no big performance hit, maybe if go async way when there's no need simplify code because there's no async operation being called in method body, you're adding compilation , run-time overhead can avoided using task.fromresult approach...
Comments
Post a Comment