I'm in the process of trying to optimize a few older projects, making sure they're "async all the way" and won't crash under heavier loads.
I've used variations of the snippet below and feel unsure if the Task.Run
is an optimization or a possible bottleneck. This method gets quite heavy use in some of the larger forms.
public static Task<List<SelectListItem>> ToMultipleSelectListItems<T>(this List<T> items, Func<T, string> nameSelector, Func<T, Guid> valueSelector, List<Guid> selected, Func<T, string> orderBy)
{
Task<List<SelectListItem>> selectList = Task.Run(() => items.OrderBy(orderBy).Select(item =>
new SelectListItem
{
Text = nameSelector(item),
Value = valueSelector(item).ToString(),
Selected = selected.Contains(valueSelector(item))
}).ToList());
return selectList;
}
Example call..
model.Abilities = await (await Api.GetAbilities()).ToMultipleSelectListItems(
x => x.Name,
x => x.Id,
model.Task.SelectedAbilitiesList,
x => x.OrderBy.ToString()
);
As I see it, the current thread would still need to wait for the new threads response before returning. So, under some load this would possibly create a strain on the CPU and perhaps even max out threads. I fail to see an upside.
Any feedback on best practice in this scenario would be appreciated.