Skip to content

Commit

Permalink
fix: Speed up dictionaries from (#2363)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Jan 30, 2022
1 parent 41c061c commit 76c41ad
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
12 changes: 5 additions & 7 deletions packages/cspell-lib/src/SpellingDictionary/Dictionaries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,7 @@ describe('Validate getDictionary', () => {

const dict = await Dictionaries.getDictionaryInternal(settings);
expect(dict.getErrors()).toEqual([expect.objectContaining(new Error('my-words: failed to load'))]);
expect(dict.dictionaries.map((d) => d.name)).toEqual([
'my-words',
'[words]',
'[userWords]',
'[ignoreWords]',
'[flagWords]',
]);
expect(dict.dictionaries.map((d) => d.name)).toEqual(['my-words', '[words]', '[ignoreWords]', '[flagWords]']);
});

test('Refresh Dictionary Cache', async () => {
Expand All @@ -165,10 +159,12 @@ describe('Validate getDictionary', () => {
]);
const toLoad = ['node', 'html', 'css', 'not_found', 'temp'];
const defsToLoad = filterDictDefsToLoad(toLoad, defs);
expect(defsToLoad.map((d) => d.name)).toEqual(['css', 'html', 'node', 'temp', 'not_found']);
const dicts = await Promise.all(Dictionaries.loadDictionaryDefs(defsToLoad));

expect(dicts[3].has('one')).toBe(true);
expect(dicts[3].has('four')).toBe(false);
expect(dicts.map((d) => d.name)).toEqual(['css', 'html', 'node', 'temp', 'not_found']);

await Dictionaries.refreshDictionaryCache(0);
const dicts2 = await Promise.all(Dictionaries.loadDictionaryDefs(defsToLoad));
Expand All @@ -185,10 +181,12 @@ describe('Validate getDictionary', () => {
// Should be using cache and will not contain the new words.
expect(dicts3[3].has('one')).toBe(true);
expect(dicts3[3].has('four')).toBe(false);
expect(dicts3.map((d) => d.name)).toEqual(['css', 'html', 'node', 'temp', 'not_found']);

await Dictionaries.refreshDictionaryCache(0);

const dicts4 = await Promise.all(Dictionaries.loadDictionaryDefs(defsToLoad));
expect(dicts4.map((d) => d.name)).toEqual(['css', 'html', 'node', 'temp', 'not_found']);
// Should be using the latest copy of the words.
expect(dicts4[3].has('one')).toBe(true);
expect(dicts4[3].has('four')).toBe(true);
Expand Down
36 changes: 16 additions & 20 deletions packages/cspell-lib/src/SpellingDictionary/Dictionaries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { CSpellSettingsInternal, DictionaryDefinitionInternal } from '../Models/CSpellSettingsInternalDef';
import { calcDictionaryDefsToLoad } from '../Settings/DictionarySettings';
import { isDefined } from '../util/util';
import { createForbiddenWordsDictionary, createSpellingDictionary } from './createSpellingDictionary';
import { loadDictionary, refreshCacheEntries } from './DictionaryLoader';
import { SpellingDictionary } from './SpellingDictionary';
Expand All @@ -19,19 +20,16 @@ export function getDictionaryInternal(settings: CSpellSettingsInternal): Promise
const { words = emptyWords, userWords = emptyWords, flagWords = emptyWords, ignoreWords = emptyWords } = settings;
const spellDictionaries = loadDictionaryDefs(calcDictionaryDefsToLoad(settings));

const settingsDictionary = createSpellingDictionary(words.concat(userWords), '[words]', 'From Settings `words`', {
const settingsWordsDictionary = createSpellingDictionary(words, '[words]', 'From Settings `words`', {
caseSensitive: true,
weightMap: undefined,
});
const settingsUserWordsDictionary = createSpellingDictionary(
userWords,
'[userWords]',
'From Settings `userWords`',
{
caseSensitive: true,
weightMap: undefined,
}
);
const settingsUserWordsDictionary = userWords.length
? createSpellingDictionary(userWords, '[userWords]', 'From Settings `userWords`', {
caseSensitive: true,
weightMap: undefined,
})
: undefined;
const ignoreWordsDictionary = createSpellingDictionary(
ignoreWords,
'[ignoreWords]',
Expand All @@ -45,14 +43,12 @@ export function getDictionaryInternal(settings: CSpellSettingsInternal): Promise
const flagWordsDictionary = createForbiddenWordsDictionary(flagWords, '[flagWords]', 'From Settings `flagWords`', {
weightMap: undefined,
});
return createCollectionP(
[
...spellDictionaries,
settingsDictionary,
settingsUserWordsDictionary,
ignoreWordsDictionary,
flagWordsDictionary,
],
'dictionary collection'
);
const dictionaries = [
...spellDictionaries,
settingsWordsDictionary,
settingsUserWordsDictionary,
ignoreWordsDictionary,
flagWordsDictionary,
].filter(isDefined);
return createCollectionP(dictionaries, 'dictionary collection');
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function loadDictionary(uri: string, options: DictionaryDefinitionInterna
return loadedEntry.dictionary;
}

const importantOptionKeys: (keyof DictionaryDefinitionInternal)[] = ['noSuggest', 'useCompounds'];
const importantOptionKeys: (keyof DictionaryDefinitionInternal)[] = ['name', 'noSuggest', 'useCompounds'];

function calcKey(uri: string, options: DictionaryDefinitionInternal) {
const loaderType = determineType(uri, options);
Expand Down

0 comments on commit 76c41ad

Please sign in to comment.