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

Perf: reuse Shiki highlighters per theme/lang #3130

Merged
merged 2 commits into from
Apr 18, 2022
Merged

Conversation

tony-sull
Copy link
Contributor

@tony-sull tony-sull commented Apr 18, 2022

Changes

Fixes #3110

This updates the built-in <Code> component to reuse Shiki highlighters based on the theme and lang properties

Testing

Tested locally against the astro-imagetools docs site

Before

Error: fail to memory allocation

Screen Shot 2022-04-18 at 10 49 22 AM

After
Screen Shot 2022-04-18 at 10 46 48 AM

Docs

N/A

@changeset-bot
Copy link

changeset-bot bot commented Apr 18, 2022

🦋 Changeset detected

Latest commit: 0f023d0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
astro Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Apr 18, 2022
@tony-sull tony-sull marked this pull request as ready for review April 18, 2022 08:54
// Highlighter has already been requested, reuse the same instance
if (_resolvedHighlighters.has(key)) { return _resolvedHighlighters.get(key) }

// Start the async getHighlighter call and cache the Promise
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a bit more poking around and confirmed that this looks like the memory issue was just due to pages being built in parallel. Each page initialized a highlighter and Node couldn't garbage collect them in between page builds

Caching the promise here makes sure that each page gets an immediate response and they can all share the same highlighter instance

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah nice, we had to do this in the remark plugin also, wish we could reuse more logic between teh two!

Copy link
Contributor

@matthewp matthewp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@tony-sull tony-sull merged commit 394ab90 into main Apr 18, 2022
@tony-sull tony-sull deleted the fix/code-shiki-perf branch April 18, 2022 16:30
@github-actions github-actions bot mentioned this pull request Apr 18, 2022
SiriousHunter pushed a commit to SiriousHunter/astro that referenced this pull request Feb 3, 2023
* reuse Shiki highlighters per theme/lang

* chore: adding changeset
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 BUG: Memory usage racking up for each page
3 participants