-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Optimize editing long text by caching each paragraph #5411
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
Conversation
Preview available at https://egui-pr-preview.github.io/pr/5411-cachegalleylines |
Okay, so I did the thing and implemented the Currently this "somewhat" works, I've managed to fix the selection issues I was seeing, but there is some layout issues you can actually see in the live demo above where the text is overlapping sometimes when wrapped (since this was present before the whole |
So I've managed to make this look pretty correct, these are the remaining issues:
|
I think I've made some good progress:
The remaining test failures are:
|
06e639e
to
139f286
Compare
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.
This looks good! I haven't reviewed all of GalleyCache
yet, because it is a very dense and complex function.
I wonder how we best test and/or benchmark this though 🤔
I defiantly feel we should have a benchmark to justify this code, i.e. something that shows a big speedup before/after this PR.
// Note we **don't** ignore the leading/trailing whitespace here! | ||
row.size.x = target_max_x - target_min_x; |
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.
Why? What does this affect?
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.
Ah, now that I think about it changing this comment was a mistake, leading and trailing whitespace is actually still ignored (I don't know why I thought otherwise). I'll change it back.
However ignoring whitespace here does seem kind of strange since the current docs for Row::rect
say that it includes whitespace. Wouldn't this be wrong after it passes through this function? Maybe at least that documentation could be adjusted to clarify this is only true for left-aligned non-justified text.
I also noticed a more important issue though I think, this function actually makes Row
s have glyphs that are outside their PlacedRow::pos
+ Row::size
rectangle (because it just repositions the glyphs and leaves the position unchanged). This currently breaks selecting such text. I'll try to fix this soon.
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.
Fixed the issue found above and the comment. Although I still want to know what you think about changing the Row::size
comment, in general halign is surprising with how it suddenly totally changes how the coordinates work even though it makes perfect sense in hindsight.
I agree having a benchmark would be nice, currently the only thing I have is "it looks quicker" when you have a lot of text (deletion of small segments works instantly for example).
As for tests, I've encountered issues with selection drawing multiple times while working on this at this point, so maybe it would be beneficial if this was actually checked by the test suite, it looks like mouse events are supported in kittest so maybe that's a good start. Especially since I also think selection visuals might be another good place for optimization because currently selecting the whole text will force all It also looks like the |
f406701
to
40f237d
Compare
I need to take a break from this now. The benchmark shows only about a ~2x speedup, which is less than I would have thought, so I think it is doing extra layouting even when it shouldn't. If anyone wants to take a look at that I would appreciate 9E88 it. |
I don't know if this is the issue you're observing, but I noticed another performance issue in the current implementation. Although even after completely disabling cache eviction, the re-layout still takes quite a bit of time even if just one row was invalidated, I'll see if that can be improved after fixing the eviction issue. |
Okay I put off fixing the invalidation because it's non-trivial, but I managed to improve performance quite a bit by simply not re-rounding all row sizes in |
Not adding the merged |
crates/epaint/src/text/fonts.rs
Outdated
// TODO(afishhh): This Galley cannot be added directly into the cache without taking | ||
// extra precautions to make sure all component paragraph Galleys are not invalidated | ||
// immediately next frame (since their `last_used` will not be updated). | ||
Arc::new(galley) |
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.
This means we don't cache the full galley, and so do the paragraph-splitting and concatenation each frame… that's potentially a performance regression for large texts.
Better I think to just improve the cache to store the hashes of all sub-paragraphs and also update them as being used when the big parent is used
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.
So I implemented this, but I still seem to experience eviction of all the paragraphs in my practical application of this, although it doesn't seem to happen in a small test app. I've pushed the implementation for review (better name or approach for layout_internal
?) and may investigate the weird cache evictions later (would blame deferred viewpoints but that wouldn't explain why it worked slightly better when re-splitting every frame).
ff1d46b
to
9d61f9c
Compare
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.
Nice, let's get this in!
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.
Nice, let's get this in!
)" This reverts commit 557bd56.
)" This reverts commit 557bd56.
)" This reverts commit 557bd56.
)" This reverts commit 557bd56.
)" This reverts commit 557bd56.
)" This reverts commit 557bd56.
)" This reverts commit 557bd56.
What
(written by @emilk)
When editing long text (thousands of line), egui would previously re-layout the entire text on each edit. This could be slow.
With this PR, we instead split the text into paragraphs (split on
\n
) and then cache each such paragraph. When editing text then, only the changed paragraph needs to be laid out again.Still, there is overhead from splitting the text, hashing each paragraph, and then joining the results, so the runtime complexity is still O(N).
In our benchmark, editing a 2000 line string goes from ~8ms to ~300 ms, a speedup of ~25x.
In the future, we could also consider laying out each paragraph in parallel, to speed up the initial layout of the text.
Details
This is an
almost completeimplementation of the approach described by emilk in this comment, excluding CoW semantics forLayoutJob
(but including them forRow
).It supersedes the previous unsuccessful attempt here: #4000.
Draft because:
Currently individual rows will haveends_with_newline
always set to false.This breaks selection with Ctrl+A (and probably many other things)
The whole block for doing the splitting and merging should probably become a function (I'll do that later).I haven't run the check scr 8000 ipt, the tests, and haven't made sure all of the examples build (although I assume they probably don't rely on Galley internals).Layout is sometimes incorrect (missing empty lines, wrapping sometimes makes text overlap).Row
s. Also this requires that we're fine making these very breaking changes.It does significantly improve the performance of rendering large blocks of text (if they have many newlines), this is the test program I used to test it (adapted from #3086):
code
I think the way to proceed would be to make a new type, something like
PositionedRow
, that would wrap anArc<Row>
but have a separatepos
and(that would meanends_with_newline
Row
only holds asize
instead of arect
). This type would of course have getters that would allow you to easily get aRect
from it and probably aDeref
to the underlyingRow
.I haven't done this yet because I wanted to get some opinions whether this would be an acceptable API first.This is now implemented, but of course I'm still open to discussion about this approach and whether it's what we want to do.Breaking changes (currently):
Galley::rows
field has a different type.PlacedRow
wrapper forRow
.Row
now uses a coordinate system relative to itself instead of theGalley
.TextEdit
#3086