Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Dynamically link against system fontconfig #40725

Merged
merged 3 commits into from
May 25, 2023

Conversation

vially
Copy link
Contributor

@vially vially commented Mar 28, 2023

This is the engine counterpart for flutter/buildroot#701. That pull-request contains more context and the main motivation for this change.

This should fix some startup performance issues related to font loading on desktop linux: flutter/flutter#118911.

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide and the C++, Objective-C, Java style guides.
  • I listed at least one issue that this PR fixes in the description above.
  • I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test-exempt. See testing the engine for instructions on writing and running engine tests.
  • I updated/added relevant documentation (doc comments with ///).
  • I signed the CLA.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@chinmaygarde
Copy link
Member

Waiting on hearing back on the original issue regarding the impact of this for third party embedders.

@vially vially force-pushed the use-system-font-config branch from 62aa884 to f034b29 Compare April 11, 2023 16:48
@dongfengweixiao
Copy link

Waiting on hearing back on the original issue regarding the impact of this for third party embedders.

Is there a plan for validation and consolidation in this submit? This issue greatly affects usage.

@chinmaygarde
Copy link
Member

We believe this is good to go. The buildroot patch that has been approved needs to land first though.

cbracken pushed a commit to flutter/buildroot that referenced this pull request May 24, 2023
Statically linking the fontconfig library into the flutter app can cause
(runtime) performance issues on systems where the statically linked
fontconfig is using a different [on-disk cache format
version](https://gitlab.freedesktop.org/fontconfig/fontconfig/-/blob/c2666a6d9a6ed18b1bfcef8176e25f62993e24db/fontconfig/fontconfig.h#L60-70)
from the system fontconfig version. This is particularly relevant on
systems with a large number of fonts installed:
flutter/flutter#118911.

This pull-request fixes that by replacing the statically linked
fontconfig version with dynamic linking to the system version. For what
it's worth, by default Skia is also [dynamically linking against system
fontconfig](https://github.com/google/skia/blob/398b9ac94aa3f49e0fdab319a27584c2a2e3e63e/third_party/BUILD.gn#L10).

**Warning**: This requires a similar change to the Flutter engine
(flutter/engine#40725).

### Underlying root issue

On a typical Linux desktop distribution[^1], the package manager
automatically updates the *system* fontconfig cache (e.g.:
`/var/cache/fontconfig`) when *system* fonts are installed.

This means that applications using a version of fontconfig with *the
same on-disk cache format as the system fontconfig* should be able to
use the system cache (and therefore have reasonably fast font
discovery).

However, when the application and system fontconfig versions do not
match, the app is unable to use the system font cache so it has to
re-index all the system fonts (which is a significantly slower
operation).

In addition to that, in an attempt to speed-up subsequent launches of
the app, the fontconfig library persists the system font cache to disk
on application startup. Since user-space apps typically do *not* have
write access to the *system* font cache directory (e.g.:
`/var/cache/fontconfig`), the fontconfig library ends up writing the
cache to the *user* fontconfig cache (e.g.: `~/.cache/fontconfig`) which
ends up slowing the application startup even futher (due to the extra
IO).

This part is a bit unclear as to *why* it happens, but it turns out
fontconfig does not attempt to read the *user* cache on the next app
startup. So the whole process starts over again on every app launch:
*system* font cache not found => fonts re-indexed => font cache written
to *user* cache (which is never to be read again).

### Benchmarks

> **Note**
> 
> These tests were performed on an ArchLinux system with approximately
6000 system fonts installed
([ttf-google-fonts-git](https://aur.archlinux.org/packages/ttf-google-fonts-git))
using version `2.14.2` of fontconfig (`FC_CACHE_VERSION: 8`).

The following graph shows the startup times (time to first frame as
reported by `flutter run --profile`) for ten consecutive runs using
statically linked fontconfig versus dynamically linked fontconfig. The
average startup time for statically linked fontconfig was 2186 ms versus
368 ms for dynamically linked.


![chart](https://user-images.githubusercontent.com/433598/228365920-1f90eddb-683e-46d3-9aec-4df22fa222db.png)

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read the [Flutter Style Guide] _recently_, and have followed its
advice.
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [ ] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#overview
[Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene
[test-exempt]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes
[Discord]: https://github.com/flutter/flutter/wiki/Chat
[^1]: This has only been tested on ArchLinux but I would expect other
Linux distributions to work in a similar way.
Copy link
Member

@cbracken cbracken left a comment

Choose a reason for hiding this comment

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

LGTM stamp from a Japanese personal seal

@cbracken cbracken added the autosubmit Merge PR when tree becomes green via auto submit App label May 25, 2023
@auto-submit auto-submit bot removed the autosubmit Merge PR when tree becomes green via auto submit App label May 25, 2023
@auto-submit
Copy link
Contributor

auto-submit bot commented May 25, 2023

auto label is removed for flutter/engine, pr: 40725, due to This PR has not met approval requirements for merging. You are not a member of flutter-hackers and need 1 more review(s) in order to merge this PR.

  • Merge guidelines: You need at least one approved review if you are already part of flutter-hackers or two member reviews if you are not a flutter-hacker before re-applying the autosubmit label. Reviewers: If you left a comment approving, please use the "approve" review action instead.

@cbracken
Copy link
Member

Thanks for applying the update. Looks great! Thanks for your patience @vially.

@cbracken
Copy link
Member

Just noticed this doesn't include the buildroot roll. Can you update this line in the DEPS file with the value e522d38d629d7522f0589e754886ed2b82232d9e?

@cbracken cbracken force-pushed the use-system-font-config branch from f034b29 to e33a29a Compare May 25, 2023 16:41
@cbracken
Copy link
Member

Actually no worries - I pushed a commit to do it :)

@cbracken cbracken added the autosubmit Merge PR when tree becomes green via auto submit App label May 25, 2023
@auto-submit auto-submit bot merged commit 81dec9f into flutter:main May 25, 2023
@vially vially deleted the use-system-font-config branch May 25, 2023 19:02
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request May 25, 2023
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request May 25, 2023
…127619)

flutter/engine@6ff02c1...99f2653

2023-05-25 jacksongardner@google.com Implement Web Codecs for Skwasm (flutter/engine#42184)
2023-05-25 godofredoc@google.com Move linux clang tidy to engine_v2. (flutter/engine#42112)
2023-05-25 skia-flutter-autoroll@skia.org Roll Skia from 69d0aa065992 to e648bf802cd2 (11 revisions) (flutter/engine#42323)
2023-05-25 jacksongardner@google.com Only use 8 cores for web test compilation. (flutter/engine#42321)
2023-05-25 skia-flutter-autoroll@skia.org Roll Fuchsia Linux SDK from nLnQzTesaABpgroOl... to DNwJQMQZladAsKTjv... (flutter/engine#42322)
2023-05-25 valentin.haloiu@gmail.com Dynamically link against system fontconfig (flutter/engine#40725)

Also rolling transitive DEPS:
  fuchsia/sdk/core/linux-amd64 from nLnQzTesaABp to DNwJQMQZladA

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-engine-flutter-autoroll
Please CC bdero@google.com,rmistry@google.com,zra@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://bugs.chromium.org/p/skia/issues/entry?template=Autoroller+Bug

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
autosubmit Merge PR when tree becomes green via auto submit App
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants