AlexanderColen/Annie-May-Discord-Bot

Calculating affinity takes really long when there are many users or really big lists.

Opened this issue · 0 comments

Two potential bottlenecks, most likely a combination of the two in the end.

Iterating over the lists multiple times, can probably be done more simply.

var sharedMedia = mediaListA.Select(x => x.MediaId)
.Intersect(mediaListB.Select(x => x.MediaId).ToList())
.Select(id => (
id,
mediaListA.Find(x => x.MediaId == id).Score,
mediaListB.Find(x => x.MediaId == id).Score
)).Where(x => x.Item2 != 0)
.Where(x => x.Item3 != 0)
.Distinct()
.ToList();

4 GraphQL queries per user really hurts performance the more users are in the server. Quick fix is to move the original user out of this method, which already cuts it down to half.
var animeListsA = await _aniListFetcher.FindUserList(user.AnilistId, MediaType.Anime.ToString());
var animeListsB = await _aniListFetcher.FindUserList(foundUser.AnilistId, MediaType.Anime.ToString());
var mangaListsA = await _aniListFetcher.FindUserList(user.AnilistId, MediaType.Manga.ToString());
var mangaListsB = await _aniListFetcher.FindUserList(foundUser.AnilistId, MediaType.Manga.ToString());