Clear cache only on new index generation

This commit is contained in:
Manav Rathi
2024-09-03 14:43:59 +05:30
parent 5e65001e37
commit d841ae5a60
5 changed files with 11 additions and 28 deletions

View File

@@ -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();
}
};

View File

@@ -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<SearchOption[]> {

View File

@@ -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);

View File

@@ -602,12 +602,6 @@ export const clipMatches = (
): Promise<CLIPMatches | undefined> =>
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.

View File

@@ -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;
};