[coverage] Add a static cache

Speeds up Roboto by around 5%; costs 2kb globally.
This commit is contained in:
Behdad Esfahbod 2023-01-30 12:19:17 -07:00
parent a924bbcfce
commit 1ec0f58297
2 changed files with 25 additions and 0 deletions

View File

@ -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;
}

View File

@ -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<Types> &range = rangeRecord[v];
if (range.first <= glyph_id && glyph_id <= range.last)
return (unsigned int) range.value + (glyph_id - range.first);
}
const RangeRecord<Types> &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;