Repository navigation
Optimize calculation of font coverage maps - #8676
Conversation
f59a745 to
17cfb98
Compare
isuffix
left a comment
There was a problem hiding this comment.
Overall this is quite nice! The implementation seems correct, and I can confirm the performance benefit when parsing for my local fonts 0.20s -> 0.10s, google-fonts 0.23s -> 0.16s and nerd-fonts 0.52s -> 0.21s.
I did add quite a few simplification and code style comments below that you should address. Feel free to let me know if you disagree with any of the smaller changes, our goal is just to ensure the code is idiomatic and maintainable :)
Stats
I also found some statistics to try to understand the performance effect myself. For the 17 embedded font faces (from the 4 embedded fonts), we have:
- Unique codepoints per face:
average 3240.8,min 1696(Libertinus),max 4989(NCMM) - The initial coverage vector length after 1 subtable:
average 308.5,min 148,max 508- Note that the number of codepoint ranges is the length divided by 2
- The coverage vector length increases exactly once after the initial subtable for each face:
- NCMM (3 font faces) increases the length by 124, 126, and 126
- The remaining 14 font faces increase the length by either 2, 4, or 6
- Total subtables were between 4 and 6, always with at least two pairs of duplicated mappings
So the coverage vector ends up usually being around 10x smaller than the total number of codepoints, and repeating the coverage vector building/merging for 3-5 subtables is cheaper than getting all of the codepoints first and sorting them before merging.
Iterating by Ranges
This isn't something to change with this PR, but we should really be iterating over codepoint ranges instead of iterating individual codepoints.
Most cmap subtable formats store ranges already. Right now we're effectively just asking ttf-parser to expand those ranges into codepoints before we then manually contract them. And a malicious font with a format 13 subtable could easily include every valid and invalid(!) codepoint to pessimize the coverage builder. I expect this is the likely cause of the 50s load time in #8561, since I don't get anywhere close to that even with all of google-fonts and nerd-fonts (~4000 ttf files, 0.99s -> 0.47s).
ttf-parser doesn't expose the raw codepoint ranges itself, and given that ttf-parser is now in maintenance mode, that's unlikely to change. It does look like fontations exposes cmap subtable internals more directly. So iterating over ranges is likely blocked on #8172, which is going to need a bit of work to properly revive.
17cfb98 to
a429d33
Compare
|
@isuffix Many thanks for the very educational review. I've implemented most of your suggestions. |
isuffix
left a comment
There was a problem hiding this comment.
Only some smaller style notes now. Otherwise, I'm happy with the PR :)
|
@isuffix Hi, thanks again for the review. To be quite honest, the interesting part of this PR is now over, I don't have much vested interest in this being merged or not, and I've already blown through the amount of my limited FOSS time that I was willing to spend on it. I don't want to spend another back-and-forth cycle. The formatting was all done by Feel free to make any changes you'd like to this patch yourself, or drop it. I'm OK either way. Thanks! |
|
That's alright! Thanks for responding as much as you have. I've pushed a commit with my recently reviewed changes and I'll take on getting this merged. I really appreciate you letting us know that we can move this forward. That's sometimes hard to accept/admit, so thank you. Also note that some of the formatting problems are real limitations of |
This is helpful because we almost always have a useful size hint when merging coverage sets.
|
Thank you! |
A large chunk of the cold start time of the
typstCLI is the initialization of the font book by scanning system fonts. Profiling has shown that the run time of this phase is entirely dominated by calculating the font coverage maps, that indicate to the compiler which fonts have a glyph mapped for a specific Unicode code point.Currently this is done by collecting the individual codepoints covered by every scanned font face into a large vector, and then compressing it into a compact range-based representation. To do so, the vector is de-duplicated and sorted, which is required because it is assembled by reading each CMAP sub-table in turn, and very often more than one sub-table can include a glyph for the same codepoint. These vectors include tens, or even hundreds, of thousands of entries; both the incremental allocation of the vector (causing multiple re-allocations and copies) and the sorting have large overhead.
We can avoid that through the observation that the codepoints in each sub-table already come out of
ttf-parserunique and sorted (though see caveat). With this we can directly collect the codepoints covered by each sub-table into the compact representation without needing to keep a vector of all codepoints in memory, and form the final coverage map by taking the union of all the sub-table coverage maps. The union is calculated with a typical linear sorted array merge algorithm.Benchmarks
Measurements taken with
hyperfine(excluding warmup) with thedev-fastbuild configuration on a circa-2019 Intel-based Linux laptop with a relatively typical selection of 289 font families installed.Comparing time to run
typst fonts:Comparing the time to cold compile a small "Hello World" document to PDF:
We can see that on average this PR shaves ~140ms from the CLI run time, and is likely to be far more significant on pathological cases such as #8561.
Caveat
As noted above, this relies on the observation that "the codepoints in each sub-table already come out of
ttf-parserunique and sorted". This is actually not a structural property of all CMAP sub-table formats, e.g. formats 4 and 12 could be stored in an unsorted order or have duplicates. However this appears to be at least an implicit requirement of the TrueType/OpenType specifications, as the CMAP formats are expected to be laid out in a way that allows font engines to binary search for the corresponding glyph for a codepoint. It is possible that there are broken fonts out there that don't maintain this expectation, but I didn't find any among the fonts available to me.