Skip to content

perf: Avoid multi-second getDestinations stalls for PDFs with many named destinations#21213

Open
saripovdenis wants to merge 1 commit intomozilla:masterfrom
saripovdenis:perf-name-tree-getall-queue-index
Open

perf: Avoid multi-second getDestinations stalls for PDFs with many named destinations#21213
saripovdenis wants to merge 1 commit intomozilla:masterfrom
saripovdenis:perf-name-tree-getall-queue-index

Conversation

@saripovdenis
Copy link
Copy Markdown

@saripovdenis saripovdenis commented May 3, 2026

Replace the queue.shift() traversal in NameOrNumberTree.getAll() with an index-based queue loop. This keeps traversal order and duplicate-reference handling, but avoids copying the remaining queue for every node.

This matters for callers that materialize full name/number trees: getDestinations(), embedded-file name trees, JavaScript name trees, XFA image name trees, and tagged-PDF parent/id trees.

End-user impact:

For small PDFs, no visible change. With 100 named destinations the run stays about 0.29 ms; with 1,000 destinations the result is noise-level because parsing/worker overhead dominates. With large destination trees, the UI/API avoids a quadratic traversal pause. In the 50,000-destination case, getDestinations() went from 5570.5 ms to 715.5 ms, saving about 4.9 s.

Benchmark:

I generated synthetic one-page PDFs with /Names << /Dests ... >> trees where the root has one leaf per named destination, then measured pdfDocument.getDestinations() in Node using the generic-legacy build.

Named destinations   master median/one-run   patched median/one-run   impact
100                  0.296 ms median         0.286 ms median          no visible change
1,000                6.78 ms median          7.13 ms median           no visible change/noise
20,000               388.6 ms median         211.7 ms median          ~1.8x faster, ~177 ms saved
50,000               5570.5 ms one run       715.5 ms one run         ~7.8x faster, ~4.9 s saved

Isolated NameTree.getAll() micro-benchmark on the same flat tree shape:

Tree entries   master median   patched median   speedup
100            0.0053 ms       0.0041 ms        ~1.3x
1,000          0.0550 ms       0.0305 ms        ~1.8x
50,000         152.9 ms        2.42 ms          ~63x

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 3, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 55.96%. Comparing base (6d5e869) to head (511a900).
⚠️ Report is 8 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #21213   +/-   ##
=======================================
  Coverage   55.95%   55.96%           
=======================================
  Files         218      218           
  Lines       59039    59033    -6     
=======================================
+ Hits        33034    33035    +1     
+ Misses      26005    25998    -7     
Flag Coverage Δ
fonttest 8.67% <ø> (+<0.01%) ⬆️
unittest 55.21% <100.00%> (+<0.01%) ⬆️
unittestcli 55.70% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread src/core/name_number_tree.js Outdated
@saripovdenis
Copy link
Copy Markdown
Author

@calixteman thank you for review! pushed a changed

@calixteman
Copy link
Copy Markdown
Contributor

/botio browsertest

@moz-tools-bot
Copy link
Copy Markdown
Collaborator

From: Bot.io (Linux m4)


Received

Command cmd_browsertest from @calixteman received. Current queue size: 0

Live output at: http://54.241.84.105:8877/5d377e52d089d5e/output.txt

@moz-tools-bot
Copy link
Copy Markdown
Collaborator

From: Bot.io (Windows)


Received

Command cmd_browsertest from @calixteman received. Current queue size: 0

Live output at: http://54.193.163.58:8877/09cc5ae668b195a/output.txt

@moz-tools-bot
Copy link
Copy Markdown
Collaborator

From: Bot.io (Linux m4)


Failed

Full output at http://54.241.84.105:8877/5d377e52d089d5e/output.txt

Total script time: 18.14 mins

  • Regression tests: FAILED
  different ref/snapshot: 1

Image differences available at: http://54.241.84.105:8877/5d377e52d089d5e/reftest-analyzer.html#web=eq.log

@moz-tools-bot
Copy link
Copy Markdown
Collaborator

From: Bot.io (Windows)


Failed

Full output at http://54.193.163.58:8877/09cc5ae668b195a/output.txt

Total script time: 24.40 mins

  • Regression tests: FAILED
  different ref/snapshot: 1

Image differences available at: http://54.193.163.58:8877/09cc5ae668b195a/reftest-analyzer.html#web=eq.log

@Snuffleupagus
Copy link
Copy Markdown
Collaborator

The commits must be squashed, note https://github.com/mozilla/pdf.js/wiki/Squashing-Commits, and please write a proper commit message (since a single line is never really sufficient).

Using Array.prototype.shift() to drain the traversal queue makes each
visited node move the remaining queued entries. For large name/number
trees this can make getAll() spend quadratic time in queue management.

Iterate over the queue with for...of instead. Children pushed while
iterating are still visited, and the queue no longer needs repeated
front removals.
@saripovdenis saripovdenis force-pushed the perf-name-tree-getall-queue-index branch from 511a900 to 473f9b4 Compare May 6, 2026 01:52
@saripovdenis
Copy link
Copy Markdown
Author

saripovdenis commented May 6, 2026

The commits must be squashed, note https://github.com/mozilla/pdf.js/wiki/Squashing-Commits, and please write a proper commit message (since a single line is never really sufficient).

@Snuffleupagus Thank you! Squashed & added a proper commit message

anything else I can improve in this PR?

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants