From 33bb3c642ae8261588a1484ae6a1c16daa2c7631 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ulysse=20G=C3=A9rard?= Date: Sat, 23 Nov 2024 12:06:12 +0100 Subject: [PATCH] Remove number of filtering passes --- bin/db_worker/db_worker.ml | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/bin/db_worker/db_worker.ml b/bin/db_worker/db_worker.ml index 676715e..6a376db 100644 --- a/bin/db_worker/db_worker.ml +++ b/bin/db_worker/db_worker.ml @@ -73,27 +73,30 @@ module Worker () = struct | None_of _ -> failwith "not implemented" in let keys = - List.fold_left filters ~init:keys ~f:(fun keys -> function + Array.filter keys + ~f:(fun + { + Db.Generic_schema.Track.Key.name; + Db.Generic_schema.Track.Key.genres; + _; + } + -> + let genres = Int.Set.of_list genres in + List.fold_left filters ~init:true ~f:(fun acc -> function | Db.View.Search sub when not (String.is_empty sub) -> let sub = String.lowercase_ascii sub in let pattern = String.Find.compile (Printf.sprintf "%s" sub) in - Array.filter - keys (* TODO EXTRACT THE filter from the loop if possible *) - ~f:(fun { Db.Generic_schema.Track.Key.name; _ } -> let name = String.lowercase_ascii name in - String.Find.find ~pattern name >= 0) + acc && String.Find.find ~pattern name >= 0 | Genres (One_of one_of) -> - Array.filter keys - ~f:(fun { Db.Generic_schema.Track.Key.genres; _ } -> - let genres = Int.Set.of_list genres in - Int.Set.is_empty one_of + acc + && (Int.Set.is_empty one_of || not (Int.Set.disjoint genres one_of)) | Genres (None_of none_of) -> - Array.filter keys - ~f:(fun { Db.Generic_schema.Track.Key.genres; _ } -> - let genres = Int.Set.of_list genres in - Int.Set.is_empty none_of || Int.Set.disjoint genres none_of) - | _ -> keys) + acc + && (Int.Set.is_empty none_of + || Int.Set.disjoint genres none_of) + | _ -> true)) in Console.log [ "Filter took "; Performance.now_ms G.performance -. n; " ms" ];