Skip to content

Fix Rust font fallback performance - #177

Merged
shps951023 merged 1 commit into
mainfrom
fix/rust-font-fallback-cache
Sep 14, 2026
Merged

Fix Rust font fallback performance#177
shps951023 merged 1 commit into
mainfrom
fix/rust-font-fallback-cache

Conversation

@shps951023

@shps951023 shps951023 commented Sep 14, 2026

Copy link
Copy Markdown
Member

Summary

  • cache registered-font character support instead of reparsing each font for every character
  • cache complete font selections by character, style, preferred family, and font-set size
  • invalidate cached support and selections when a new font is registered
  • add focused tests for support caching, selection reuse, and invalidation

Performance

Using the existing nthu_article.docx fixture (2,073 words) with copied Arial fonts:

Registered fonts Before median After median
10 0.62 s 0.48 s
300 6.75 s 0.86 s

All generated PDFs remained byte-identical to the baseline.

Validation

  • cargo fmt --check
  • cargo clippy --workspace --all-targets -- -D warnings
  • cargo test --workspace (112 library tests and 1 CLI test passed)
  • git diff --check

Fixes #164

Summary by CodeRabbit

  • Performance

    • Improved PDF font selection performance by reusing font-support results during document generation.
    • Reduced repeated font checks, which may improve processing times for documents containing many characters or fonts.
  • Bug Fixes

    • Updated font handling so newly registered fonts are recognized immediately, preventing stale font-selection results from affecting generated PDFs.

Copilot AI lite review requested due to automatic review settings September 14, 2026 16:45
@shps951023
shps951023 merged commit 0216eda into main Sep 14, 2026
6 of 7 checks passed
@coderabbitai

coderabbitai Bot commented Sep 14, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: 4793c361-474d-4417-9bfb-1cfcbfd5d817

📥 Commits

Reviewing files that changed from the base of the PR and between ae9671f and 51c4b05.

📒 Files selected for processing (2)
  • minipdf-rs/crates/minipdf/src/lib.rs
  • minipdf-rs/crates/minipdf/src/pdf.rs

📝 Walkthrough

Walkthrough

The change adds a synchronized global cache for font support and selection. Font registration clears the cache. Tests verify repeated probes, selection caching, and cache clearing.

Changes

Font support caching

Layer / File(s) Summary
Cached font selection and support probes
minipdf-rs/crates/minipdf/src/pdf.rs
FontSupportCache stores font support and selection results. select_font and font_supports use the cache and probe uncached results.
Cache invalidation and validation
minipdf-rs/crates/minipdf/src/lib.rs, minipdf-rs/crates/minipdf/src/pdf.rs
register_font clears cached results after adding a font. Tests verify support caching, selection caching, and clearing both maps.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Sequence Diagram(s)

sequenceDiagram
  participant select_font
  participant FontSupportCache
  participant probe_font_support
  select_font->>FontSupportCache: request font for character and style
  FontSupportCache->>probe_font_support: probe uncached font support
  probe_font_support-->>FontSupportCache: return support result
  FontSupportCache-->>select_font: return selected font
Loading
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/rust-font-fallback-cache

Comment @coderabbitai help to get the list of available commands.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Address global mutex contention, unbounded cache growth, and missing regression coverage.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Improves Rust font fallback performance by caching font support and selections across conversions.

Changes:

  • Adds process-wide support and selection caches.
  • Invalidates caches when fonts are registered.
  • Adds focused cache behavior tests.
File summaries
File Description
minipdf-rs/crates/minipdf/src/pdf.rs Implements font caching and related tests.
minipdf-rs/crates/minipdf/src/lib.rs Triggers cache invalidation during font registration.
Review details

Suppressed comments (1)

minipdf-rs/crates/minipdf/src/pdf.rs:1384

  • This test does not actually prove selection reuse: repeating the same call and checking selections.len() == 1 also passes if the cache lookup is removed and each call recomputes then overwrites the same key. Add an observable probe counter or test seam so the optimization is protected against that regression.
        for _ in 0..100 {
            assert_eq!(
                cache.select_font(&fonts, 'A', false, false, Some("Arial")),
                None
            );
  • Files reviewed: 2/2 changed files
  • Comments generated: 3
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +10 to +12
struct FontSupportCache {
support_by_character: HashMap<char, Vec<Option<bool>>>,
selections: HashMap<FontSelectionKey, Option<usize>>,
Comment on lines +805 to +809
let cache = FONT_SUPPORT_CACHE.get_or_init(|| Mutex::new(FontSupportCache::default()));
cache
.lock()
.expect("font support cache lock poisoned")
.select_font(fonts, ch, bold, italic, preferred_font)
name: name.into(),
data: font_data.into(),
});
pdf::clear_font_support_cache();
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.

[Bug]: Rust conversion gets very slow when many fonts are registered

2 participants