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. */ if (line.cached_width != maxw) {
for (;;) { /* First run or width has changed, so we need to go through the layouter. Lines are moved into a cache to
auto l = line.layout->NextLine(maxw); * be reused if the width is not changed. */
if (l == nullptr) break; line.cached_layout.clear();
this->push_back(std::move(l)); 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. */ /* Break out if this was the last line. */

View File

@ -135,7 +135,7 @@ public:
* *
* It also accounts for the memory allocations and frees. * 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. std::string_view string; ///< Pointer to the original string.
/** Key into the linecache */ /** Key into the linecache */
@ -174,6 +174,9 @@ public:
FontState state_after; ///< Font state after the line. FontState state_after; ///< Font state after the line.
std::unique_ptr<ParagraphLayouter> layout = nullptr; ///< Layout of 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: private:
typedef std::map<LineCacheKey, LineCacheItem, LineCacheCompare> LineCache; typedef std::map<LineCacheKey, LineCacheItem, LineCacheCompare> LineCache;