Skip to content

Inherit page-tree attributes in one top-down pass - #603

Merged
Velli20 merged 1 commit into
mainfrom
page-tree-inherit-once
Oct 25, 2026
Merged

Velli20 merged 1 commit into
mainfrom
page-tree-inherit-once

Conversation

@Velli20

@Velli20 Velli20 commented Oct 25, 2026

Copy link
Copy Markdown
Owner

Summary

Opening a document re-merged each page's inherited /Resources at every level of the page tree. Pages that share one /Resources dictionary also each got their own deep copy. Moving the PdfPage values themselves was cheap (about 120 bytes per move, no heap data copied).

  • Resources::merged_with_parent skips needless merges. It returns None when the parent defines nothing the child lacks, so the child's Arc is kept. It used to return Some every time.
  • Inheritance happens in one pass. PageTreeBranch reads each /Pages node exactly as written, then PageTree walks down the tree once from the root. Each node combines with its ancestors once, each page is merged at most once, and each page is moved once into the final Vec.
  • Shared merges. A ResourceMerges cache, keyed on the (own, inherited) Arc pointers, gives pages that share /Resources under the same ancestors one shared merged copy.

One test assertion changed: leaf_page_child_entries_take_priority_during_inheritance now expects the child's own Arc back, because there the parent adds nothing.

Measurements

Release profile (opt-level "z", fat LTO), base vs. change alternating over 5 rounds, best time per document:

Document Before After Allocations before → after
Generated 2,000-page tree, 3 levels, shared page /Resources 38.2 ms 11.6 ms 581k → 158k
examples/assets/* (8 documents) under 1.5% change (noise) ±1

Test plan

  • cargo test
  • cargo clippy --workspace --all-targets -- -D warnings, cargo fmt --check
  • PDFium corpus, read-only: 0 documents read worse or better than the base commit
  • PDFium corpus, full render: all 1,126 pages have the same status, metrics and draw count as the base commit
  • pdf.js corpus (needs a local PDFium library; CI covers it)

🤖 Generated with Claude Code

Decoding the page tree applied each /Pages node's inherited attributes to
every descendant at every level, so a page with its own /Resources was
merged once per resource-carrying ancestor, and pages sharing one
/Resources dictionary each got their own deep copy.

The tree is now decoded as written and flattened once from the root: each
node combines with its ancestors once, each page is merged at most once,
and merges are shared between pages that point at the same resources
below the same ancestors. Resources::merged_with_parent returns None when
the parent adds nothing, so the child's resources are kept as they are.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Velli20
Velli20 merged commit 04f06c7 into main Oct 25, 2026
10 checks passed
@Velli20
Velli20 deleted the page-tree-inherit-once branch October 25, 2026 14:35
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