From f48372f7b3e553deb0f118b17c0a462be8b75dea Mon Sep 17 00:00:00 2001 From: Claudio Ortolina Date: Tue, 10 Dec 2024 14:19:37 +0300 Subject: [PATCH] Use MapSets for performance Not noticeable with current database size, but lays a good foundation for larger libraries. For example, given a list of 300 artist IDs, checking if an ID is in the list takes between 30 and 40 ms (for an ID towards the end of the list), while the MapSet version takes consistently between 15 and 20 ms. --- lib/music_library/collection.ex | 2 +- lib/music_library/records.ex | 2 +- lib/music_library/wishlist.ex | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/music_library/collection.ex b/lib/music_library/collection.ex index eb0d6c1f..02ec4756 100644 --- a/lib/music_library/collection.ex +++ b/lib/music_library/collection.ex @@ -56,7 +56,7 @@ defmodule MusicLibrary.Collection do where: fragment("records.purchased_at IS NOT NULL"), select: r.value - Repo.all(q) + q |> Repo.all() |> MapSet.new() end defp base_search do diff --git a/lib/music_library/records.ex b/lib/music_library/records.ex index 4d11f850..376a844f 100644 --- a/lib/music_library/records.ex +++ b/lib/music_library/records.ex @@ -103,7 +103,7 @@ defmodule MusicLibrary.Records do def get_all_artist_ids do q = from ar in ArtistRecord, distinct: true, select: ar.musicbrainz_id - Repo.all(q) + q |> Repo.all() |> MapSet.new() end def get_artist_records(musicbrainz_id) do diff --git a/lib/music_library/wishlist.ex b/lib/music_library/wishlist.ex index bcb19192..22281883 100644 --- a/lib/music_library/wishlist.ex +++ b/lib/music_library/wishlist.ex @@ -27,7 +27,7 @@ defmodule MusicLibrary.Wishlist do where: fragment("records.purchased_at IS NULL"), select: r.value - Repo.all(q) + q |> Repo.all() |> MapSet.new() end defp base_search do