From 1ec0f58297f590dfc0884688f2f3167a14e31c13 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 30 Jan 2023 12:19:17 -0700 Subject: [PATCH] [coverage] Add a static cache Speeds up Roboto by around 5%; costs 2kb globally. --- src/OT/Layout/Common/CoverageFormat1.hh | 12 ++++++++++++ src/OT/Layout/Common/CoverageFormat2.hh | 13 +++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/OT/Layout/Common/CoverageFormat1.hh b/src/OT/Layout/Common/CoverageFormat1.hh index 5d68e3d15..e8a082d57 100644 --- a/src/OT/Layout/Common/CoverageFormat1.hh +++ b/src/OT/Layout/Common/CoverageFormat1.hh @@ -30,6 +30,8 @@ #ifndef OT_LAYOUT_COMMON_COVERAGEFORMAT1_HH #define OT_LAYOUT_COMMON_COVERAGEFORMAT1_HH +#include "../../../hb-cache.hh" + namespace OT { namespace Layout { namespace Common { @@ -57,8 +59,18 @@ struct CoverageFormat1_3 unsigned int get_coverage (hb_codepoint_t glyph_id) const { + static hb_cache_t<16, 8, 9, true> cache; + + unsigned v; + if (cache.get ((glyph_id + (uintptr_t) this) & 0xFFFF, &v)) + { + if (glyphArray[v] == glyph_id) + return v; + } + unsigned int i; glyphArray.bfind (glyph_id, &i, HB_NOT_FOUND_STORE, NOT_COVERED); + cache.set ((glyph_id + (uintptr_t) this) & 0xFFFF, i); return i; } diff --git a/src/OT/Layout/Common/CoverageFormat2.hh b/src/OT/Layout/Common/CoverageFormat2.hh index d7fcc3520..64af59900 100644 --- a/src/OT/Layout/Common/CoverageFormat2.hh +++ b/src/OT/Layout/Common/CoverageFormat2.hh @@ -31,6 +31,8 @@ #include "RangeRecord.hh" +#include "../../../hb-cache.hh" + namespace OT { namespace Layout { namespace Common { @@ -59,7 +61,18 @@ struct CoverageFormat2_4 unsigned int get_coverage (hb_codepoint_t glyph_id) const { + static hb_cache_t<16, 8, 9, true> cache; + + unsigned v; + if (cache.get ((glyph_id + (uintptr_t) this) & 0xFFFF, &v)) + { + const RangeRecord &range = rangeRecord[v]; + if (range.first <= glyph_id && glyph_id <= range.last) + return (unsigned int) range.value + (glyph_id - range.first); + } + const RangeRecord &range = rangeRecord.bsearch (glyph_id); + cache.set ((glyph_id + (uintptr_t) this) & 0xFFFF, &range - &rangeRecord[0]); return likely (range.first <= range.last) ? (unsigned int) range.value + (glyph_id - range.first) : NOT_COVERED;