refactor: memoize type-related functions

I did some quick-and-dirty profiling, and these functions made up ~2% of the flamegraph, mostly for when they called through to the type parser; also, it's likely that they'll be called repeatedly with many of the same inputs. Memoizing the functions cuts them to ~1% of the flamegraph.
This commit is contained in:
Jeff Williams 2023-12-10 17:37:36 -08:00
parent 45c497e192
commit 2a5ae15ed3
No known key found for this signature in database
3 changed files with 32 additions and 8 deletions

8
package-lock.json generated
View File

@ -7698,6 +7698,11 @@
"resolved": "https://registry.npmjs.org/mdurl/-/mdurl-2.0.0.tgz",
"integrity": "sha512-Lf+9+2r+Tdp5wXDXC4PcIBjTDtq4UKjCPMQhKIuzpJNW0b96kVqSwW0bT7FhRSfmAiFYgP+SCRvdrDozfh0U5w=="
},
"node_modules/memize": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/memize/-/memize-2.1.0.tgz",
"integrity": "sha512-yywVJy8ctVlN5lNPxsep5urnZ6TTclwPEyigM9M3Bi8vseJBOfqNrGWN/r8NzuIt3PovM323W04blJfGQfQSVg=="
},
"node_modules/meow": {
"version": "8.1.2",
"resolved": "https://registry.npmjs.org/meow/-/meow-8.1.2.tgz",
@ -12370,7 +12375,8 @@
"@jsdoc/util": "^0.3.2",
"catharsis": "^0.9.0",
"common-path-prefix": "^3.0.0",
"lodash": "^4.17.21"
"lodash": "^4.17.21",
"memize": "^2.1.0"
},
"engines": {
"node": ">=v18.12.0"

View File

@ -13,6 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* @module @jsdoc/tag/lib/type
* @alias @jsdoc/tag.type
@ -20,11 +21,14 @@
import { name } from '@jsdoc/core';
import { cast } from '@jsdoc/util';
import catharsis from 'catharsis';
import memize from 'memize';
import { extractInlineTag } from './inline.js';
const MEMIZE_OPTS = { maxSize: 500 };
const NAME_AND_DEFAULT_VALUE_REGEXP = /^(.+?)\s*=\s*(.+)$/;
const NAME_AND_TYPE_REGEXP = /^(\[)?\s*(.+?)\s*(\])?$/;
const TYPES = catharsis.Types;
/**
* Information about a type expression extracted from tag text.
@ -35,6 +39,11 @@ const NAME_AND_TYPE_REGEXP = /^(\[)?\s*(.+?)\s*(\])?$/;
* @property {string} text - The updated tag text.
*/
/** @private */
function memoize(fn) {
return memize(fn, MEMIZE_OPTS);
}
/** @private */
function unescapeBraces(text) {
return text.replace(/\\\{/g, '{').replace(/\\\}/g, '}');
@ -47,7 +56,7 @@ function unescapeBraces(text) {
* @param {string} string - The tag text.
* @return {module:@jsdoc/tag.type.TypeExpressionInfo} The type expression and updated tag text.
*/
function extractTypeExpression(string) {
function _extractTypeExpression(string) {
let completeExpression;
let count = 0;
let position = 0;
@ -94,8 +103,10 @@ function extractTypeExpression(string) {
};
}
const extractTypeExpression = memoize(_extractTypeExpression);
/** @private */
function getTagInfo(tagValue, canHaveName, canHaveType) {
function _getTagInfo(tagValue, canHaveName, canHaveType) {
let tagName = '';
let typeExpression = '';
let tagText = tagValue;
@ -131,6 +142,8 @@ function getTagInfo(tagValue, canHaveName, canHaveType) {
};
}
const getTagInfo = memoize(_getTagInfo);
/**
* Information provided in a JSDoc tag.
*
@ -182,15 +195,15 @@ function parseName(tagInfo) {
return tagInfo;
}
let getTypeStrings;
/** @private */
function getTypeStrings(parsedType, isOutermostType) {
function _getTypeStrings(parsedType, isOutermostType) {
let applications;
let typeString;
let types = [];
const TYPES = catharsis.Types;
switch (parsedType.type) {
case TYPES.AllLiteral:
types.push('*');
@ -239,6 +252,8 @@ function getTypeStrings(parsedType, isOutermostType) {
return types;
}
getTypeStrings = memoize(_getTypeStrings);
/**
* Extract JSDoc-style and Closure Compiler-style type information from the type expression
* specified in the tag info.
@ -297,7 +312,7 @@ const typeParsers = [parseName, parseTypeExpression];
* @return {module:@jsdoc/tag.type.TagInfo} Information obtained from the tag.
* @throws {Error} Thrown if a type expression cannot be parsed.
*/
export function parse(tagValue, canHaveName, canHaveType) {
function _parse(tagValue, canHaveName, canHaveType) {
let tagInfo;
if (typeof tagValue !== 'string') {
@ -318,3 +333,5 @@ export function parse(tagValue, canHaveName, canHaveType) {
return tagInfo;
}
export const parse = memoize(_parse);

View File

@ -39,7 +39,8 @@
"@jsdoc/util": "^0.3.2",
"catharsis": "^0.9.0",
"common-path-prefix": "^3.0.0",
"lodash": "^4.17.21"
"lodash": "^4.17.21",
"memize": "^2.1.0"
},
"engines": {
"node": ">=v18.12.0"