Skip to content

Commit e2ed450

Browse files
authored
Speedup pronunciations retrieval for heavy workflows (#2438)
* return pronunciation with tokenize * fix cache conflicts
1 parent d9c3c4d commit e2ed450

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

ext/js/background/backend.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,7 +1722,8 @@ export class Backend {
17221722
const codePoint = /** @type {number} */ (text.codePointAt(i));
17231723
const character = String.fromCodePoint(codePoint);
17241724
const substring = text.substring(i, i + scanLength);
1725-
const cacheKey = `${optionsContext.index}:${substring}`;
1725+
const metadataMode = useAllFrequencyDictionaries === true ? 1 : 0;
1726+
const cacheKey = `${optionsContext.index}:${metadataMode}:${substring}`;
17261727
let cached = this._textParseCache.get(cacheKey);
17271728
if (typeof cached === 'undefined') {
17281729
const {dictionaryEntries, originalTextLength} = await this._translator.findTerms(
@@ -1754,7 +1755,15 @@ export class Backend {
17541755
if (src.matchType !== 'exact') { continue; }
17551756
validSources.push(src);
17561757
}
1757-
if (validSources.length > 0) { validHeadwords.push({term: headword.term, reading: headword.reading, sources: validSources, frequencies: dictionaryEntry.frequencies.filter((f) => f.headwordIndex === headword.headwordIndex)}); }
1758+
if (validSources.length > 0) {
1759+
validHeadwords.push({
1760+
term: headword.term,
1761+
reading: headword.reading,
1762+
sources: validSources,
1763+
frequencies: dictionaryEntry.frequencies.filter((f) => f.headwordIndex === headword.headwordIndex),
1764+
pronunciations: dictionaryEntry.pronunciations.filter((p) => p.headwordIndex === headword.headwordIndex),
1765+
});
1766+
}
17581767
}
17591768
if (validHeadwords.length > 0) { trimmedHeadwords.push(validHeadwords); }
17601769
}

types/ext/api.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export type ParseTextSegment = {
6969
reading: string;
7070
sources: Dictionary.TermSource[];
7171
frequencies: Dictionary.TermFrequency[];
72+
pronunciations: Dictionary.TermPronunciation[];
7273
}[][];
7374
};
7475

0 commit comments

Comments
 (0)