Skip to content

Fix memory leak #60

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
56 changes: 35 additions & 21 deletions src/memoize-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ interface MemoizeArgs {
tags?: string[];
}

interface CacheValue {
resultsMap: Map<any,any>;
tagVersions?: Record<string, symbol>;
}

export function Memoize(args?: MemoizeArgs | MemoizeArgs['hashFunction']) {
let hashFunction: MemoizeArgs['hashFunction'];
let duration: MemoizeArgs['expiring'];
Expand Down Expand Up @@ -35,50 +40,59 @@ export function MemoizeExpiring(expiring: number, hashFunction?: MemoizeArgs['ha
});
}

const clearCacheTagsMap: Map<string, Map<any, any>[]> = new Map();
const latestTagVersions: Map<string, symbol> = new Map();

export function clear (tags: string[]): number {
const cleared: Set<Map<any, any>> = new Set();
export function clear (tags: string[]): void {
for (const tag of tags) {
const maps = clearCacheTagsMap.get(tag);
if (maps) {
for (const mp of maps) {
if (!cleared.has(mp)) {
mp.clear();
cleared.add(mp);
}
}
if (latestTagVersions.has(tag)) {
latestTagVersions.set(tag, Symbol());
}
}
return cleared.size;
}

function getLatestTagVersionsForTags (tags: string[]) {
return tags.reduce<Record<string, symbol>>((acc, tag) => {
if (!latestTagVersions.has(tag)) {
const symbolForTag = Symbol();
latestTagVersions.set(tag, symbolForTag);
return Object.assign(acc, { [tag]: symbolForTag });
}
return Object.assign(acc, { [tag]: latestTagVersions.get(tag) });
}, {});
}

function getNewFunction(originalMethod: () => void, hashFunction?: MemoizeArgs['hashFunction'], duration: number = 0, tags?: MemoizeArgs['tags']) {
const propMapName = Symbol(`__memoized_map__`);
const propMapName = Symbol(`__cache__`);

// The function returned here gets called instead of originalMethod.
return function (...args: any[]) {
let returnedValue: any;

// Get or create map
if (!this.hasOwnProperty(propMapName)) {
const value: CacheValue = { resultsMap: new Map<any, any>() };
if (Array.isArray(tags)) {
value.tagVersions = getLatestTagVersionsForTags(tags);
}
Object.defineProperty(this, propMapName, {
configurable: false,
enumerable: false,
writable: false,
value: new Map<any, any>()
value,
});
}
let myMap: Map<any, any> = this[propMapName];

const cache = this[propMapName] as CacheValue;
let myMap: Map<any, any> = cache.resultsMap;

if (Array.isArray(tags)) {
for (const tag of tags) {
if (clearCacheTagsMap.has(tag)) {
clearCacheTagsMap.get(tag).push(myMap);
} else {
clearCacheTagsMap.set(tag, [myMap]);
}
const tagVersions = this[propMapName].tagVersions;
const isAtLeastOneTagStale = tags.some((tag) => tagVersions[tag] !== latestTagVersions.get(tag));
if (isAtLeastOneTagStale) {
myMap.clear();
cache.tagVersions = getLatestTagVersionsForTags(tags);
}

}

if (hashFunction || args.length > 0 || duration > 0) {
Expand Down
8 changes: 5 additions & 3 deletions test/specs/memoize-decorator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,20 @@ describe('Memoize()', () => {
let val3 = a.getGreeting4('Hello', 'World');
clear(["foo"]);
let val4 = a.getGreeting4('Hello', 'Moon');
let val5 = a.getGreeting4('Hello', 'World');
clear(["bar"]);
let val5 = a.getGreeting4('Hello', 'Moon');
let val6 = a.getGreeting4('Hello', 'World');
clear(["bar"]);
let val7 = a.getGreeting4('Hello', 'World');

clear(["unknown"]);

expect(val1).toEqual('Hello, World');
expect(val2).toEqual('Hello, Moon');
expect(val3).toEqual('Hello, World');
expect(val4).toEqual('Hello, Moon');
expect(val5).toEqual('Hello, World');
expect(val5).toEqual('Hello, Moon');
expect(val6).toEqual('Hello, World');
expect(val7).toEqual('Hello, World');

expect(getGreetingSpy).toHaveBeenCalledTimes(5);
});
Expand Down