@ -287,51 +287,63 @@ TEST_F(FastLRUCacheTest, ValidateKeySize) {
}
}
TEST_F ( FastLRUCacheTest , CalcHashBitsTest ) {
TEST_F ( FastLRUCacheTest , CalcHashBitsTest ) {
size_t capacity = 1024 ;
size_t capacity ;
size_t estimated_value_size = 1 ;
size_t estimated_value_size ;
CacheMetadataChargePolicy metadata_charge_policy = kDontChargeCacheMetadata ;
double max_occupancy ;
double max_occupancy =
int hash_bits ;
CalcMaxOccupancy ( capacity , estimated_value_size , metadata_charge_policy ) ;
CacheMetadataChargePolicy metadata_charge_policy ;
int hash_bits = CalcHashBitsWrapper ( capacity , estimated_value_size ,
metadata_charge_policy ) ;
// Vary the cache capacity, fix the element charge.
EXPECT_TRUE ( TableSizeIsAppropriate ( hash_bits , max_occupancy ) ) ;
for ( int i = 0 ; i < 2048 ; i + + ) {
capacity = i ;
capacity = 1024 ;
estimated_value_size = 0 ;
metadata_charge_policy = kFullChargeCacheMetadata ;
max_occupancy = CalcMaxOccupancy ( capacity , estimated_value_size ,
metadata_charge_policy ) ;
hash_bits = CalcHashBitsWrapper ( capacity , estimated_value_size ,
metadata_charge_policy ) ;
EXPECT_TRUE ( TableSizeIsAppropriate ( hash_bits , max_occupancy ) ) ;
}
// Fix the cache capacity, vary the element charge.
for ( int i = 0 ; i < 1024 ; i + + ) {
capacity = 1024 ;
estimated_value_size = i ;
metadata_charge_policy = kFullChargeCacheMetadata ;
max_occupancy = CalcMaxOccupancy ( capacity , estimated_value_size ,
metadata_charge_policy ) ;
hash_bits = CalcHashBitsWrapper ( capacity , estimated_value_size ,
metadata_charge_policy ) ;
EXPECT_TRUE ( TableSizeIsAppropriate ( hash_bits , max_occupancy ) ) ;
}
// Zero-capacity cache, and only values have charge.
capacity = 0 ;
estimated_value_size = 1 ;
estimated_value_size = 1 ;
metadata_charge_policy = kFullChargeCacheMetadata ;
metadata_charge_policy = kDontChargeCacheMetadata ;
max_occupancy =
CalcMaxOccupancy ( capacity , estimated_value_size , metadata_charge_policy ) ;
hash_bits = CalcHashBitsWrapper ( capacity , estimated_value_size ,
hash_bits = CalcHashBitsWrapper ( capacity , estimated_value_size ,
metadata_charge_policy ) ;
metadata_charge_policy ) ;
EXPECT_TRUE ( TableSizeIsAppropriate ( hash_bits , max_occupancy ) ) ;
EXPECT_TRUE ( TableSizeIsAppropriate ( hash_bits , 0 /* max_occupancy */ ) ) ;
// No elements fit in cache.
// Zero-capacity cache, and only metadata has charg e.
capacity = 0 ;
capacity = 0 ;
estimated_value_size = 1 ;
estimated_value_size = 0 ;
metadata_charge_policy = kDontChargeCacheMetadata ;
metadata_charge_policy = kFull ChargeCacheMetadata ;
hash_bits = CalcHashBitsWrapper ( capacity , estimated_value_size ,
hash_bits = CalcHashBitsWrapper ( capacity , estimated_value_size ,
metadata_charge_policy ) ;
metadata_charge_policy ) ;
EXPECT_TRUE ( TableSizeIsAppropriate ( hash_bits , 0 /* max_occupancy */ ) ) ;
EXPECT_TRUE ( TableSizeIsAppropriate ( hash_bits , 0 /* max_occupancy */ ) ) ;
// Set the capacity just below a single handle. Because the load factor is <
// Small cache, large elements.
// 100% at least one handle will fit in the table.
capacity = 1024 ;
estimated_value_size = 1 ;
estimated_value_size = 8192 ;
size_t handle_charge = CalcEstimatedHandleChargeWrapper (
metadata_charge_policy = kFullChargeCacheMetadata ;
8192 /* estimated_value_size */ , kDontChargeCacheMetadata ) ;
capacity = handle_charge - 1 ;
// The load factor should be bounded away from 100%.
assert ( static_cast < size_t > ( capacity / fast_lru_cache : : kLoadFactor ) >
handle_charge ) ;
metadata_charge_policy = kDontChargeCacheMetadata ;
max_occupancy =
CalcMaxOccupancy ( capacity , estimated_value_size , metadata_charge_policy ) ;
hash_bits = CalcHashBitsWrapper ( capacity , estimated_value_size ,
hash_bits = CalcHashBitsWrapper ( capacity , estimated_value_size ,
metadata_charge_policy ) ;
metadata_charge_policy ) ;
EXPECT_TRUE ( TableSizeIsAppropriate ( hash_bits , max_occupancy ) ) ;
EXPECT_TRUE ( TableSizeIsAppropriate ( hash_bits , 0 /* max_occupancy */ ) ) ;
// Large capacity.
// Large capacity.
capacity = 31924172 ;
capacity = 31924172 ;
estimated_value_size = 321 ;
estimated_value_size = 8192 ;
metadata_charge_policy = kFullChargeCacheMetadata ;
metadata_charge_policy = kFullChargeCacheMetadata ;
max_occupancy =
max_occupancy =
CalcMaxOccupancy ( capacity , estimated_value_size , metadata_charge_policy ) ;
CalcMaxOccupancy ( capacity , estimated_value_size , metadata_charge_policy ) ;