Skip to content

Excessive sampling of linear gradients in PDF #7365

Closed

Description

@saecki

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
#show math.equation: set text(fill: gradient.linear(..color.map.rainbow))
#show math.equation: box

$ hat(x) = bar x bar = vec(x, y, z) = tilde(x) = dot(x) $
$ x prime = vec(1, 2, delim: "[") $
$ sum_(i in NN) 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.

Activity

  1. added
    pdf
    perf
    on Nov 12, 2025
  2. LaurenzV commented on Nov 12, 2025

    @LaurenzV
    Collaborator

    I would recommend using the color crate, 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-L450

    The only problem is that CMYK is not supported, but I believe all other color spaces should be supported.

  3. added
    regression
    urgent
    on Nov 20, 2025
  4. added a commit that references this issue on Feb 6, 2026
    c404595
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pdfperfregressionurgent

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions