Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Efficiency enhancement after node profiler investigation #2386

Closed
ajesshope opened this issue Sep 3, 2023 · 2 comments
Closed

Efficiency enhancement after node profiler investigation #2386

ajesshope opened this issue Sep 3, 2023 · 2 comments
Labels
enhancement Improved functionality

Comments

@ajesshope
Copy link

ajesshope commented Sep 3, 2023

Search Terms

getRelativeUrl, enhancement

Problem

Not a problem... an efficiency enhancement. We ran typedoc thru the nodeJs profiler to investigate why running typedoc against our system was taking so long.

Top 6 of the node profiler output:

   ticks  total  nonlib   name
  95136    4.5%   23.0%  JS: *renderElement ~/<pkgName>/node_modules/typedoc/dist/lib/utils/jsx.js:79:23
  55925    2.7%   13.5%  JS: *getRelativeUrl ~/<pkgName>/node_modules/typedoc/dist/lib/output/components.js:62:19
  43660    2.1%   10.6%  JS: *join node:path:1170:7
  36254    1.7%    8.8%  JS: *links ~/<pkgName>/node_modules/typedoc/dist/lib/output/themes/default/partials/navigation.js:93:19
  33670    1.6%    8.1%  JS: *renderChildren ~/<pkgName>/node_modules/typedoc/dist/lib/utils/jsx.js:130:28
  26826    1.3%    6.5%  RegExp: [\s\S]*?(?:[^_-][_-](?=[^_-])|[^A-Z](?=[A-Z][^A-Z]))

There were 2 significant areas for us:

  1. At 23% was renderElement in lib/utils/jsx.js. We know the large number of interfaces in one d.ts was the cause of this and have opened an enhancement for it. See open issue #2382
  2. At 13.5% was getRelativeUrl in lib/output/component.js. This method converts an absolute path to a relative path and is the target of this enhancement request below.

Suggested Solution

The enhancement we suggest is to use a Map to hold the results of previous conversions and return the held result if the key matches.

    /**
     * Map to hold previous absolute path to relative path conversions
     */
    protected absoluteToRelativePathMap = new Map<string, string>();

    /**
     * Transform the given absolute path into a relative path.
     *
     * @param absolute  The absolute path to transform.
     * @returns A path relative to the document currently processed.
     */
    public getRelativeUrl(absolute: string): string {
        if (this.urlPrefix.test(absolute)) {
            return absolute;
        } else {
            const key = `${this.location}:${absolute}`;
            let path = this.absoluteToRelativePathMap.get(key);
            if (path) return path;
            path = Path.posix.relative(this.location, absolute) || ".";
            this.absoluteToRelativePathMap.set(key, path);
            return path;
        }
    }

After we implemented this locally the getRelativeUrl went from 13.5%(2nd highest @55,925 ticks) of the total time to %0(99th highest @ 25 ticks).
For our project typedoc calls getRelativeUrl a total of 87,530,384 times (yup... that is 87.5 million)... 149,731 of those are unique... which means there are 87,380,653(99.83%) duplicate calls... we currently have 9,342 pages.
Even though a Map get/set are not noops... they use significantly less processing than Path.posix.relative... therefore we make huge savings (~13.5%) on processing with this small change.
Obviously with small projects this is probably not even noticeable. However, the larger the project the bigger the impact.

Latest profiling top 6 (using change above).

   ticks  total  nonlib   name
  91250    4.3%   32.3%  JS: *renderElement ~/<pkgName>/node_modules/typedoc/dist/lib/utils/jsx.js:79:45
  33172    1.6%   11.7%  JS: *renderChildren ~/<pkgName>/node_modules/typedoc/dist/lib/utils/jsx.js:139:28
  28007    1.3%    9.9%  JS: *links ~/<pkgName>/node_modules/typedoc/dist/lib/output/themes/default/partials/navigation.js:105:19
  27063    1.3%    9.6%  RegExp: [\s\S]*?(?:[^_-][_-](?=[^_-])|[^A-Z](?=[A-Z][^A-Z]))
  14535    0.7%    5.1%  JS: *DefaultThemeRenderContext.urlTo ~/<pkgName>/node_modules/typedoc/dist/lib/output/themes/default/DefaultThemeRenderContext.js:49:22
  10872    0.5%    3.8%  RegExp: [&<>'"]

@ajesshope ajesshope added the enhancement Improved functionality label Sep 3, 2023
@Gerrit0
Copy link
Collaborator

Gerrit0 commented Sep 3, 2023

PR welcome! Basically all of my test cases are tiny

@Gerrit0
Copy link
Collaborator

Gerrit0 commented Sep 4, 2023

27063 1.3% 9.6% RegExp: [\s\S]*?(?:[^_-]_-|^A-Z)

I just noticed this... wat, that makes me want to kill wbr. Not sure it provides any real value to begin with, it's a carryover from back when the themes were in handlebars.

I'm working (hopefully tomorrow) on having navigation load dynamically, which should drastically reduce the number of calls to getRelativeUrl

@Gerrit0 Gerrit0 closed this as completed in a6823cf Sep 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improved functionality
Projects
None yet
Development

No branches or pull requests

2 participants