Skip to content

Commit

Permalink
Refactor highlighter to improve performance
Browse files Browse the repository at this point in the history
Previously, Shiki was taking 600ms to initialize on my box, this change makes it take 400ms instead.
  • Loading branch information
Gerrit0 committed Sep 18, 2021
1 parent b00e88e commit 9494412
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
5 changes: 5 additions & 0 deletions src/lib/output/renderer.ts
Expand Up @@ -195,7 +195,11 @@ export class Renderer extends ChildableComponent<
project: ProjectReflection,
outputDirectory: string
): Promise<void> {
const start = Date.now();
await loadHighlighter(this.lightTheme, this.darkTheme);
this.application.logger.verbose(
`Renderer: Loading highlighter took ${Date.now() - start}ms`
);
if (
!this.prepareTheme() ||
!(await this.prepareOutputDirectory(outputDirectory))
Expand All @@ -211,6 +215,7 @@ export class Renderer extends ChildableComponent<
output.urls = this.theme!.getUrls(project);

this.trigger(output);

if (!output.isDefaultPrevented) {
output.urls.forEach((mapping: UrlMapping) => {
this.renderDocument(output.createPageEvent(mapping));
Expand Down
17 changes: 7 additions & 10 deletions src/lib/utils/highlighter.tsx
Expand Up @@ -15,11 +15,11 @@ const supportedLanguages = unique(["text", ...aliases.keys(), ...BUNDLED_LANGUAG
class DoubleHighlighter {
private schemes = new Map<string, string>();

constructor(private light: Highlighter, private dark: Highlighter) {}
constructor(private highlighter: Highlighter, private light: Theme, private dark: Theme) {}

highlight(code: string, lang: string) {
const lightTokens = this.light.codeToThemedTokens(code, lang, void 0, { includeExplanation: false });
const darkTokens = this.dark.codeToThemedTokens(code, lang, void 0, { includeExplanation: false });
const lightTokens = this.highlighter.codeToThemedTokens(code, lang, this.light, { includeExplanation: false });
const darkTokens = this.highlighter.codeToThemedTokens(code, lang, this.dark, { includeExplanation: false });

// If this fails... something went *very* wrong.
assert(lightTokens.length === darkTokens.length);
Expand Down Expand Up @@ -88,8 +88,8 @@ class DoubleHighlighter {
i++;
}

style.push(` --light-code-background: ${this.light.getTheme().bg};`);
style.push(` --dark-code-background: ${this.dark.getTheme().bg};`);
style.push(` --light-code-background: ${this.highlighter.getTheme(this.light).bg};`);
style.push(` --dark-code-background: ${this.highlighter.getTheme(this.dark).bg};`);
lightRules.push(` --code-background: var(--light-code-background);`);
darkRules.push(` --code-background: var(--dark-code-background);`);

Expand Down Expand Up @@ -134,11 +134,8 @@ let highlighter: DoubleHighlighter | undefined;

export async function loadHighlighter(lightTheme: Theme, darkTheme: Theme) {
if (highlighter) return;
const [lightHl, darkHl] = await Promise.all([
getHighlighter({ theme: lightTheme }),
getHighlighter({ theme: darkTheme }),
]);
highlighter = new DoubleHighlighter(lightHl, darkHl);
const hl = await getHighlighter({ themes: [lightTheme, darkTheme] });
highlighter = new DoubleHighlighter(hl, lightTheme, darkTheme);
}

export function isSupportedLanguage(lang: string) {
Expand Down

0 comments on commit 9494412

Please sign in to comment.