Здравствуйте,
Task func() => throw new Exception();
//---
// 1
Task method() => func();
// vs
// 2
async Task method3() => await func();
Какой код вы предподчитаете использовать? В первом варианте нет оверхеда ввиде `await` со стейт машиной. Во втором лучше колл стек трейс.
Здравствуйте, yenik, Вы писали:
Y>Cтек-трейс — это хороший довод. Если func подозрительна в смысле ошибок, то лучше не экономить, а предпочесть стек-трейс. Особенно если таких функций немало.
Y>Но если 100500 экземпляров класса, то, может быть, лучше сэкономить и пренебречь стек-трейсом.
Нашел много комментириев в нете в пользую возвращения таски без async(ради оптимизации и потому что стектрейс создан не для того что бы по нему диагностировать коллстек функций). Самый весомый:
https://stackoverflow.com/questions/19098143/what-is-the-purpose-of-return-await-in-c#answer-19098209
J>Нашел много комментириев в нете в пользую возвращения таски без async. Самый весомый: https://stackoverflow.com/questions/19098143/what-is-the-purpose-of-return-await-in-c#answer-19098209
Автор в своём блоге пишет, что тут не всё так однозначно.
Efficiency
It’s more efficient to elide async and await. By not including these keywords, the compiler can skip generating the async state machine. This means that there are fewer compiler-generated types in your assembly, less pressure on the garbage collector, and fewer CPU instructions to execute.
However, it’s important to point out that each of these gains are absolutely minimal. There’s one fewer type, a handful of small objects saved from GC, and only a few CPU instructions skipped. The vast majority of the time, async is dealing with I/O, which completely dwarfs any performance gains. In almost every scenario, eliding async and await doesn’t make any difference to the running time of your application.
For a thorough overview of the efficiency benefits of eliding async and await, see Stephen Toub’s classic video The Zen of Async or his MSDN article on the subject.
When I started writing about async, I would always recommend eliding async and await, but I’ve modified that stand in recent years. There are just too many pitfalls to recommend eliding as a default decision. These days I recommend keeping the async and await keywords around except for a few scenarios, because of the drawbacks described in the rest of this blog post.
Pitfalls
By eliding async and await, you can avoid the compiler modifications to your method. Unfortunately, this also means that all the compiler modifications to your method must now be done by hand if you desire the same semantics.
Далее он перечисляет колдобины.