Skip to content

Avoid cloning owned font data when constructing font faces#8320

Open
undermoonn wants to merge 1 commit into
emilk:mainfrom
undermoonn:epaint-share-font-data
Open

Avoid cloning owned font data when constructing font faces#8320
undermoonn wants to merge 1 commit into
emilk:mainfrom
undermoonn:epaint-share-font-data

Conversation

@undermoonn

Copy link
Copy Markdown
  • Closes N/A
  • I have followed the instructions in the PR template

Summary

Reuse the Arc<FontData> already stored in FontDefinitions as the owning Blob for each font face.

Previously, blob_from_font_data cloned FontData. When the font uses Cow::Owned, this also cloned the entire underlying font byte buffer.

FontData already implements AsRef<[u8]>, so its existing Arc can be used directly as the stable owner required by FontCell and Skrifa.

Reproduction

A black-box eframe reproduction is available here:

https://github.com/undermoonn/egui-font-data-clone-repro

The repository contains two branches:

  • main: crates.io eframe/epaint 0.35.0
  • patched: the same application with only epaint and emath patched to this change

The application code and font are identical between the branches. It loads a
7.95 MiB Noto Sans SC font from disk using FontData::from_owned and renders
through glow/OpenGL.

Three Windows runs were measured for each branch:

Branch Private bytes mean Working set mean
main 99.46 MiB 85.61 MiB
patched 91.63 MiB 77.61 MiB
Reduction 7.83 MiB 8.00 MiB

The process-level reduction closely matches the 7.95 MiB font file.

Safety

FontCell retains its own strong Arc reference, so the font bytes remain alive
at a stable address for as long as Skrifa references them.

The shared data cannot be mutated in place. Any use of Arc::make_mut creates a
separate copy instead of modifying bytes referenced by an existing font face.

Testing

  • cargo test --locked -p epaint --lib — 45 passed
  • cargo clippy --locked -p epaint --all-targets --all-features -- -D warnings
  • cargo check --locked -p epaint --no-default-features
  • cargo fmt -p epaint -- --check
  • Black-box eframe comparison linked above

@github-actions

Copy link
Copy Markdown

Preview is being built...

Preview will be available at https://egui-pr-preview.github.io/pr/8320-epaint-share-font-data

View snapshot changes at kitdiff

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