From ac4a7d02c7e6fdd76af801f7eeabc1e9efeefcff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Simonis?= Date: Mon, 16 Aug 2021 11:18:47 +0200 Subject: [PATCH] Codechange: Improve LineCache queries (#9417) Adds the support to query the linecache without copying the string. This uses a custom transparent comparator in conjunction with a query type using a std::string_view. --- src/gfx_layout.cpp | 6 ++++++ src/gfx_layout.h | 27 ++++++++++++++++++++------- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/gfx_layout.cpp b/src/gfx_layout.cpp index 0baeb32d50..38f6d62e59 100644 --- a/src/gfx_layout.cpp +++ b/src/gfx_layout.cpp @@ -888,6 +888,12 @@ Layouter::LineCacheItem &Layouter::GetCachedParagraphLayout(const char *str, siz linecache = new LineCache(); } + if (auto match = linecache->find(LineCacheQuery{state, std::string_view{str, len}}); + match != linecache->end()) { + return match->second; + } + + /* Create missing entry */ LineCacheKey key; key.state_before = state; key.str.assign(str, len); diff --git a/src/gfx_layout.h b/src/gfx_layout.h index 4854be6e55..15248a277d 100644 --- a/src/gfx_layout.h +++ b/src/gfx_layout.h @@ -17,6 +17,8 @@ #include #include #include +#include +#include #include #ifdef WITH_ICU_LX @@ -155,14 +157,25 @@ class Layouter : public std::vector + bool operator()(const Key1 &lhs, const Key2 &rhs) const { - if (this->state_before.fontsize != other.state_before.fontsize) return this->state_before.fontsize < other.state_before.fontsize; - if (this->state_before.cur_colour != other.state_before.cur_colour) return this->state_before.cur_colour < other.state_before.cur_colour; - if (this->state_before.colour_stack != other.state_before.colour_stack) return this->state_before.colour_stack < other.state_before.colour_stack; - return this->str < other.str; + if (lhs.state_before.fontsize != rhs.state_before.fontsize) return lhs.state_before.fontsize < rhs.state_before.fontsize; + if (lhs.state_before.cur_colour != rhs.state_before.cur_colour) return lhs.state_before.cur_colour < rhs.state_before.cur_colour; + if (lhs.state_before.colour_stack != rhs.state_before.colour_stack) return lhs.state_before.colour_stack < rhs.state_before.colour_stack; + return lhs.str < rhs.str; } }; public: @@ -179,7 +192,7 @@ public: ~LineCacheItem() { delete layout; free(buffer); } }; private: - typedef std::map LineCache; + typedef std::map LineCache; static LineCache *linecache; static LineCacheItem &GetCachedParagraphLayout(const char *str, size_t len, const FontState &state);