Skip to content

perf: highlight with fewer grammars by default - #1235

Open
ovflowd wants to merge 5 commits into
mainfrom
perf/fewer-grammars
Open

ovflowd wants to merge 5 commits into
mainfrom
perf/fewer-grammars

Conversation

@ovflowd

@ovflowd ovflowd commented Nov 3, 2026 •

Copy link
Copy Markdown
Member

Description

This PR highlights code with fewer grammars by default, as each one takes its own memory on every thread that highlights code. JavaScript (js, mjs, cjs, javascript) now uses TypeScript's grammar, and C++ (cpp, c++) uses C's. In the light theme JavaScript looks the same, and Nord loses a few colors only JavaScript's grammar gives. langAlias takes precedence, so { cpp: 'cpp' } brings C++'s own grammar back.

doc-kit now resolves aliases itself, since Shiki replaced configured ones with a grammar's own as it registered it, so aliases also follow other aliases and match in any case.

Highlighting the Node.js docs' 2,866 JavaScript, TypeScript, C and C++ blocks in one process, JavaScript's own grammar takes +20MB RSS and C++'s +98MB, +119MB for both as on main (5 runs each, within ±2MB).

Full Node.js docs, medians of 7 interleaved runs (5 with one thread):

Build Threads main This PR
site 1 22.47s, 1951MB 21.48s (-4%), 1813MB (-7%)
site 4 7.60s, 2171MB 7.42s (-2%), 2007MB (-8%)
site 6 6.54s, 2830MB 6.28s (-4%), 2566MB (-9%)
legacy 1 9.27s, 1210MB 8.87s (-4%), 1050MB (-13%)
legacy 4 4.14s, 1529MB 3.93s (-5%), 1501MB (-2%)
legacy 6 3.91s, 1967MB 3.84s (-2%), 1868MB (-5%)

Peaks vary by ~100MB between runs, so the -2% rows are within that. On top of #1243, the site build is 3-4% faster with 8-10% less peak memory.

Validation

Added tests for highlighting JavaScript as TypeScript and C++ as C, configured aliases taking precedence, following aliases in any case, and an alias leading to a language of langs.

Related Issues

Split out of #1205.

Check List

  • I have read the Contributing Guidelines and made commit messages that follow the guideline.
  • I have run node --run test and all tests passed.
  • I have check code formatting with node --run format:check & node --run lint.
  • I've covered new added functionality with unit tests if necessary.

@vercel

vercel Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
api-docs-tooling Ready Ready Preview Nov 28, 2026 10:16pm UTC

Request Review

@codecov

codecov Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.04%. Comparing base (3d1c428) to head (d6a7712).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1235      +/-   ##
==========================================
+ Coverage   93.99%   94.04%   +0.04%     
==========================================
  Files         301      302       +1     
  Lines       31520    31648     +128     
  Branches     3216     3233      +17     
==========================================
+ Hits        29627    29763     +136     
+ Misses       1871     1863       -8     
  Partials       22       22              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ovflowd ovflowd mentioned this pull request Nov 3, 2026
4 tasks done
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

🚀 Deploying Preview to Cloudflare 🚀

Preview Deployments by commit

Status Deployment URL Commit Updated (UTC) See this deployment's details
  • Build: Success ✅

View logs ↗
No Preview URL. Enable ↗ d6a7712 2026-11-28T22:16:47.783Z View logs ↗
  • Build: Success ✅

View logs ↗
No Preview URL. Enable ↗ 69ad107 2026-11-28T22:14:04.386Z View logs ↗
  • Build: Success ✅

View logs ↗
No Preview URL. Enable ↗ 5c2b0e2 2026-11-28T22:00:40.978Z View logs ↗
  • Build: Success ✅

View logs ↗
No Preview URL. Enable ↗ b570722 2026-11-11T00:08:49.230Z View logs ↗
  • Build: Success ✅

View logs ↗
No Preview URL. Enable ↗ 15a06e7 2026-11-06T19:44:14.119Z View logs ↗
  • Build: In progress 🔵

View logs ↗
891436d 2026-11-06T19:30:49.437Z View logs ↗
  • Build: Success ✅

View logs ↗
No Preview URL. Enable ↗ 269a455 2026-11-06T13:35:43.503Z View logs ↗
  • Build: Success ✅

View logs ↗
No Preview URL. Enable ↗ ad1a970 2026-11-04T18:24:22.829Z View logs ↗
  • Build: Success ✅

View logs ↗
No Preview URL. Enable ↗ 2b215f2 2026-11-03T19:34:36.267Z View logs ↗

@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

api-links Generator

Performance estimate (single CI run)

  • Generation time: 40.0% faster (1.00 s → 600.00 ms)
  • Peak memory: 6.2% higher (268.43 MB → 285.04 MB)

json Generator

