Cache all top-level calls to filterByTypes (~7% runtime reduction).

This commit is contained in:
David Anson 2024-08-24 22:05:16 -07:00
parent 85e704f32a
commit dfcb4529f3
47 changed files with 427 additions and 481 deletions

View file

@ -3,7 +3,8 @@
"use strict";
const { addError } = require("../helpers");
const { addRangeToSet, filterByTypes } = require("../helpers/micromark.cjs");
const { addRangeToSet } = require("../helpers/micromark.cjs");
const { filterByTypesCached } = require("./cache");
// eslint-disable-next-line jsdoc/valid-types
/** @type import("./markdownlint").Rule */
@ -17,17 +18,16 @@ module.exports = {
brSpaces = Number((brSpaces === undefined) ? 2 : brSpaces);
const listItemEmptyLines = !!params.config.list_item_empty_lines;
const strict = !!params.config.strict;
const { tokens } = params.parsers.micromark;
const codeBlockLineNumbers = new Set();
for (const codeBlock of filterByTypes(tokens, [ "codeFenced" ])) {
for (const codeBlock of filterByTypesCached([ "codeFenced" ])) {
addRangeToSet(codeBlockLineNumbers, codeBlock.startLine + 1, codeBlock.endLine - 1);
}
for (const codeBlock of filterByTypes(tokens, [ "codeIndented" ])) {
for (const codeBlock of filterByTypesCached([ "codeIndented" ])) {
addRangeToSet(codeBlockLineNumbers, codeBlock.startLine, codeBlock.endLine);
}
const listItemLineNumbers = new Set();
if (listItemEmptyLines) {
for (const listBlock of filterByTypes(tokens, [ "listOrdered", "listUnordered" ])) {
for (const listBlock of filterByTypesCached([ "listOrdered", "listUnordered" ])) {
addRangeToSet(listItemLineNumbers, listBlock.startLine, listBlock.endLine);
let trailingIndent = true;
for (let i = listBlock.children.length - 1; i >= 0; i--) {
@ -53,10 +53,10 @@ module.exports = {
const paragraphLineNumbers = new Set();
const codeInlineLineNumbers = new Set();
if (strict) {
for (const paragraph of filterByTypes(tokens, [ "paragraph" ])) {
for (const paragraph of filterByTypesCached([ "paragraph" ])) {
addRangeToSet(paragraphLineNumbers, paragraph.startLine, paragraph.endLine - 1);
}
for (const codeText of filterByTypes(tokens, [ "codeText" ])) {
for (const codeText of filterByTypesCached([ "codeText" ])) {
addRangeToSet(codeInlineLineNumbers, codeText.startLine, codeText.endLine - 1);
}
}