-
-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Improve speed of suggestions for long words. (#2406)
* fix: add operator `tap` to pipe * fix: Workaround bad node definitions * fix: calc elapsed time even for cached items. * Fix performance issue with A Start distance calc. * Add memorizer utility * Simple AutoCache * Measure the amount of time to generate a suggestion. * Update suggestCollector.test.ts.snap * Expose opTap * Update snapshots
- Loading branch information
Showing
19 changed files
with
347 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ Array [ | |
"opFlatten", | ||
"opJoinStrings", | ||
"opMap", | ||
"opTap", | ||
"opUnique", | ||
"operators", | ||
"pipeAsync", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { opMap } from '.'; | ||
import { toArray } from '../helpers'; | ||
import { pipeAsync, pipeSync } from '../pipe'; | ||
import { opTap } from './tap'; | ||
|
||
describe('Validate map', () => { | ||
test('map', async () => { | ||
const values = ['one', 'two', 'three']; | ||
|
||
const mapFn = (v: string) => v.length; | ||
const tapFn1 = jest.fn(); | ||
const tapFn2 = jest.fn(); | ||
|
||
const expected = values.map(mapFn); | ||
const mapToLen = opMap(mapFn); | ||
|
||
const s = pipeSync(values, opTap(tapFn1), mapToLen, opTap(tapFn2)); | ||
toArray(s); | ||
expect(tapFn1.mock.calls.map((c) => c[0])).toEqual(values); | ||
expect(tapFn2.mock.calls.map((c) => c[0])).toEqual(expected); | ||
|
||
tapFn1.mockClear(); | ||
tapFn2.mockClear(); | ||
|
||
const a = pipeAsync(values, opTap(tapFn1), mapToLen, opTap(tapFn2)); | ||
await toArray(a); | ||
|
||
expect(tapFn1.mock.calls.map((c) => c[0])).toEqual(values); | ||
expect(tapFn2.mock.calls.map((c) => c[0])).toEqual(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { toPipeFn } from '../helpers/util'; | ||
|
||
/** | ||
* Tap allows you to listen on values, without modifying them. | ||
* | ||
* @param fn - function to call for each value. | ||
*/ | ||
export function opTapAsync<T>(tapFn: (v: T) => void): (iter: AsyncIterable<T>) => AsyncIterable<T> { | ||
async function* fn(iter: Iterable<T> | AsyncIterable<T>) { | ||
for await (const v of iter) { | ||
tapFn(v); | ||
yield v; | ||
} | ||
} | ||
|
||
return fn; | ||
} | ||
|
||
/** | ||
* Tap allows you to listen on values, without modifying them. | ||
* | ||
* @param fn - function to call for each value. | ||
*/ | ||
export function opTapSync<T>(tapFn: (v: T) => void): (iter: Iterable<T>) => Iterable<T> { | ||
function* fn(iter: Iterable<T>) { | ||
for (const v of iter) { | ||
tapFn(v); | ||
yield v; | ||
} | ||
} | ||
return fn; | ||
} | ||
|
||
/** | ||
* Tap allows you to listen on values, without modifying them. | ||
* | ||
* @param fn - function to call for each value. | ||
*/ | ||
export const opTap = <T>(fn: (v: T) => void) => toPipeFn<T, T>(opTapSync(fn), opTapAsync(fn)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
packages/cspell-trie-lib/src/lib/utils/autoCacheMap.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { AutoCacheMap, AutoCacheWeakMap } from './autoCacheMap'; | ||
import { isDefined } from './util'; | ||
|
||
describe('autoCacheMap', () => { | ||
test('AutoCacheMap', () => { | ||
const values = ['one', 'two', 'three', 'four', 'one', 'four', 'three', 'one', 'two', 'five']; | ||
const unique = [...new Set(values)]; | ||
function transform(s: string): number { | ||
return s.length; | ||
} | ||
|
||
const fn = jest.fn(transform); | ||
|
||
const cache = new AutoCacheMap(fn); | ||
|
||
const r = values.map((s) => cache.get(s)); | ||
expect(r).toEqual(values.map(transform)); | ||
expect(fn.mock.calls).toEqual(unique.map((c) => [c])); | ||
}); | ||
|
||
test('AutoCacheWeakMap', () => { | ||
const numbers = ['one', 'two', 'three', 'four', 'one', 'four', 'three', 'one', 'two', 'five']; | ||
const objs = new Map(numbers.map((s) => [s, { name: s }])); | ||
const values = numbers.map((n) => objs.get(n)).filter(isDefined); | ||
|
||
function transform(n: { name: string }): number { | ||
return n.name.length; | ||
} | ||
|
||
const fn = jest.fn(transform); | ||
|
||
const cache = new AutoCacheWeakMap(fn); | ||
|
||
const r = values.map((s) => cache.get(s)); | ||
expect(r).toEqual(values.map(transform)); | ||
expect(fn.mock.calls).toEqual([...objs.values()].map((c) => [c])); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
export class AutoCacheMap<T, U> extends Map<T, U> { | ||
constructor(readonly autoFn: (v: T) => U) { | ||
super(); | ||
} | ||
|
||
get(v: T): U { | ||
const r = super.get(v); | ||
|
||
if (r !== undefined) return r; | ||
|
||
const u = this.autoFn(v); | ||
super.set(v, u); | ||
return u; | ||
} | ||
} | ||
|
||
export class AutoCacheWeakMap<T extends object, U> extends WeakMap<T, U> { | ||
constructor(readonly autoFn: (v: T) => U) { | ||
super(); | ||
} | ||
|
||
get(v: T): U { | ||
const r = super.get(v); | ||
|
||
if (r !== undefined) return r; | ||
|
||
const u = this.autoFn(v); | ||
super.set(v, u); | ||
return u; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { memorizer } from './memorizer'; | ||
|
||
describe('memorizer', () => { | ||
test('memorizer', () => { | ||
const transform = (...args: string[]) => args.reduce((sum, s) => (sum += s.length), 0); | ||
|
||
const fn = jest.fn(transform); | ||
|
||
const m = memorizer(fn); | ||
|
||
const requests: string[][] = [['a'], ['a', 'b', 'c'], ['a'], ['a', 'b', 'c']]; | ||
|
||
const expected = [...new Set(requests.map((r) => r.join('|')))].map((r) => r.split('|')); | ||
|
||
requests.forEach((r) => expect(m(...r)).toBe(transform(...r))); | ||
|
||
expect(fn.mock.calls).toEqual(expected); | ||
}); | ||
|
||
test('mixed params', () => { | ||
function transform(...params: [a: string, ai: number, b: string, bi?: number | undefined]): string { | ||
const [a, ai, b, bi] = params; | ||
return `${a}: ${ai.toFixed(2)}, ${b}: ${bi?.toFixed(2) || '<>'}`; | ||
} | ||
|
||
const fn = jest.fn(transform); | ||
|
||
const m = memorizer(fn); | ||
|
||
const requests: Parameters<typeof transform>[] = [ | ||
['a', 23, 'b', undefined], | ||
['a', 23, 'b'], | ||
['a', 23, 'b', undefined], | ||
['a', 23, 'b'], | ||
['a', 23, 'b'], | ||
['b', 42, 'c', 10], | ||
['a', 23, 'b', undefined], | ||
['b', 42, 'c', 10], | ||
]; | ||
|
||
const expected = [ | ||
['a', 23, 'b', undefined], | ||
['a', 23, 'b'], | ||
['b', 42, 'c', 10], | ||
]; | ||
|
||
requests.forEach((r) => expect(m(...r)).toBe(transform(...r))); | ||
|
||
expect(fn.mock.calls).toEqual(expected); | ||
}); | ||
}); |
Oops, something went wrong.