Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit text caching #2058

Merged
merged 28 commits into from
Sep 9, 2023
Merged

Explicit text caching #2058

merged 28 commits into from
Sep 9, 2023

Conversation

hecrj
Copy link
Member

@hecrj hecrj commented Aug 30, 2023

This PR enables explicit text caching at the widget level by introducing a new Paragraph associated type to core::text::Renderer.

Until now, widgets could draw text indirectly by issuing a Renderer::fill_text command. The Renderer then took care of layout and caching behind the scenes. This caused a bunch of challenges; specifically, hashing, aliasing, and cache invalidation.

With caching, memory management also becomes tricky and less intuitive. If a specific frame needs to measure a lot of intermediate text, then the internal map used for caching may allocate a bunch of memory unnecessarily, as most of the entries may not be reused.

After this PR, a widget can choose to create and manage a Paragraph. A Paragraph represents a bunch of text that has been laid out and is ready to be rendered at minimum cost. Since it's already shaped and laid out, its bounds are already computed and, therefore, measuring it has no cost. Internally, a Paragraph is just a thin wrapper of a cosmic_text::Buffer.

The tricky part is that a Paragraph needs to be managed. Widgets are responsible to keep it in sync manually with their state. This normally consists in calling Renderer::update_paragraph during Widget::layout, which will try to reconcile any differences as efficiently as possible (e.g. a change in paragraph bounds can skip shaping altogether!).

Text, TextInput, Checkbox, Radio, and Toggler use the new managed approach, which I expect should improve performance and memory usage in most cases.

There are some cases where caching is superior, however. Specifically, given that paragraphs are stored in the widget state tree, a small change in the widget tree can cause a bunch of text to be relaid out (potentially all of it!). The cached approach doesn't have this problem because caching happens on a per-frame basis, independently of the widget tree.

However, I have some ideas to improve the diffing strategy to handle most common cases more efficiently. And it's also important to note that a text::Renderer does still support the cached strategy, so we could eventually give users some control over the strategy used.

This also sets up some of the foundations necessary for multi-line text editing, as it is now possible to easily query and manipulate text buffers without recalculating (nor hashing) everything.

Related changes

Besides the Paragraph additions, there have been some other changes here and there:

  • A new layout::next_to_each_other helper. No more awkwardly using Row inside widget code!
  • Widget::layout now takes a mutable widget::Tree. This is useful to cache layout data or update managed paragraphs, for instance.
  • All text sizes are represented by the Pixels type now.
  • core::Text does no longer have color. Instead, the color is provided to the Renderer when drawing.
  • The bounds in core::Text are now a Size instead of a Rectangle. The final position of the Text must be provided when drawing.

TODOs

  • Rename Paragraph. Technically, Paragraph isn't the right name because the idea it currently represents can contain multiple paragraphs. I'm considering renaming it to something different like text::Allocation or text::Buffer. I can't find a more descriptive name, so Paragraph it is!
  • Implement Paragraph drawing in iced_tiny_skia.
  • Implement Icon support for TextInput.
  • Invalidate paragraphs when a new font is loaded.

@hecrj hecrj added this to the 0.11.0 milestone Aug 30, 2023
graphics/src/text/paragraph.rs Outdated Show resolved Hide resolved
graphics/Cargo.toml Outdated Show resolved Hide resolved
@hecrj hecrj added the addition label Sep 6, 2023
Comment on lines +3 to +39
//! # What is continuity?
//! Continuity is the feeling of persistence of state.
//!
//! In a graphical user interface, users expect widgets to have a
//! certain degree of continuous state. For instance, a text input
//! that is focused should stay focused even if the widget tree
//! changes slightly.
//!
//! Continuity is tricky in `iced` and the Elm Architecture because
//! the whole widget tree is rebuilt during every `view` call. This is
//! very convenient from a developer perspective because you can build
//! extremely dynamic interfaces without worrying about changing state.
//!
//! However, the tradeoff is that determining what changed becomes hard
//! for `iced`. If you have a list of things, adding an element at the
//! top may cause a loss of continuity on every element on the list!
//!
//! # How can we keep continuity?
//! The good news is that user interfaces generally have a static widget
//! structure. This structure can be relied on to ensure some degree of
//! continuity. `iced` already does this.
//!
//! However, sometimes you have a certain part of your interface that is
//! quite dynamic. For instance, a list of things where items may be added
//! or removed at any place.
//!
//! There are different ways to mitigate this during the reconciliation
//! stage, but they involve comparing trees at certain depths and
//! backtracking... Quite computationally expensive.
//!
//! One approach that is cheaper consists in letting the user provide some hints
//! about the identities of the different widgets so that they can be compared
//! directly without going deeper.
//!
//! The widgets in this module will all ask for a "hint" of some sort. In order
//! to help them keep continuity, you need to make sure the hint stays the same
//! for the same items in your user interface between `view` calls.
Copy link
Member Author

Choose a reason for hiding this comment

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

Hey! I wrote some docs for once!

@hecrj hecrj enabled auto-merge September 9, 2023 22:47
Good thing I just set up CI earlier for this 😅
@hecrj hecrj merged commit 1af5ff4 into master Sep 9, 2023
24 checks passed
@hecrj hecrj deleted the explicit-text-caching branch September 9, 2023 23:14
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.

3 participants