Performance estimate (single CI run)

  • Generation time: 38.6% faster (9.22 s → 5.66 s)
  • Peak memory: 1.2% higher (876.40 MB → 887.27 MB)

legacy-html Generator

Output size: 10 files changed · net -52.68 KB

File size details
File Main PR Change
addons.html 374.52 KB 329.16 KB -45.36 KB (-12.1%)
embedding.html 64.38 KB 57.78 KB -6.60 KB (-10.3%)
async_context.html 247.17 KB 245.10 KB -2.08 KB (-0.8%)
tls.html 301.26 KB 302.80 KB +1.54 KB (+0.5%)
n-api.html 822.59 KB 822.23 KB -364.00 B (-0.0%)
readline.html 219.10 KB 219.27 KB +168.00 B (+0.1%)
worker_threads.html 414.90 KB 414.73 KB -168.00 B (-0.0%)
module.html 226.11 KB 226.22 KB +112.00 B (+0.0%)
repl.html 226.64 KB 226.75 KB +112.00 B (+0.0%)
test.html 1011.46 KB 1011.40 KB -56.00 B (-0.0%)

Performance estimate (single CI run)

  • Generation time: 5.4% slower (14.93 s → 15.74 s)
  • Peak memory: 11.2% lower (1.36 GB → 1.21 GB)

legacy-json Generator

Performance estimate (single CI run)

  • Generation time: 34.1% slower (6.45 s → 8.65 s)
  • Peak memory: 2.1% higher (892.93 MB → 911.27 MB)

llms-txt Generator

Performance estimate (single CI run)

  • Generation time: 35.0% slower (5.11 s → 6.90 s)
  • Peak memory: 5.8% lower (952.15 MB → 896.66 MB)

orama-db Generator

Performance estimate (single CI run)

  • Generation time: 33.9% faster (8.16 s → 5.39 s)
  • Peak memory: 0.8% lower (1.02 GB → 1.02 GB)

web Generator

Output size: 45 files changed · net -54.17 KB

File size details
File Main PR Change
addons.html 390.53 KB 362.73 KB -27.80 KB (-7.1%)
embedding.html 73.32 KB 68.68 KB -4.63 KB (-6.3%)
process.html 1.20 MB 1.20 MB -4.16 KB (-0.3%)
child_process.html 707.46 KB 705.21 KB -2.24 KB (-0.3%)
tls.html 559.10 KB 561.28 KB +2.18 KB (+0.4%)
fs.html 2.36 MB 2.36 MB -1.32 KB (-0.1%)
crypto.html 2.11 MB 2.11 MB -1.21 KB (-0.1%)
http.html 1.25 MB 1.25 MB -1.11 KB (-0.1%)
assert.html 673.68 KB 672.57 KB -1.11 KB (-0.2%)
util.html 1.22 MB 1.22 MB -964.00 B (-0.1%)
async_hooks.html 293.79 KB 292.92 KB -898.00 B (-0.3%)
http2.html 1.36 MB 1.36 MB -788.00 B (-0.1%)
readline.html 381.81 KB 381.04 KB -788.00 B (-0.2%)
buffer.html 1.82 MB 1.82 MB -736.00 B (-0.0%)
dgram.html 314.37 KB 313.65 KB -728.00 B (-0.2%)
vm.html 657.72 KB 657.06 KB -680.00 B (-0.1%)
webstreams.html 566.06 KB 565.40 KB -674.00 B (-0.1%)
perf_hooks.html 913.52 KB 912.92 KB -620.00 B (-0.1%)
cluster.html 318.50 KB 317.90 KB -616.00 B (-0.2%)
worker_threads.html 657.04 KB 656.47 KB -588.00 B (-0.1%)
zlib.html 1.02 MB 1.02 MB -508.00 B (-0.0%)
ffi.html 217.71 KB 217.22 KB -504.00 B (-0.2%)
test.html 1.48 MB 1.48 MB -458.00 B (-0.0%)
globals.html 347.43 KB 347.04 KB -394.00 B (-0.1%)
vfs.html 214.50 KB 214.17 KB -340.00 B (-0.2%)
url.html 536.62 KB 536.29 KB -338.00 B (-0.1%)
stream_iter.html 798.56 KB 798.28 KB -296.00 B (-0.0%)
v8.html 601.27 KB 600.99 KB -294.00 B (-0.0%)
async_context.html 330.58 KB 330.35 KB -234.00 B (-0.1%)
n-api.html 1.06 MB 1.06 MB -230.00 B (-0.0%)
dns.html 443.11 KB 442.89 KB -224.00 B (-0.0%)
sqlite.html 559.94 KB 559.77 KB -174.00 B (-0.0%)
domain.html 131.70 KB 131.53 KB -168.00 B (-0.1%)
quic.html 995.95 KB 995.78 KB -168.00 B (-0.0%)
stream.html 1.28 MB 1.28 MB -122.00 B (-0.0%)
repl.html 314.72 KB 314.60 KB -116.00 B (-0.0%)
modules.html 239.11 KB 239.00 KB -112.00 B (-0.0%)
tty.html 114.29 KB 114.18 KB -112.00 B (-0.1%)
bench.html 243.97 KB 243.91 KB -60.00 B (-0.0%)
cli.html 691.78 KB 691.72 KB -56.00 B (-0.0%)
permissions.html 96.40 KB 96.35 KB -56.00 B (-0.1%)
module.html 360.84 KB 360.83 KB -14.00 B (-0.0%)
esm.html 198.27 KB 198.26 KB -8.00 B (-0.0%)
single-executable-applications.html 188.49 KB 188.48 KB -2.00 B (-0.0%)
wasi.html 84.34 KB 84.34 KB -2.00 B (-0.0%)

