[cache] Add a non-threadsafe version

Use in hb-ft, since already mutex'ed.
This commit is contained in:
Behdad Esfahbod 2022-08-03 13:00:48 -06:00
parent f73c15ca6c
commit ec90d1e161
3 changed files with 17 additions and 8 deletions

View File

@ -32,13 +32,18 @@
/* Implements a lockfree cache for int->int functions. */ /* Implements a lockfree cache for int->int functions. */
template <unsigned int key_bits=16, unsigned int value_bits=8 + 32 - key_bits, unsigned int cache_bits=8> template <unsigned int key_bits=16,
unsigned int value_bits=8 + 32 - key_bits,
unsigned int cache_bits=8,
bool thread_safe=true>
struct hb_cache_t struct hb_cache_t
{ {
static_assert ((key_bits >= cache_bits), ""); static_assert ((key_bits >= cache_bits), "");
static_assert ((key_bits + value_bits - cache_bits <= 8 * sizeof (hb_atomic_int_t)), ""); static_assert ((key_bits + value_bits - cache_bits <= 8 * sizeof (hb_atomic_int_t)), "");
static_assert (sizeof (hb_atomic_int_t) == sizeof (unsigned int), ""); static_assert (sizeof (hb_atomic_int_t) == sizeof (unsigned int), "");
using item_t = typename std::conditional<thread_safe, hb_atomic_int_t, int>::type;
void init () { clear (); } void init () { clear (); }
void fini () {} void fini () {}
@ -70,11 +75,15 @@ struct hb_cache_t
} }
private: private:
hb_atomic_int_t values[1u<<cache_bits]; item_t values[1u<<cache_bits];
}; };
typedef hb_cache_t<21, 16, 8> hb_cmap_cache_t;
typedef hb_cache_t<16, 24, 8> hb_advance_cache_t; template <bool thread_safe = true>
using hb_cmap_cache_t = hb_cache_t<21, 16, 8, thread_safe>;
template <bool thread_safe = true>
using hb_advance_cache_t = hb_cache_t<16, 24, 8, thread_safe>;
#endif /* HB_CACHE_HH */ #endif /* HB_CACHE_HH */

View File

@ -89,7 +89,7 @@ struct hb_ft_font_t
mutable hb_mutex_t lock; mutable hb_mutex_t lock;
FT_Face ft_face; FT_Face ft_face;
mutable unsigned cached_serial; mutable unsigned cached_serial;
mutable hb_advance_cache_t advance_cache; mutable hb_advance_cache_t<false> advance_cache;
}; };
static hb_ft_font_t * static hb_ft_font_t *

View File

@ -65,7 +65,7 @@ struct hb_ot_font_t
/* h_advance caching */ /* h_advance caching */
mutable hb_atomic_int_t cached_coords_serial; mutable hb_atomic_int_t cached_coords_serial;
mutable hb_atomic_ptr_t<hb_advance_cache_t> advance_cache; mutable hb_atomic_ptr_t<hb_advance_cache_t<>> advance_cache;
}; };
static hb_ot_font_t * static hb_ot_font_t *
@ -161,14 +161,14 @@ hb_ot_get_glyph_h_advances (hb_font_t* font, void* font_data,
bool use_cache = false; bool use_cache = false;
#endif #endif
hb_advance_cache_t *cache = nullptr; hb_advance_cache_t<> *cache = nullptr;
if (use_cache) if (use_cache)
{ {
retry: retry:
cache = ot_font->advance_cache.get_acquire (); cache = ot_font->advance_cache.get_acquire ();
if (unlikely (!cache)) if (unlikely (!cache))
{ {
cache = (hb_advance_cache_t *) hb_malloc (sizeof (hb_advance_cache_t)); cache = (hb_advance_cache_t<> *) hb_malloc (sizeof (hb_advance_cache_t<>));
if (unlikely (!cache)) if (unlikely (!cache))
{ {
use_cache = false; use_cache = false;