0

I have this (below) process where it collects search results returned from a service. Each result is then added to UI for display.

Can this be improved so that the _list can be processed in parallel (perhaps using multiple threads?), therefore I get faster results?

List<Query> queries = _list.Where(x => string.IsNullOrEmpty(x.Title));

foreach (var item in queries)
{
    List<ExtendedSearchResult> searchResults = (await _service.SearchAsync(item.Query))
                                .Select(x => ExtendedSearchResult.FromSearchResult(x))
                                .ToList();

    if (searchResults != null)
    {
        foreach (var result in searchResults)
        {
            _view.AddItem(result);
        }
    }
}

Found this post but not sure if this applies to my scenario and how to implement it.

yonan2236
  • 13,371
  • 33
  • 95
  • 141
  • 1
    You could try, `var results = await Task.WhenAll(queries.Select(x => _service.SearchAsync(x.Query))))` – TheGeneral Aug 19 '21 at 09:37
  • We have no idea what `_service.SearchAsync` actually does, but you could potentially create a list of `Task` of these calls and then await Task.WhenAll(tasks)` on that. – DavidG Aug 19 '21 at 09:37
  • @TheGeneral - I'll try your suggestion – yonan2236 Aug 19 '21 at 09:42
  • I see that you try to find query without `Title` and than ask for query results. Maybe you can compare query and avoid duplicated once (if these kinds exists)? – hsd Aug 19 '21 at 09:52

1 Answers1

0

Can this be improved so that the _list can be processed in parallel (perhaps using multiple threads?)

Maybe? there is not really anyway to tell from the example. Is the performed work IO-bound or compute bound? The searching might need to take some kind of lock to gain exclusive access to some resource, if that is the case parallelism would do nothing except increase overhead.

As a rule of thumb, parallelism is best for compute bound tasks, while async is best for IO bound tasks. But often a combination can be useful, modern SSDs are inherently parallel, and compute bound tasks are often done in the background to avoid blocking the main thread.

If you want to make this code parallel you need to make it thread-safe, i.e. make sure any shared objects are threadsafe, and ensure the UI is only updated on the main-thread.

There are plenty of resources available for running tasks concurrently. For example using async await for multiple tasks. As an alternative I would consider IAsynEnumerable to update the UI as results are returned.

JonasH
  • 28,608
  • 2
  • 10
  • 23