Performance estimate (single CI run)

  • Generation time: 23.7% slower (16.66 s → 20.61 s)
  • Peak memory: 15.2% lower (1.70 GB → 1.44 GB)

@ovflowd
ovflowd marked this pull request as ready for review November 3, 2026 20:39
@ovflowd
ovflowd requested a review from a team as a code owner November 3, 2026 20:39
kgeminicdev

This comment was marked as low quality.

@ovflowd

ovflowd commented Nov 4, 2026 •

Copy link
Copy Markdown
Member Author

Ack, the alias keys are lowercased now, with a test

@ovflowd

ovflowd commented Nov 28, 2026

Copy link
Copy Markdown
Member Author

cc @avivkeller could you review?

@avivkeller

Copy link
Copy Markdown
Member
Screenshot 2026-11-28 at 2 35 50 PM

Can you explain this? I feel this implies that the perf improvements might just be noise

@ovflowd

ovflowd commented Nov 28, 2026 •

Copy link
Copy Markdown
Member Author

Can you explain this? I feel this implies that the perf improvements might just be noise

So peak memory can be noisy, yes... This wasn't the ideal measurement to show on the PR 😅

But loading few grammars (aliasing supersets of them) reduces the amount of memory Shiki is loading. Some of these grammars use quite a good chunk of memory. Let me update the PR description with some more runs. Let me actually run this PR on top of my performance stack that should be less noisy and also give you numbers on how much each one of these grammars use in terms of memory.

@ovflowd
ovflowd force-pushed the perf/fewer-grammars branch from b570722 to 5c2b0e2 Compare November 28, 2026 21:59
@ovflowd

ovflowd commented Nov 28, 2026

Copy link
Copy Markdown
Member Author

@avivkeller updated the description: 7 runs each now and every row's lower, on top of the stack too. C++'s grammar alone is ~98MB per thread, JS ~20MB :)

Each grammar costs every thread its own memory, about 60MB for
JavaScript's on Oniguruma, and time to register. Types and signatures
are already highlighted as TypeScript, whose grammar highlights
JavaScript too, so `js`, `mjs`, `cjs` and `javascript` code is now
highlighted with it by default, and C++ (`cpp`, `c++`) with C's. On
Node.js's docs that saves ~190MB of peak memory and 2-7% of build time.

In the light theme, the default, JavaScript comes out the same but for
one block of console output marked as `js`. Nord, the dark theme, has
a few colors only for JavaScript's scopes. C++-only keywords lose their
color.

`langAlias` takes precedence, and doc-kit now resolves aliases itself:
Shiki replaced configured aliases with a grammar's own as it
registered. Aliases now also lead on through other aliases and are
found in any case. Transformers, and the language `resolveLanguage`
returns, are the one code is highlighted as.

Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
Assisted-by: Claude Sonnet 5.5 <noreply@anthropic.com>
The lookup lowercased the language, but the aliases kept their keys as
written, so `{ MyConf: 'ini' }` never matched. They're keyed in
lowercase now. The test also says why Rust stands in for a bundled
language registered through its own alias.

Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
Following aliases always ended, since each one is followed once, but
the set of those followed kept the names as written while the lookup
lowercased them, so it took a second look to see. Both use the
lowercased name now, and the test adds a cycle that differs only in
case.

Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
The cycle differing only in case checked nothing the other cycle
doesn't: following aliases ended before tracking them by their
lowercased name too, which only made it easier to see. A capitalised
key is checked once, the conf alias is highlighted as ini in the test
of configured languages already, and the test of configured aliases
over the default ones keeps C++, as the test of a language of langs
checks JavaScript's. The comment on Rust no longer says "now".

Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>
From the review. resolveLanguage now reads the language its aliases lead
to from followAliases, which follows each configured alias once, and
takes the default alias when none applies.

Assisted-by: Claude Opus 5.5 <noreply@anthropic.com>

This branch was successfully deployed

1 active deployment
Preview – api-docs-tooling — d6a7712f Deployed Nov 28, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants