Increase recalculation performance by additional caching

This commit is contained in:
Tobias Hößl 2020-06-07 11:44:07 +02:00
parent 18bc495bd8
commit bc3b8be78d
No known key found for this signature in database
GPG Key ID: 1D780C7599C2D2A2

View File

@ -377,22 +377,27 @@ export class LinenumberingService {
* @returns {LineNumberRange} * @returns {LineNumberRange}
*/ */
public getLineNumberRange(html: string): LineNumberRange { public getLineNumberRange(html: string): LineNumberRange {
const fragment = this.htmlToFragment(html); const cacheKey = this.djb2hash(html);
const range = { let range = this.lineNumberCache.get(cacheKey);
from: null, if (!range) {
to: null const fragment = this.htmlToFragment(html);
}; range = {
const lineNumbers = fragment.querySelectorAll('.os-line-number'); from: null,
for (let i = 0; i < lineNumbers.length; i++) { to: null
const node = lineNumbers.item(i); };
const number = parseInt(node.getAttribute('data-line-number'), 10); const lineNumbers = fragment.querySelectorAll('.os-line-number');
if (range.from === null || number < range.from) { for (let i = 0; i < lineNumbers.length; i++) {
range.from = number; const node = lineNumbers.item(i);
} const number = parseInt(node.getAttribute('data-line-number'), 10);
if (range.to === null || number + 1 > range.to) { if (range.from === null || number < range.from) {
range.to = number + 1; range.from = number;
}
if (range.to === null || number + 1 > range.to) {
range.to = number + 1;
}
} }
} }
this.lineNumberCache.put(cacheKey, range);
return range; return range;
} }
@ -482,10 +487,17 @@ export class LinenumberingService {
* @return {string[]} * @return {string[]}
*/ */
public splitToParagraphs(html: string): string[] { public splitToParagraphs(html: string): string[] {
const fragment = this.htmlToFragment(html); const cacheKey = this.djb2hash(html);
return this.splitNodeToParagraphs(fragment).map((node: Element): string => { let cachedParagraphs = this.lineNumberCache.get(cacheKey);
return node.outerHTML; if (!cachedParagraphs) {
}); const fragment = this.htmlToFragment(html);
cachedParagraphs = this.splitNodeToParagraphs(fragment).map((node: Element): string => {
return node.outerHTML;
});
this.lineNumberCache.put(cacheKey, cachedParagraphs);
}
return cachedParagraphs;
} }
/** /**