Skip to content

Commit 6bcbf88

Browse files
authored
Merge pull request #335 from nellh/memoize-fixes
fix: Add memoize resolver option and default to serialization of all arguments
2 parents 6537476 + 37a1d30 commit 6bcbf88

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!--
2+
A new scriv changelog fragment.
3+
4+
Uncomment the section that is right (remove the HTML comment wrapper).
5+
For top level release notes, leave all the headers commented out.
6+
-->
7+
8+
<!--
9+
### Added
10+
11+
- A bullet item for the Added category.
12+
13+
-->
14+
<!--
15+
### Changed
16+
17+
- A bullet item for the Changed category.
18+
19+
-->
20+
### Fixed
21+
22+
- Fix memoization memory leak for readRemotes.
23+
24+
<!--
25+
### Deprecated
26+
27+
- A bullet item for the Deprecated category.
28+
29+
-->
30+
<!--
31+
### Removed
32+
33+
- A bullet item for the Removed category.
34+
35+
-->
36+
<!--
37+
### Security
38+
39+
- A bullet item for the Security category.
40+
41+
-->
42+
<!--
43+
### Infrastructure
44+
45+
- A bullet item for the Infrastructure category.
46+
47+
-->

src/files/repo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ async function _readRemotes(options: any): Promise<Record<string, Record<string,
157157
return byUUID
158158
}
159159

160-
const readRemotes = memoize(_readRemotes)
160+
const readRemotes = memoize(_readRemotes, (options) => options?.gitdir)
161161

162162
export async function parseAnnexedFile(
163163
path: string,

src/schema/datatypes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function _modalityTable(schema: Schema): Record<string, string> {
1414
}
1515

1616
// Construct once per schema; should only be multiple in tests
17-
export const modalityTable = memoize(_modalityTable)
17+
export const modalityTable = memoize(_modalityTable, (schema) => schema)
1818

1919
export function findDatatype(
2020
file: BIDSFile,

src/utils/memoize.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
export type WithCache<T> = T & { cache: Map<string, any> }
1+
export type WithCache<T> = T & { cache: Map<any, any> }
22
interface FileLike {
33
path: string
44
parent: { path: string }
55
}
66

77
export const memoize = <T>(
88
fn: (...args: any[]) => T,
9+
resolver?: (...args: any[]) => any,
910
): WithCache<(...args: any[]) => T> => {
1011
const cache = new Map()
11-
const cached = function (this: any, val: T) {
12-
return cache.has(val) ? cache.get(val) : cache.set(val, fn.call(this, val)) && cache.get(val)
12+
const cached = function (this: any, ...args: any[]) {
13+
const key = resolver ? resolver(...args) : JSON.stringify(args)
14+
return cache.has(key) ? cache.get(key) : cache.set(key, fn.apply(this, args)) && cache.get(key)
1315
}
1416
cached.cache = cache
1517
return cached

0 commit comments

Comments
 (0)