-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Explicit text caching #2058
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imberflur
reviewed
Aug 31, 2023
tarkah
reviewed
Aug 31, 2023
We use `MaybeUninit` here instead of `Option` to save some cycles, but I will most likely change it for an `Option` since unsafe code is quite scary.
hecrj
commented
Sep 9, 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. |
There was a problem hiding this comment.
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!
Good thing I just set up CI earlier for this 😅
This was referenced Mar 9, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR enables explicit text caching at the widget level by introducing a new
Paragraph
associated type tocore::text::Renderer
.Until now, widgets could draw text indirectly by issuing a
Renderer::fill_text
command. TheRenderer
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
. AParagraph
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, aParagraph
is just a thin wrapper of acosmic_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 callingRenderer::update_paragraph
duringWidget::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
, andToggler
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:layout::next_to_each_other
helper. No more awkwardly usingRow
inside widget code!Widget::layout
now takes a mutablewidget::Tree
. This is useful to cache layout data or update managed paragraphs, for instance.Pixels
type now.core::Text
does no longer havecolor
. Instead, thecolor
is provided to theRenderer
when drawing.bounds
incore::Text
are now aSize
instead of aRectangle
. The finalposition
of theText
must be provided when drawing.TODOs
RenameI can't find a more descriptive name, soParagraph
. 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 liketext::Allocation
ortext::Buffer
.Paragraph
it is!Paragraph
drawing iniced_tiny_skia
.Icon
support forTextInput
.