don't precompile regexps in storage.js (no gain)

This commit is contained in:
tophf 2017-06-25 13:16:23 +03:00
parent 93a4f4704f
commit e3e1ecf2d3
2 changed files with 18 additions and 32 deletions

View File

@ -397,6 +397,21 @@ function handleDelete(id) {
Additionally we'll check for invalid regexps. Additionally we'll check for invalid regexps.
*/ */
function detectSloppyRegexps({entry, style}) { function detectSloppyRegexps({entry, style}) {
// make sure all regexps are compiled
const rxCache = BG.cachedStyles.regexps;
for (const section of style.sections) {
for (const regexp of section.regexps) {
for (let pass = 1; pass <= 2; pass++) {
const cacheKey = pass == 1 ? regexp : BG.SLOPPY_REGEXP_PREFIX + regexp;
if (!rxCache.has(cacheKey)) {
// according to CSS4 @document specification the entire URL must match
const anchored = pass == 1 ? '^(?:' + regexp + ')$' : '^' + regexp + '$';
const rx = tryRegExp(anchored);
rxCache.set(cacheKey, rx || false);
}
}
}
}
const { const {
appliedSections = appliedSections =
BG.getApplicableSections({style, matchUrl: tabURL}), BG.getApplicableSections({style, matchUrl: tabURL}),
@ -404,9 +419,8 @@ function detectSloppyRegexps({entry, style}) {
BG.getApplicableSections({style, matchUrl: tabURL, strictRegexp: false}), BG.getApplicableSections({style, matchUrl: tabURL, strictRegexp: false}),
} = style; } = style;
BG.compileStyleRegExps({style, compileAll: true});
entry.hasInvalidRegexps = wannabeSections.some(section => entry.hasInvalidRegexps = wannabeSections.some(section =>
section.regexps.some(rx => !BG.cachedStyles.regexps.has(rx))); section.regexps.some(rx => !rxCache.has(rx)));
entry.sectionsSkipped = wannabeSections.length - appliedSections.length; entry.sectionsSkipped = wannabeSections.length - appliedSections.length;
if (!appliedSections.length) { if (!appliedSections.length) {

View File

@ -4,7 +4,8 @@ const RX_NAMESPACE = new RegExp([/[\s\r\n]*/,
/(@namespace[\s\r\n]+(?:[^\s\r\n]+[\s\r\n]+)?url\(http:\/\/.*?\);)/, /(@namespace[\s\r\n]+(?:[^\s\r\n]+[\s\r\n]+)?url\(http:\/\/.*?\);)/,
/[\s\r\n]*/].map(rx => rx.source).join(''), 'g'); /[\s\r\n]*/].map(rx => rx.source).join(''), 'g');
const RX_CSS_COMMENTS = /\/\*[\s\S]*?\*\//g; const RX_CSS_COMMENTS = /\/\*[\s\S]*?\*\//g;
const SLOPPY_REGEXP_PREFIX = '\0'; // eslint-disable-next-line no-var
var SLOPPY_REGEXP_PREFIX = '\0';
// Note, only 'var'-declared variables are visible from another extension page // Note, only 'var'-declared variables are visible from another extension page
// eslint-disable-next-line no-var // eslint-disable-next-line no-var
@ -88,13 +89,8 @@ function getStyles(options) {
return dbExec('getAll').then(event => { return dbExec('getAll').then(event => {
cachedStyles.list = event.target.result || []; cachedStyles.list = event.target.result || [];
cachedStyles.byId.clear(); cachedStyles.byId.clear();
const t0 = performance.now();
let hasTimeToCompile = true;
for (const style of cachedStyles.list) { for (const style of cachedStyles.list) {
cachedStyles.byId.set(style.id, style); cachedStyles.byId.set(style.id, style);
if (hasTimeToCompile) {
hasTimeToCompile = !compileStyleRegExps({style}) || performance.now() - t0 > 100;
}
} }
cachedStyles.mutex.inProgress = false; cachedStyles.mutex.inProgress = false;
@ -296,7 +292,6 @@ function saveStyle(style) {
} }
style.id = style.id || event.target.result; style.id = style.id || event.target.result;
invalidateCache(existed ? {updated: style} : {added: style}); invalidateCache(existed ? {updated: style} : {added: style});
compileStyleRegExps({style});
if (notify) { if (notify) {
notifyAllTabs({ notifyAllTabs({
method: existed ? 'styleUpdated' : 'styleAdded', method: existed ? 'styleUpdated' : 'styleAdded',
@ -463,29 +458,6 @@ function styleSectionsEqual({sections: a}, {sections: b}) {
} }
function compileStyleRegExps({style, compileAll}) {
const t0 = performance.now();
for (const section of style.sections || []) {
for (const regexp of section.regexps) {
for (let pass = 1; pass <= (compileAll ? 2 : 1); pass++) {
const cacheKey = pass == 1 ? regexp : SLOPPY_REGEXP_PREFIX + regexp;
if (cachedStyles.regexps.has(cacheKey)) {
continue;
}
// according to CSS4 @document specification the entire URL must match
const anchored = pass == 1 ? '^(?:' + regexp + ')$' : '^' + regexp + '$';
const rx = tryRegExp(anchored);
cachedStyles.regexps.set(cacheKey, rx || false);
if (!compileAll && performance.now() - t0 > 100) {
return false;
}
}
}
}
return true;
}
function invalidateCache({added, updated, deletedId} = {}) { function invalidateCache({added, updated, deletedId} = {}) {
if (!cachedStyles.list) { if (!cachedStyles.list) {
return; return;