Don't use atomic ops in hb_cache_t

We don't care about linearizability, so unprotected int read/write
are enough, no need for expensive memory barriers.  It's a cache,
that's all.
This commit is contained in:
Behdad Esfahbod 2012-05-27 10:11:13 -04:00
parent 819faa0530
commit e4b6d503c5
1 changed files with 4 additions and 4 deletions

View File

@ -36,7 +36,7 @@ template <unsigned int key_bits, unsigned int value_bits, unsigned int cache_bit
struct hb_cache_t struct hb_cache_t
{ {
ASSERT_STATIC (key_bits >= cache_bits); ASSERT_STATIC (key_bits >= cache_bits);
ASSERT_STATIC (key_bits + value_bits - cache_bits < 8 * sizeof (hb_atomic_int_t)); ASSERT_STATIC (key_bits + value_bits - cache_bits < 8 * sizeof (unsigned int));
inline void clear (void) inline void clear (void)
{ {
@ -46,7 +46,7 @@ struct hb_cache_t
inline bool get (unsigned int key, unsigned int *value) inline bool get (unsigned int key, unsigned int *value)
{ {
unsigned int k = key & ((1<<cache_bits)-1); unsigned int k = key & ((1<<cache_bits)-1);
unsigned int v = hb_atomic_int_get (values[k]); unsigned int v = values[k];
if ((v >> value_bits) != (key >> cache_bits)) if ((v >> value_bits) != (key >> cache_bits))
return false; return false;
} }
@ -57,12 +57,12 @@ struct hb_cache_t
return false; /* Overflows */ return false; /* Overflows */
unsigned int k = key & ((1<<cache_bits)-1); unsigned int k = key & ((1<<cache_bits)-1);
unsigned int v = ((key>>cache_bits)<<value_bits) | value; unsigned int v = ((key>>cache_bits)<<value_bits) | value;
hb_atomic_int_set (values[k], v); values[k] = v;
return true; return true;
} }
private: private:
hb_atomic_int_t values[1<<cache_bits]; unsigned int values[1<<cache_bits];
}; };
typedef hb_cache_t<21, 16, 8> hb_cmap_cache_t; typedef hb_cache_t<21, 16, 8> hb_cmap_cache_t;