Skip to content

Commit ed0aff0

Browse files
IshiiharaMrCroxx
andauthored
feat: add S3Fifo eviction for memory (#303)
* Add S3Fifo eviction for memory * fix: refine s3fifo, fix some bugs Signed-off-by: MrCroxx <mrcroxx@outlook.com> * fix: fix license Signed-off-by: MrCroxx <mrcroxx@outlook.com> * refactor: expose s3fifo, fix hakari, add s3fifo fuzzy test Signed-off-by: MrCroxx <mrcroxx@outlook.com> * bench: add s3fifo to hit ratio bench Signed-off-by: MrCroxx <mrcroxx@outlook.com> * test: add s3fifo uts Signed-off-by: MrCroxx <mrcroxx@outlook.com> --------- Signed-off-by: MrCroxx <mrcroxx@outlook.com> Co-authored-by: MrCroxx <mrcroxx@outlook.com>
1 parent 19facb2 commit ed0aff0

File tree

10 files changed

+567
-32
lines changed

10 files changed

+567
-32
lines changed

foyer-intrusive/src/core/adapter.rs

+6
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ pub unsafe trait PriorityAdapter: Adapter {
117117
/// # Examples
118118
///
119119
/// ```
120+
/// #![feature(offset_of)]
121+
///
120122
/// use foyer_intrusive::{intrusive_adapter, key_adapter};
121123
/// use foyer_intrusive::core::adapter::{Adapter, KeyAdapter, Link};
122124
/// use foyer_intrusive::core::pointer::Pointer;
@@ -213,6 +215,8 @@ macro_rules! intrusive_adapter {
213215
/// # Examples
214216
///
215217
/// ```
218+
/// #![feature(offset_of)]
219+
///
216220
/// use foyer_intrusive::{intrusive_adapter, key_adapter};
217221
/// use foyer_intrusive::core::adapter::{Adapter, KeyAdapter, Link};
218222
/// use foyer_intrusive::core::pointer::Pointer;
@@ -282,6 +286,8 @@ macro_rules! key_adapter {
282286
/// # Examples
283287
///
284288
/// ```
289+
/// #![feature(offset_of)]
290+
///
285291
/// use foyer_intrusive::{intrusive_adapter, priority_adapter};
286292
/// use foyer_intrusive::core::adapter::{Adapter, PriorityAdapter, Link};
287293
/// use foyer_intrusive::core::pointer::Pointer;

foyer-intrusive/src/eviction/lfu.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ where
468468
link
469469
}
470470
(Some(link_main), Some(link_tiny)) => {
471-
// Eviction from tiny or main depending on whether the tiny handle woould be
471+
// Eviction from tiny or main depending on whether the tiny handle would be
472472
// admitted to main cachce. If it would be, evict from main cache, otherwise
473473
// from tiny cache.
474474
if self.lfu.admit_to_main(link_main.raw(), link_tiny.raw()) {

foyer-intrusive/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ pub use memoffset::offset_of;
2727
/// # Examples
2828
///
2929
/// ```
30+
/// #![feature(offset_of)]
31+
///
3032
/// use foyer_intrusive::container_of;
3133
///
3234
/// struct S { x: u32, y: u32 };

foyer-memory/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ itertools = "0.12"
2525
libc = "0.2"
2626
parking_lot = "0.12"
2727
tokio = { workspace = true }
28+
2829
[dev-dependencies]
2930
bytesize = "1"
3031
clap = { version = "4", features = ["derive"] }

foyer-memory/benches/bench_hit_ratio.rs

+53-28
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ use std::sync::Arc;
1616

1717
use ahash::RandomState;
1818
use foyer_memory::{
19-
Cache, DefaultCacheEventListener, FifoCacheConfig, FifoConfig, LfuCacheConfig, LfuConfig, LruCacheConfig, LruConfig,
19+
Cache, DefaultCacheEventListener, FifoCacheConfig, FifoConfig, LfuCacheConfig, LfuConfig, LruCacheConfig,
20+
LruConfig, S3FifoCacheConfig, S3FifoConfig,
2021
};
2122
use rand::{distributions::Distribution, thread_rng};
2223

@@ -32,32 +33,32 @@ const OBJECT_POOL_CAPACITY: usize = 16;
3233
inspired by pingora/tinyufo/benches/bench_hit_ratio.rs
3334
cargo bench --bench bench_hit_ratio
3435
35-
zif_exp, cache_size fifo lru lfu moka
36-
0.90, 0.005 16.26% 19.22% 32.38% 33.46%
37-
0.90, 0.01 22.55% 26.21% 38.55% 37.93%
38-
0.90, 0.05 41.10% 45.60% 55.45% 55.26%
39-
0.90, 0.1 51.11% 55.72% 63.83% 64.21%
40-
0.90, 0.25 66.81% 71.17% 76.21% 77.14%
41-
1.00, 0.005 26.64% 31.08% 44.14% 45.61%
42-
1.00, 0.01 34.37% 39.13% 50.60% 50.67%
43-
1.00, 0.05 54.06% 58.79% 66.79% 66.99%
44-
1.00, 0.1 63.12% 67.58% 73.92% 74.38%
45-
1.00, 0.25 76.14% 79.92% 83.61% 84.33%
46-
1.05, 0.005 32.63% 37.68% 50.24% 51.77%
47-
1.05, 0.01 40.95% 46.09% 56.75% 57.15%
48-
1.05, 0.05 60.47% 65.06% 72.07% 72.36%
49-
1.05, 0.1 68.96% 73.15% 78.52% 78.96%
50-
1.05, 0.25 80.42% 83.76% 86.79% 87.42%
51-
1.10, 0.005 39.02% 44.52% 56.29% 57.90%
52-
1.10, 0.01 47.66% 52.99% 62.64% 63.23%
53-
1.10, 0.05 66.60% 70.95% 76.94% 77.25%
54-
1.10, 0.1 74.26% 78.09% 82.56% 82.93%
55-
1.10, 0.25 84.15% 87.06% 89.54% 90.05%
56-
1.50, 0.005 81.19% 85.28% 88.91% 89.94%
57-
1.50, 0.01 86.91% 89.87% 92.24% 92.78%
58-
1.50, 0.05 94.75% 96.04% 96.95% 97.07%
59-
1.50, 0.1 96.65% 97.51% 98.06% 98.15%
60-
1.50, 0.25 98.35% 98.81% 99.04% 99.09%
36+
zif_exp, cache_size fifo lru lfu s3fifo moka
37+
0.90, 0.005 16.24% 19.22% 32.37% 32.39% 33.50%
38+
0.90, 0.01 22.55% 26.20% 38.54% 39.20% 37.92%
39+
0.90, 0.05 41.05% 45.56% 55.37% 56.63% 55.25%
40+
0.90, 0.1 51.06% 55.68% 63.82% 65.06% 64.20%
41+
0.90, 0.25 66.81% 71.17% 76.21% 77.26% 77.12%
42+
1.00, 0.005 26.62% 31.10% 44.16% 44.15% 45.62%
43+
1.00, 0.01 34.38% 39.17% 50.63% 51.29% 50.72%
44+
1.00, 0.05 54.04% 58.76% 66.79% 67.85% 66.89%
45+
1.00, 0.1 63.15% 67.60% 73.93% 74.92% 74.38%
46+
1.00, 0.25 76.18% 79.95% 83.63% 84.39% 84.38%
47+
1.05, 0.005 32.67% 37.71% 50.26% 50.21% 51.85%
48+
1.05, 0.01 40.97% 46.10% 56.74% 57.40% 57.09%
49+
1.05, 0.05 60.44% 65.03% 72.04% 73.02% 72.28%
50+
1.05, 0.1 68.93% 73.12% 78.49% 79.37% 79.00%
51+
1.05, 0.25 80.38% 83.73% 86.78% 87.42% 87.41%
52+
1.10, 0.005 39.02% 44.50% 56.26% 56.20% 57.90%
53+
1.10, 0.01 47.60% 52.93% 62.61% 63.24% 63.05%
54+
1.10, 0.05 66.59% 70.95% 76.92% 77.76% 77.27%
55+
1.10, 0.1 74.24% 78.07% 82.54% 83.28% 83.00%
56+
1.10, 0.25 84.18% 87.10% 89.57% 90.06% 90.08%
57+
1.50, 0.005 81.17% 85.27% 88.90% 89.10% 89.89%
58+
1.50, 0.01 86.91% 89.87% 92.25% 92.56% 92.79%
59+
1.50, 0.05 94.77% 96.04% 96.96% 97.10% 97.07%
60+
1.50, 0.1 96.65% 97.50% 98.06% 98.14% 98.15%
61+
1.50, 0.25 98.36% 98.81% 99.04% 99.06% 99.09%
6162
*/
6263
fn cache_hit(cache: Arc<Cache<CacheKey, CacheValue>>, keys: Arc<Vec<CacheKey>>) -> f64 {
6364
let mut hit = 0;
@@ -131,6 +132,21 @@ fn new_lfu_cache(capacity: usize) -> Arc<Cache<CacheKey, CacheValue>> {
131132
Arc::new(Cache::lfu(config))
132133
}
133134

135+
fn new_s3fifo_cache(capacity: usize) -> Arc<Cache<CacheKey, CacheValue>> {
136+
let config = S3FifoCacheConfig {
137+
capacity,
138+
shards: SHARDS,
139+
eviction_config: S3FifoConfig {
140+
small_queue_capacity_ratio: 0.1,
141+
},
142+
object_pool_capacity: OBJECT_POOL_CAPACITY,
143+
hash_builder: RandomState::default(),
144+
event_listener: DefaultCacheEventListener::default(),
145+
};
146+
147+
Arc::new(Cache::s3fifo(config))
148+
}
149+
134150
fn bench_one(zif_exp: f64, cache_size_percent: f64) {
135151
print!("{zif_exp:.2}, {cache_size_percent:4}\t\t\t");
136152
let mut rng = thread_rng();
@@ -141,6 +157,7 @@ fn bench_one(zif_exp: f64, cache_size_percent: f64) {
141157
let fifo_cache = new_fifo_cache(cache_size);
142158
let lru_cache = new_lru_cache(cache_size);
143159
let lfu_cache = new_lfu_cache(cache_size);
160+
let s3fifo_cache = new_s3fifo_cache(cache_size);
144161
let moka_cache = moka::sync::Cache::new(cache_size as u64);
145162

146163
let mut keys = Vec::with_capacity(ITERATIONS);
@@ -170,6 +187,12 @@ fn bench_one(zif_exp: f64, cache_size_percent: f64) {
170187
move || cache_hit(cache, keys)
171188
});
172189

190+
let s3fifo_cache_hit_handle = std::thread::spawn({
191+
let cache = s3fifo_cache.clone();
192+
let keys = keys.clone();
193+
move || cache_hit(cache, keys)
194+
});
195+
173196
let moka_cache_hit_handle = std::thread::spawn({
174197
let cache = moka_cache.clone();
175198
let keys = keys.clone();
@@ -179,16 +202,18 @@ fn bench_one(zif_exp: f64, cache_size_percent: f64) {
179202
let fifo_hit_ratio = fifo_cache_hit_handle.join().unwrap();
180203
let lru_hit_ratio = lru_cache_hit_handle.join().unwrap();
181204
let lfu_hit_ratio = lfu_cache_hit_handle.join().unwrap();
205+
let s3fifo_hit_ratio = s3fifo_cache_hit_handle.join().unwrap();
182206
let moka_hit_ratio = moka_cache_hit_handle.join().unwrap();
183207

184208
print!("{:.2}%\t\t", fifo_hit_ratio * 100.0);
185209
print!("{:.2}%\t\t", lru_hit_ratio * 100.0);
186210
print!("{:.2}%\t\t", lfu_hit_ratio * 100.0);
211+
print!("{:.2}%\t\t", s3fifo_hit_ratio * 100.0);
187212
println!("{:.2}%", moka_hit_ratio * 100.0);
188213
}
189214

190215
fn bench_zipf_hit() {
191-
println!("zif_exp, cache_size\t\tfifo\t\tlru\t\tlfu\t\tmoka");
216+
println!("zif_exp, cache_size\t\tfifo\t\tlru\t\tlfu\t\ts3fifo\t\tmoka");
192217
for zif_exp in [0.9, 1.0, 1.05, 1.1, 1.5] {
193218
for cache_capacity in [0.005, 0.01, 0.05, 0.1, 0.25] {
194219
bench_one(zif_exp, cache_capacity);

0 commit comments

Comments
 (0)