From d841ae5a604e56351e5cf14b6e8fd84ef3a1e5bb Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Tue, 3 Sep 2024 14:43:59 +0530 Subject: [PATCH] Clear cache only on new index generation --- .../Search/SearchBar/searchInput/index.tsx | 2 -- web/apps/photos/src/services/searchService.ts | 5 ----- web/packages/new/photos/services/ml/clip.ts | 16 ++++++++-------- web/packages/new/photos/services/ml/index.ts | 6 ------ web/packages/new/photos/services/ml/worker.ts | 10 +++------- 5 files changed, 11 insertions(+), 28 deletions(-) diff --git a/web/apps/photos/src/components/Search/SearchBar/searchInput/index.tsx b/web/apps/photos/src/components/Search/SearchBar/searchInput/index.tsx index a41983d5c3..18b7289915 100644 --- a/web/apps/photos/src/components/Search/SearchBar/searchInput/index.tsx +++ b/web/apps/photos/src/components/Search/SearchBar/searchInput/index.tsx @@ -17,7 +17,6 @@ import AsyncSelect from "react-select/async"; import { InputActionMeta } from "react-select/src/types"; import { City } from "services/locationSearchService"; import { - clearSearchCaches, getAutoCompleteSuggestions, getDefaultOptions, } from "services/searchService"; @@ -98,7 +97,6 @@ export default function SearchInput(props: Iprops) { props.setIsOpen(false); setValue(null); setQuery(""); - clearSearchCaches(); } }; diff --git a/web/apps/photos/src/services/searchService.ts b/web/apps/photos/src/services/searchService.ts index 35e4015cc1..76b1d5cb2b 100644 --- a/web/apps/photos/src/services/searchService.ts +++ b/web/apps/photos/src/services/searchService.ts @@ -2,7 +2,6 @@ import { isDesktop } from "@/base/app"; import log from "@/base/log"; import { FileType } from "@/media/file-type"; import { - clearCachedCLIPIndexes, clipMatches, isMLEnabled, isMLSupported, @@ -62,10 +61,6 @@ export const getAutoCompleteSuggestions = } }; -export const clearSearchCaches = async () => { - await clearCachedCLIPIndexes(); -}; - async function convertSuggestionsToOptions( suggestions: Suggestion[], ): Promise { diff --git a/web/packages/new/photos/services/ml/clip.ts b/web/packages/new/photos/services/ml/clip.ts index c4e9ff6692..11058ec4c5 100644 --- a/web/packages/new/photos/services/ml/clip.ts +++ b/web/packages/new/photos/services/ml/clip.ts @@ -185,7 +185,6 @@ export const clipMatches = async ( // This code is on the hot path, so these optimizations help. [fileID, dotProductF32(embedding, textEmbedding)] as const, ); - // This score threshold was obtain heuristically. 0.2 generally gives solid // results, and around 0.15 we start getting many false positives (all this // is query dependent too). @@ -204,14 +203,15 @@ let _cachedCLIPIndexes: * Dot product performance]). But doing that each time loses out on the * amortized benefit, so this temporary cache is as attempt to alleviate that. * - * Once the user is done searching (for now), call - * {@link clearCachedCLIPIndexes}. + * Use {@link clearCachedCLIPIndexes} to clear the cache (e.g. after indexing + * produces potentially new CLIP indexes). */ const cachedOrReadCLIPIndexes = async () => - _cachedCLIPIndexes ?? - (await clipIndexes()).map(({ fileID, embedding }) => ({ - fileID, - embedding: new Float32Array(embedding), - })); + (_cachedCLIPIndexes ??= (await clipIndexes()).map( + ({ fileID, embedding }) => ({ + fileID, + embedding: new Float32Array(embedding), + }), + )); export const clearCachedCLIPIndexes = () => (_cachedCLIPIndexes = undefined); diff --git a/web/packages/new/photos/services/ml/index.ts b/web/packages/new/photos/services/ml/index.ts index 24305fdba6..97ed45c7ea 100644 --- a/web/packages/new/photos/services/ml/index.ts +++ b/web/packages/new/photos/services/ml/index.ts @@ -602,12 +602,6 @@ export const clipMatches = ( ): Promise => worker().then((w) => w.clipMatches(searchPhrase)); -/** - * Clear any cached intermediate state created during a search session. - */ -export const clearCachedCLIPIndexes = () => - worker().then((w) => w.clearCachedCLIPIndexes()); - /** * Return the IDs of all the faces in the given {@link enteFile} that are not * associated with a person cluster. diff --git a/web/packages/new/photos/services/ml/worker.ts b/web/packages/new/photos/services/ml/worker.ts index 6408188769..8a80842a07 100644 --- a/web/packages/new/photos/services/ml/worker.ts +++ b/web/packages/new/photos/services/ml/worker.ts @@ -196,13 +196,6 @@ export class MLWorker { return clipMatches(searchPhrase, ensure(this.electron)); } - /** - * Clear cached intermediate state preserved during a search "session". - */ - clearCachedCLIPIndexes() { - clearCachedCLIPIndexes(); - } - private async tick() { log.debug(() => [ "ml/tick", @@ -364,6 +357,9 @@ const indexNextBatch = async ( // Wait for the pending tasks to drain out. await Promise.all(tasks); + // Clear any cached CLIP indexes, since now we might have new ones. + clearCachedCLIPIndexes(); + // Return true if nothing failed. return allSuccess; };