Hey guys, im a noob who’s making a asp net core project which is basically GoodReads but for anime and manga. I’ve made a service witch finds best anime recommendations for a certain user based on what they have already viewed. How it works is I have one public method that takes in the users ID from the database. Using that I find what his top genres are and take a subset of all the anime that have at least one of those genres (since to me similar genres are the most important metric) so that I’m working with a less data. Then i create a leaderboard witch is a Dictionary<Anime, int> where int are points. Then I call all of my methods giving them leaderboard and they change it based on witch anime they have given points.

My problem is that i think this is too slow and want to make it async. I’ve made plenty of async methods but they all await some other async method. Is there a way to make this all async without awaiting anything async in the seperate method (obviously it can be done but I’m not sure how). Should i make that the body of each method is like this:
and then await each one in the public method?
Making it async won’t help improve per-request performance. The goal of async is to free threads doing IO work (such as accessing the db or network) to do other work, e.g. serving other requests at the same time.
Task.Run() works counter to this idea because it actually requires another thread instead of freeing the current one.
Edit: or do you mean you have some work you want done in parallel? In that case, you gather all the Tasks returned to you and await Task.WhenAll()
My idea is to maybe have all of them run in parallel then take the results from each one and put it in my leaderboard. Maybe i’m wrong that doing this will improve performance im still new to async programming
async won’t do you good if you’ve got a LOT of short-running CPU-bound tasks. Some operations will take time to crunch, the more you scale them. Parallelizing also means handling concurrency. If you’ve got at least one Dictionary shared between threads/Tasks there will be contention.
Remember that CPU and memory are not infinite, and simply creating tasks/threads cannot magically solve your issues, and locking on a shared resource to manage concurrency will slow you down.
There is an approach where you queue work items to a set of workers, and I’ve tried that using Channels. Basically you have a fixed set of tasks/threads that do some work. You feed them work items and they process as many as they can at their own pace.
Are you sure this is something that can’t be handled in the database side?
So you have a Dictionary<Anime, int>, then you pass it to several methods that update the dictionary. How many methods? What do those methods do? Do they call the databases? Once? Many times? Is there some intense CPU processing involved or a lot of IO to the database?
I’m trying to figure out what the cause of the performance issue is, because you need to be cross-referencing hundreds of thousands to records in each method, or doing something extremely inefficient with the database, or both.


They don’t pull from the database but the loop around all the data sometimes with nested loops and i feel all that looping can be done in parallel somehow
See answer further down in the comment section
This won’t compile. You forgot the async
But wont this just run synchronously, and if it does is it ok since when I call all the methods each of them will run asynchronously?
C# devs
null reference exceptions

source