1
0
Fork 0

Codechange: Cache layouted text for the last used width. (#14177)

pull/14199/head
Peter Nelson 2025-05-03 18:33:47 +01:00 committed by GitHub
parent bd1a3fe0b7
commit 7596f98e2d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 6 deletions

View File

@ -174,11 +174,21 @@ Layouter::Layouter(std::string_view str, int maxw, FontSize fontsize) : string(s
}
}
/* Move all lines into a local cache so we can reuse them later on more easily. */
for (;;) {
auto l = line.layout->NextLine(maxw);
if (l == nullptr) break;
this->push_back(std::move(l));
if (line.cached_width != maxw) {
/* First run or width has changed, so we need to go through the layouter. Lines are moved into a cache to
* be reused if the width is not changed. */
line.cached_layout.clear();
line.cached_width = maxw;
for (;;) {
auto l = line.layout->NextLine(maxw);
if (l == nullptr) break;
line.cached_layout.push_back(std::move(l));
}
}
/* Retrieve layout from the cache. */
for (const auto &l : line.cached_layout) {
this->push_back(l.get());
}
/* Break out if this was the last line. */

View File

@ -135,7 +135,7 @@ public:
*
* It also accounts for the memory allocations and frees.
*/
class Layouter : public std::vector<std::unique_ptr<const ParagraphLayouter::Line>> {
class Layouter : public std::vector<const ParagraphLayouter::Line *> {
std::string_view string; ///< Pointer to the original string.
/** Key into the linecache */
@ -174,6 +174,9 @@ public:
FontState state_after; ///< Font state after the line.
std::unique_ptr<ParagraphLayouter> layout = nullptr; ///< Layout of the line.
std::vector<std::unique_ptr<const ParagraphLayouter::Line>> cached_layout{}; ///< Cached results of line layouting.
int cached_width = 0; ///< Width used for the cached layout.
};
private:
typedef std::map<LineCacheKey, LineCacheItem, LineCacheCompare> LineCache;