mathjax / mathjax/MathJax-src

Font Packing Optimisations

Open
#400 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
TypeScript
Stars
2.4k
Forks
245
Avg merge
5d 2h
Merged PRs (30d)
2

Description

So I was optimizing my build of mathjax for an app, and spent some time looking at how fonts are packaged

Currently the metrics data, the SVG path data, and the unicode character are split into three files, and then combined into two fonts at runtime. However, this is really optimizing for the case where the user is exporting to both SVG and chtml in the same page

This splitting adds overhead: you need to duplicate a good amount of the structure of the font when it's split out into this way, you need to include runtime code, and you also need to run the runtime code

We could make two versions of each font, one for SVG and one for chtml

Another optimization we could do is to JSON.stringify these structures at build time and then JSON.parse the strings at runtime - which counter-intuitively speeds up parse time (Chrome Labs Benchmark). We an actually do this regardless of if we combine the font file structures. Webpack does this automatically for any .json file

So in my project, I target only SVG, and I stubbed a most of the font files out with null-loaderMy bundle size before these optimizations was 1.8MB. I did these two optimizations brought this down to 1.5MB - a 300kb reduction. This would have also been a bigger reduction if I had included all the fonts

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing how the font metrics, SVG path data, and Unicode character data are packaged and combined for SVG and CHTML. Compare the runtime and bundle paths described in the issue, including JSON serialization and parsing. Done means the packaging reduces overhead and bundle size without breaking either output mode.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript, webpack
Domain
build-system, performance
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.