Skip to content

perf(layout): avoid quadratic shift() in resolveBookmarks - #3579

Open
exoego wants to merge 1 commit into
diegomura:masterfrom
exoego:perf/layout-resolve-bookmarks-index
Open

exoego wants to merge 1 commit into
diegomura:masterfrom
exoego:perf/layout-resolve-bookmarks-index

Conversation

@exoego

@exoego exoego commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

resolveBookmarks walks the whole document tree on every render to find bookmark props. It does this even when the document has no bookmarks.

The walk takes items from a single queue with listToExplore.shift(). The queue holds a whole level of the tree, so it gets large on big documents. Each shift() on a large array moves every remaining item, so the walk is quadratic in the number of elements.

This PR reads the queue by index instead. The visit order is the same breadth-first order as before. Bookmark refs and parent links do not change.

Measurements

Rendering rows of 9 cells with no bookmarks. Apple M4 Pro, Node 24.21.0, median of 3 runs.

Rows Render before Render after resolveBookmarks before resolveBookmarks after
2,000 1.36 s 1.26 s 97 ms 3 ms
4,000 2.79 s 2.35 s 425 ms 9 ms
8,000 6.62 s 4.90 s 1,721 ms 17 ms

At 8,000 rows the whole render is 26% faster. Before this change, doubling the rows made resolveBookmarks about 4 times slower. After it, the time doubles.

The step runs before pagination, so both pagination engines are affected. In our 270-page report with large tables, it took 4.5 s of a 26 s render.

How to reproduce

Render many rows with no bookmarks. Profile with node --cpu-prof and look at the self time of resolveBookmarks.

const doc = (rows) => (
  <Document>
    <Page size="A4" experimentalPagination>
      {Array.from({ length: rows }, (_, r) => (
        <View key={r} style={{ flexDirection: 'row' }}>
          {Array.from({ length: 9 }, (_, c) => (
            <View key={c} style={{ flex: 1 }}>
              <Text>{`${r}-${c}`}</Text>
            </View>
          ))}
        </View>
      ))}
    </Page>
  </Document>
);

await renderToBuffer(doc(8000));

Tests

No new tests. The existing "should resolve bookmark hierarchy" test already checks the breadth-first ref order and parent links. A timing test would be flaky in CI. All layout tests pass.

Notes

fetchAssets and buildXLinksIndex use the same shift() pattern. Their queues stay small, so they are not slow in practice. I left them out to keep this PR focused.

@changeset-bot

changeset-bot Bot commented Oct 6, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ab2035a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@react-pdf/layout Patch
@react-pdf/renderer Patch
@react-pdf/examples Patch
@react-pdf/math Patch
@react-pdf/mermaid Patch
@react-pdf/ui Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel

vercel Bot commented Oct 6, 2026

Copy link
Copy Markdown

@exoego is attempting to deploy a commit to the diegomura Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant