-
-
Notifications
You must be signed in to change notification settings - Fork 710
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
Performance issue on v53 with large font #1457
Comments
Hello, and thanks for the report. It’s true, WeasyPrint 53.x is slower when dealing with fonts. It’s because we use fonttools to remove unused characters from fonts, and it takes a lot of time compared to what Cairo did. A "workaround" is to use the Real fixes would be:
|
I see. I located the related codes. doc.write_pdf(
path.join(outpath, f"{code['school']}.pdf"),
image_cache=image_cache,
optimize_size=(),
) And then run
53.3 Running with optimize font'ABC School.pdf' 2.5M
53.3 Running without optimize font'ABC School.pdf' 34M
52.5'ABC School.pdf' 5.9M
Findings
|
Since I've quite a lot of tickets to generate PDF, I'm wondering if this works:
|
For sure, there’s a lot of work yet, but better performances and smaller PDFs are on the roadmap. 😉
Fonts with 52.5 are always "optimized" (actually re-created with needed glyphs by Cairo, not optimized by fontTools). So, the real regression is the speed issue, not the file size.
It would work, but … You have to add a |
(And don’t use WOFF or WOFF2 fonts, it would be even slower.) |
For what it's worth, I spent a little bit of time earlier this week profiling effectively this exact issue (py-spy + speedscope is excellent for this), and found that indeed the vast majority of the time was spent in FontTools' subsetting. There was a bit of low-hanging fruit to pick off in speeding it up, and I've submitted fonttools/fonttools#2467 for that. With those minor (+7 lines, -5 lines) changes, my test document render went down from 41 seconds to 6 seconds. A quick look showed that there were still optimizations that could be made in FontTools' subsetting, but they're not as major, and (at first glance) not as easy. It turns out that Harfbuzz also has a subsetter (hb-subset), which may well be faster, but it doesn't support variable weights if you wanted to try to close #1454 at the same time. 😞 |
Good to know. I’ve been using cProfile + gprof2dot for years, but py-spy looks quite appealing.
👏
How did I miss that? Thanks for the information, it would be better to avoid fontTools if possible (mainly because it comes with non-pure-Python dependencies, but also for speed).
Then we’ll have to create our own fonts for that. Cairo already does that using Pango, so that’s possible, but it’s not easy to find how. Harfbuzz provides an experimental API to retrieve outlines, but until it’s stable I suppose that it’s better to use FreeType instead. |
technically fontTools doesn't have any required dependencies, only optional extras. Looks like you require |
That’s right, and thanks a lot for maintaining these packages! Avoiding non-pure-Python is useful to avoid newcomers with compilation errors when new versions of Python are released, often because they don’t even have a compiler installed. We already have a large stack of non-Python dependencies (Pango, Harfbuzz, Fontconfig, Freetype…) + Pillow, we shouldn’t hesitate to remove some of them if it’s possible to have the same features with the other deps. |
We do try to rebuild wheels as soon as new python versions reach stable. Anyway, for some things like compression pure python would be too slow so there's now way around native dependencies. |
I’ve tried to use Harfbuzz’ subsetter instead of fontTools’ one, but I’m not sure that the results are worth the change. It’s pushed on the (It looks like |
Thanks! I'll try to get a chance to test that out soon. With an eye towards v55's performance goals, I'd also like to automatically profile some of our document renders to get an idea of where the hotspots are at least for our documents, but it's likely to be [at least] several weeks before I get there. |
👍
You could find WeasyPerf useful! |
f8110d6 has been written to save a couple of seconds for tests, but it gives incredible results with documents with a lot of big fonts. Don’t hesitate to try! |
Let’s say it’s fixed! |
- don't write out the HTML out by default - suppress the weird warning when writing the PDF - Kozea/WeasyPrint#1457 (comment) - we might get bigger PDFs
When I tried the fix on v53 for #1392, I found WeasyPrint spent much longer time on "Step 7 - Adding PDF metadata".
The issue is related to the font "Noto Sans CJK TC" I used so it is not reproducible on any system.
This maybe reproducible with a large font.
PC config:
Repo to reproduce this issue: https://github.com/leesei/WeasyPrint_1392 (Updated)
Run as is to produce the issue
Without "Noto Sans CJK TC"
On systems without the font, or commenting out ln 13 (
font-family: "Noto Sans CJK TC";
) instyles.css
WeasyPrint 52.5
The output PDF size is doubled though.
The text was updated successfully, but these errors were encountered: