Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref(cache): use Map instead of object to store JSMol cache #43

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/worker/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ import { JSMol } from '@rdkit/rdkit';

export interface RDKitWorkerGlobals {
jsMolCacheEnabled: boolean;
jsMolCache: Record<string, JSMol> | null;
jsMolCache: Map<string, JSMol> | null;
maxJsMolsCached: number;
}
12 changes: 5 additions & 7 deletions src/worker/utils/caching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,29 @@ export const storeJSMolInCache = (smiles: string, mol: JSMol) => {
const nbCachedMolecules = Object.keys(globalThis.rdkitWorkerGlobals.jsMolCache).length;
if (nbCachedMolecules > globalThis.rdkitWorkerGlobals.maxJsMolsCached) {
cleanJSMolCache();
globalThis.rdkitWorkerGlobals.jsMolCache = { [smiles]: mol };
return;
}
try {
globalThis.rdkitWorkerGlobals.jsMolCache[smiles] = mol;
globalThis.rdkitWorkerGlobals.jsMolCache.set(smiles, mol);
} catch (e) {
console.error(e);
cleanJSMolCache();
globalThis.rdkitWorkerGlobals.jsMolCache = { [smiles]: mol };
globalThis.rdkitWorkerGlobals.jsMolCache.set(smiles, mol);
}
};

export const getJSMolFromCache = (smiles: string) => {
if (!globalThis.rdkitWorkerGlobals.jsMolCacheEnabled || !globalThis.rdkitWorkerGlobals.jsMolCache) {
return null;
}
return globalThis.rdkitWorkerGlobals.jsMolCache[smiles];
return globalThis.rdkitWorkerGlobals.jsMolCache.get(smiles);
};

export const cleanJSMolCache = () => {
if (!globalThis.rdkitWorkerGlobals?.jsMolCache) return;
for (const [smiles, mol] of Object.entries(globalThis.rdkitWorkerGlobals.jsMolCache)) {
for (const [smiles, mol] of globalThis.rdkitWorkerGlobals.jsMolCache.entries()) {
try {
mol.delete();
delete globalThis.rdkitWorkerGlobals.jsMolCache[smiles];
globalThis.rdkitWorkerGlobals.jsMolCache.delete(smiles);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • TODO: use Map.clear at the end of the loop, instead of n calls to delete

Copy link
Contributor

@atfera atfera Apr 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about something like this:

class MolCache extends Map {
  set(smiles) {
    this.set(smiles, rdkit.get_mol(smiles))
  }
  
  delete(k) {
    this.get(k).delete() // delete rdkit mol
    this.delete(k)
  }
  clear() {
    this.forEach((v, k) => {
      v.delete();
      this.delete(k); 
    )}
  }
}

// MolCache
cache.set('C')
cache.clear() //

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, I'll do that

} catch {
// multiple cleanJSMolCache could be called in the same time, => avoid calling delete on the same mol
}
Expand Down
2 changes: 1 addition & 1 deletion src/worker/utils/initRDKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const initWorkerCache = (cache: RDKitProviderCacheOptions) => {
if (enableJsMolCaching) {
globalThis.rdkitWorkerGlobals = {
jsMolCacheEnabled: !!enableJsMolCaching,
jsMolCache: enableJsMolCaching ? {} : null,
jsMolCache: enableJsMolCaching ? new Map() : null,
maxJsMolsCached: maxJsMolsCached ?? MAX_CACHED_JSMOLS,
};
}
Expand Down