You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Excessive sampling of linear gradients in PDF #7365
pdfRelated to PDF export or PDF embedding.perfRelated to performance and optimization.regressionSomething that did work before stopped working.urgentShould be fixed before the next release.
After rebasing my test-suite branch, I noticed some PDF tests getting noticable slower. I pinpointed it to this PR: #7326
This specific test now takes up twice the amount of time:
--- gradient-math-misc ---
// Test miscellaneous#showmath.equation: settext(fill: gradient.linear(..color.map.rainbow))
#showmath.equation: box$hat(x) = bar x bar = vec(x, y, z) = tilde(x) = dot(x) $$ x prime = vec(1, 2, delim: "[") $$sum_(i inNN) 1 + i $$attach(
Pi, t: alpha, b: beta,
tl: 1, tr: 2+3, bl: 4+5, br: 6,
) $
The gradient code in typst-pdf generates 31 samples between every stop of a gradient. Since the rainbow gradient already has a lot of stops, this seems to be the culprit. Maybe we can choose the number of samples based on the distance of the stops and or distance of the hue instead? In the rainbow case I think it would be fine to basically omit any extra sampled stops.
pdfRelated to PDF export or PDF embedding.perfRelated to performance and optimization.regressionSomething that did work before stopped working.urgentShould be fixed before the next release.
Activity
LaurenzV commentedon Nov 12, 2025
I would recommend using the
colorcrate, which allows you to determine the right number of stops in a more principled way. We use it in vello_cpu: https://github.com/linebender/vello/blob/42f32e46a1e847e9392271841f8679aac855b238/sparse_strips/vello_common/src/encode.rs#L321-L450The only problem is that CMYK is not supported, but I believe all other color spaces should be supported.
Better approximation for linear gradients in PDF