priority-lfu 0.3.0

A high-performance, concurrent, in-memory cache with W-TinyLFU eviction policy and weight-based prioritization.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
use std::hint::black_box;
use std::sync::Arc;

use criterion::{BenchmarkId, Criterion, Throughput, criterion_group, criterion_main};
use priority_lfu::{Cache, CacheKey, CacheKeyLookup, CachePolicy, DeepSizeOf};
use quick_cache::sync::Cache as QuickCache;

#[derive(Hash, Eq, PartialEq, Clone, Debug)]
struct BenchKey(u64);

impl CacheKey for BenchKey {
	type Value = BenchValue;

	fn policy(&self) -> CachePolicy {
		CachePolicy::Standard
	}
}

#[derive(Clone, Debug, PartialEq, DeepSizeOf)]
struct BenchValue {
	data: Vec<u8>,
}

fn bench_insert(c: &mut Criterion) {
	let mut group = c.benchmark_group("insert");

	for size in [100, 1000, 10000] {
		group.throughput(Throughput::Elements(size));
		group.bench_with_input(BenchmarkId::from_parameter(size), &size, |b, &size| {
			b.iter(|| {
				let cache = Cache::new(1024 * 1024);
				for i in 0..size {
					let key = BenchKey(i);
					let value = BenchValue {
						data: vec![0u8; 64],
					};
					cache.insert(black_box(key), black_box(value));
				}
			});
		});
	}

	group.finish();
}

fn bench_get_hit(c: &mut Criterion) {
	let cache = Arc::new(Cache::new(1024 * 1024));

	// Pre-populate cache
	for i in 0..1000 {
		cache.insert(
			BenchKey(i),
			BenchValue {
				data: vec![0u8; 64],
			},
		);
	}

	c.bench_function("get_hit", |b| {
		b.iter(|| {
			for i in 0..1000 {
				let key = BenchKey(black_box(i));
				black_box(cache.get_clone(&key));
			}
		});
	});
}

fn bench_get_vs_get_clone(c: &mut Criterion) {
	let cache = Arc::new(Cache::new(1024 * 1024));

	// Pre-populate
	for i in 0..100 {
		cache.insert(
			BenchKey(i),
			BenchValue {
				data: vec![0u8; 64],
			},
		);
	}

	let mut group = c.benchmark_group("get_methods");

	group.bench_function("get", |b| {
		b.iter(|| {
			for i in 0..100 {
				black_box(cache.get(&BenchKey(black_box(i))));
			}
		});
	});

	group.bench_function("get_clone", |b| {
		b.iter(|| {
			for i in 0..100 {
				black_box(cache.get_clone(&BenchKey(black_box(i))));
			}
		});
	});

	group.finish();
}

fn bench_mixed_workload(c: &mut Criterion) {
	let cache = Arc::new(Cache::new(1024 * 1024));

	// Pre-populate
	for i in 0..500 {
		cache.insert(
			BenchKey(i),
			BenchValue {
				data: vec![0u8; 64],
			},
		);
	}

	c.bench_function("mixed_80_20", |b| {
		b.iter(|| {
			for i in 0..100 {
				if i % 5 == 0 {
					// 20% writes
					cache.insert(
						BenchKey(black_box(i)),
						BenchValue {
							data: vec![0u8; 64],
						},
					);
				} else {
					// 80% reads
					black_box(cache.get_clone(&BenchKey(black_box(i % 500))));
				}
			}
		});
	});
}

fn bench_concurrent_reads(c: &mut Criterion) {
	use std::thread;

	let cache = Arc::new(Cache::new(1024 * 1024));

	// Pre-populate
	for i in 0..1000 {
		cache.insert(
			BenchKey(i),
			BenchValue {
				data: vec![0u8; 64],
			},
		);
	}

	c.bench_function("concurrent_reads_4_threads", |b| {
		b.iter(|| {
			let mut handles = vec![];

			for _ in 0..4 {
				let cache = cache.clone();
				handles.push(thread::spawn(move || {
					for i in 0..250 {
						black_box(cache.get_clone(&BenchKey(black_box(i))));
					}
				}));
			}

			for handle in handles {
				handle.join().expect("thread should not panic");
			}
		});
	});
}

fn bench_eviction_pressure(c: &mut Criterion) {
	c.bench_function("eviction_pressure", |b| {
		b.iter(|| {
			let cache = Cache::new(10240); // Small cache to trigger eviction

			// Insert many items, forcing eviction
			for i in 0..1000 {
				cache.insert(
					BenchKey(black_box(i)),
					BenchValue {
						data: vec![0u8; 100],
					},
				);
			}
		});
	});
}

fn bench_hit_rate_zipf(c: &mut Criterion) {
	let cache = Arc::new(Cache::new(1024 * 1024));

	// Simulate Zipf distribution: some keys are accessed much more frequently
	let zipf_keys: Vec<u64> = (0..100)
		.flat_map(|i| {
			let freq = 100 / (i + 1); // First key appears 100 times, second 50 times, etc.
			vec![i; freq as usize]
		})
		.collect();

	c.bench_function("zipf_distribution", |b| {
		b.iter(|| {
			for &key_id in &zipf_keys {
				let key = BenchKey(black_box(key_id));
				match cache.get_clone(&key) {
					Some(val) => {
						black_box(val);
					}
					None => {
						cache.insert(
							key,
							BenchValue {
								data: vec![0u8; 64],
							},
						);
					}
				}
			}
		});
	});
}

// ============================================================================
// Comparison Benchmarks: priority-lfu vs quick_cache
// ============================================================================

fn bench_comparison_insert(c: &mut Criterion) {
	let mut group = c.benchmark_group("comparison/insert");

	// Use same item capacity for fair comparison
	const CACHE_CAPACITY: usize = 20000;

	for size in [100, 1000, 10000] {
		group.throughput(Throughput::Elements(size));

		group.bench_with_input(BenchmarkId::new("priority_lfu", size), &size, |b, &size| {
			b.iter(|| {
				// Estimate ~100 bytes per entry (64 byte value + key + Arc overhead)
				let cache = Cache::new(CACHE_CAPACITY * 100);
				for i in 0..size {
					let key = BenchKey(i);
					let value = BenchValue {
						data: vec![0u8; 64],
					};
					cache.insert(black_box(key), black_box(value));
				}
			});
		});

		group.bench_with_input(BenchmarkId::new("quick_cache", size), &size, |b, &size| {
			b.iter(|| {
				let cache = QuickCache::new(CACHE_CAPACITY);
				for i in 0..size {
					let key = i;
					let value = vec![0u8; 64];
					cache.insert(black_box(key), black_box(value));
				}
			});
		});
	}

	group.finish();
}

fn bench_comparison_get_hit(c: &mut Criterion) {
	let mut group = c.benchmark_group("comparison/get_hit");

	// Use same item capacity for fair comparison
	const NUM_ITEMS: u64 = 1000;
	const CACHE_CAPACITY: usize = 2000;

	// priority-lfu setup (~100 bytes per entry)
	let lfu_cache = Arc::new(Cache::new(CACHE_CAPACITY * 100));
	for i in 0..NUM_ITEMS {
		lfu_cache.insert(
			BenchKey(i),
			BenchValue {
				data: vec![0u8; 64],
			},
		);
	}

	// quick_cache setup
	let quick_cache = Arc::new(QuickCache::new(CACHE_CAPACITY));
	for i in 0..NUM_ITEMS {
		quick_cache.insert(i, vec![0u8; 64]);
	}

	group.bench_function("priority_lfu", |b| {
		b.iter(|| {
			for i in 0..NUM_ITEMS {
				let key = BenchKey(black_box(i));
				black_box(lfu_cache.get(&key));
			}
		});
	});

	group.bench_function("quick_cache", |b| {
		b.iter(|| {
			for i in 0..NUM_ITEMS {
				black_box(quick_cache.get(&black_box(i)));
			}
		});
	});

	group.finish();
}

fn bench_comparison_mixed_workload(c: &mut Criterion) {
	let mut group = c.benchmark_group("comparison/mixed_80_20");

	// Use same item capacity for fair comparison
	const NUM_ITEMS: u64 = 500;
	const CACHE_CAPACITY: usize = 1000;

	// priority-lfu setup (~100 bytes per entry)
	let lfu_cache = Arc::new(Cache::new(CACHE_CAPACITY * 100));
	for i in 0..NUM_ITEMS {
		lfu_cache.insert(
			BenchKey(i),
			BenchValue {
				data: vec![0u8; 64],
			},
		);
	}

	// quick_cache setup
	let quick_cache = Arc::new(QuickCache::new(CACHE_CAPACITY));
	for i in 0..NUM_ITEMS {
		quick_cache.insert(i, vec![0u8; 64]);
	}

	group.bench_function("priority_lfu", |b| {
		b.iter(|| {
			for i in 0..100u64 {
				if i % 5 == 0 {
					// 20% writes
					lfu_cache.insert(
						BenchKey(black_box(i)),
						BenchValue {
							data: vec![0u8; 64],
						},
					);
				} else {
					// 80% reads
					black_box(lfu_cache.get(&BenchKey(black_box(i % NUM_ITEMS))));
				}
			}
		});
	});

	group.bench_function("quick_cache", |b| {
		b.iter(|| {
			for i in 0..100u64 {
				if i % 5 == 0 {
					// 20% writes
					quick_cache.insert(black_box(i), vec![0u8; 64]);
				} else {
					// 80% reads
					black_box(quick_cache.get(&black_box(i % NUM_ITEMS)));
				}
			}
		});
	});

	group.finish();
}

fn bench_comparison_concurrent_reads(c: &mut Criterion) {
	use std::thread;

	let mut group = c.benchmark_group("comparison/concurrent_reads_4_threads");

	// Use same item capacity for fair comparison
	const NUM_ITEMS: u64 = 1000;
	const CACHE_CAPACITY: usize = 2000;

	// priority-lfu setup (~100 bytes per entry)
	let lfu_cache = Arc::new(Cache::new(CACHE_CAPACITY * 100));
	for i in 0..NUM_ITEMS {
		lfu_cache.insert(
			BenchKey(i),
			BenchValue {
				data: vec![0u8; 64],
			},
		);
	}

	// quick_cache setup
	let quick_cache = Arc::new(QuickCache::new(CACHE_CAPACITY));
	for i in 0..NUM_ITEMS {
		quick_cache.insert(i, vec![0u8; 64]);
	}

	group.bench_function("priority_lfu", |b| {
		b.iter(|| {
			let mut handles = vec![];

			for _ in 0..4 {
				let cache = lfu_cache.clone();
				handles.push(thread::spawn(move || {
					for i in 0..250u64 {
						black_box(cache.get(&BenchKey(black_box(i))));
					}
				}));
			}

			for handle in handles {
				handle.join().expect("thread should not panic");
			}
		});
	});

	group.bench_function("quick_cache", |b| {
		b.iter(|| {
			let mut handles = vec![];

			for _ in 0..4 {
				let cache = quick_cache.clone();
				handles.push(thread::spawn(move || {
					for i in 0..250u64 {
						black_box(cache.get(&black_box(i)));
					}
				}));
			}

			for handle in handles {
				handle.join().expect("thread should not panic");
			}
		});
	});

	group.finish();
}

fn bench_comparison_eviction_pressure(c: &mut Criterion) {
	let mut group = c.benchmark_group("comparison/eviction_pressure");

	// Use same effective item capacity for fair comparison
	// Both caches should hold ~100 items before eviction starts
	const CACHE_CAPACITY: usize = 100;

	group.bench_function("priority_lfu", |b| {
		b.iter(|| {
			// ~150 bytes per entry (100 byte value + key + Arc overhead)
			let cache = Cache::new(CACHE_CAPACITY * 150);

			// Insert many items, forcing eviction
			for i in 0..1000u64 {
				cache.insert(
					BenchKey(black_box(i)),
					BenchValue {
						data: vec![0u8; 100],
					},
				);
			}
		});
	});

	group.bench_function("quick_cache", |b| {
		b.iter(|| {
			let cache = QuickCache::new(CACHE_CAPACITY);

			// Insert many items, forcing eviction
			for i in 0..1000u64 {
				cache.insert(black_box(i), vec![0u8; 100]);
			}
		});
	});

	group.finish();
}

fn bench_comparison_zipf_distribution(c: &mut Criterion) {
	let mut group = c.benchmark_group("comparison/zipf_distribution");

	// Use same item capacity for fair comparison
	const CACHE_CAPACITY: usize = 200;

	// Simulate Zipf distribution: some keys are accessed much more frequently
	let zipf_keys: Vec<u64> = (0..100)
		.flat_map(|i| {
			let freq = 100 / (i + 1); // First key appears 100 times, second 50 times, etc.
			vec![i; freq as usize]
		})
		.collect();

	// priority-lfu setup (~100 bytes per entry)
	let lfu_cache = Arc::new(Cache::new(CACHE_CAPACITY * 100));

	// quick_cache setup
	let quick_cache = Arc::new(QuickCache::new(CACHE_CAPACITY));

	group.bench_function("priority_lfu", |b| {
		b.iter(|| {
			for &key_id in &zipf_keys {
				let key = BenchKey(black_box(key_id));
				match lfu_cache.get(&key) {
					Some(val) => {
						black_box(val);
					}
					None => {
						lfu_cache.insert(
							key,
							BenchValue {
								data: vec![0u8; 64],
							},
						);
					}
				}
			}
		});
	});

	group.bench_function("quick_cache", |b| {
		b.iter(|| {
			for &key_id in &zipf_keys {
				let key = black_box(key_id);
				match quick_cache.get(&key) {
					Some(val) => {
						black_box(val);
					}
					None => {
						quick_cache.insert(key, vec![0u8; 64]);
					}
				}
			}
		});
	});

	group.finish();
}

// ============================================================================
// Borrowed Key Lookup Benchmarks: Demonstrates zero-allocation lookups
// ============================================================================

// String-based key for realistic benchmarking
#[derive(Hash, Eq, PartialEq, Clone, Debug)]
struct StringKey(String, String);

impl CacheKey for StringKey {
	type Value = BenchValue;

	fn policy(&self) -> CachePolicy {
		CachePolicy::Standard
	}
}

// Borrowed lookup type
struct StringKeyRef<'a>(&'a str, &'a str);

impl std::hash::Hash for StringKeyRef<'_> {
	fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
		self.0.hash(state);
		self.1.hash(state);
	}
}

impl CacheKeyLookup<StringKey> for StringKeyRef<'_> {
	fn eq_key(&self, key: &StringKey) -> bool {
		self.0 == key.0 && self.1 == key.1
	}

	fn to_owned_key(self) -> StringKey {
		StringKey(self.0.to_owned(), self.1.to_owned())
	}
}

fn bench_owned_vs_borrowed_get(c: &mut Criterion) {
	let mut group = c.benchmark_group("borrowed_key/get");

	let cache = Arc::new(Cache::new(1024 * 1024));

	// Pre-populate cache with string keys
	for i in 0..1000 {
		cache.insert(
			StringKey(format!("namespace_{}", i), format!("database_{}", i)),
			BenchValue {
				data: vec![0u8; 64],
			},
		);
	}

	// Benchmark: get with owned key (requires allocation)
	group.bench_function("owned_key", |b| {
		b.iter(|| {
			for i in 0..1000 {
				let ns = format!("namespace_{}", black_box(i));
				let db = format!("database_{}", black_box(i));
				let key = StringKey(ns, db);
				black_box(cache.get(&key));
			}
		});
	});

	// Benchmark: get_by with borrowed key (zero allocation)
	group.bench_function("borrowed_key", |b| {
		b.iter(|| {
			for i in 0..1000 {
				let ns = format!("namespace_{}", black_box(i));
				let db = format!("database_{}", black_box(i));
				let key_ref = StringKeyRef(&ns, &db);
				black_box(cache.get_by::<StringKey, _>(&key_ref));
			}
		});
	});

	group.finish();
}

fn bench_owned_vs_borrowed_get_clone(c: &mut Criterion) {
	let mut group = c.benchmark_group("borrowed_key/get_clone");

	let cache = Arc::new(Cache::new(1024 * 1024));

	// Pre-populate cache with string keys
	for i in 0..1000 {
		cache.insert(
			StringKey(format!("namespace_{}", i), format!("database_{}", i)),
			BenchValue {
				data: vec![0u8; 64],
			},
		);
	}

	// Benchmark: get_clone with owned key (requires allocation)
	group.bench_function("owned_key", |b| {
		b.iter(|| {
			for i in 0..1000 {
				let ns = format!("namespace_{}", black_box(i));
				let db = format!("database_{}", black_box(i));
				let key = StringKey(ns, db);
				black_box(cache.get_clone(&key));
			}
		});
	});

	// Benchmark: get_clone_by with borrowed key (zero allocation)
	group.bench_function("borrowed_key", |b| {
		b.iter(|| {
			for i in 0..1000 {
				let ns = format!("namespace_{}", black_box(i));
				let db = format!("database_{}", black_box(i));
				let key_ref = StringKeyRef(&ns, &db);
				black_box(cache.get_clone_by::<StringKey, _>(&key_ref));
			}
		});
	});

	group.finish();
}

fn bench_owned_vs_borrowed_contains(c: &mut Criterion) {
	let mut group = c.benchmark_group("borrowed_key/contains");

	let cache = Arc::new(Cache::new(1024 * 1024));

	// Pre-populate cache with string keys
	for i in 0..1000 {
		cache.insert(
			StringKey(format!("namespace_{}", i), format!("database_{}", i)),
			BenchValue {
				data: vec![0u8; 64],
			},
		);
	}

	// Benchmark: contains with owned key (requires allocation)
	group.bench_function("owned_key", |b| {
		b.iter(|| {
			for i in 0..1000 {
				let ns = format!("namespace_{}", black_box(i));
				let db = format!("database_{}", black_box(i));
				let key = StringKey(ns, db);
				black_box(cache.contains(&key));
			}
		});
	});

	// Benchmark: contains_by with borrowed key (zero allocation)
	group.bench_function("borrowed_key", |b| {
		b.iter(|| {
			for i in 0..1000 {
				let ns = format!("namespace_{}", black_box(i));
				let db = format!("database_{}", black_box(i));
				let key_ref = StringKeyRef(&ns, &db);
				black_box(cache.contains_by::<StringKey, _>(&key_ref));
			}
		});
	});

	group.finish();
}

fn bench_borrowed_key_realistic_workload(c: &mut Criterion) {
	let mut group = c.benchmark_group("borrowed_key/realistic_workload");

	let cache = Arc::new(Cache::new(1024 * 1024));

	// Pre-populate with realistic database cache keys
	for i in 0..500 {
		cache.insert(
			StringKey(format!("ns_{}", i % 10), format!("db_{}", i)),
			BenchValue {
				data: vec![0u8; 128],
			},
		);
	}

	// Benchmark: Owned key approach (allocates on every lookup)
	group.bench_function("owned_key", |b| {
		b.iter(|| {
			// Simulate 100 cache operations
			for i in 0..100 {
				let ns = format!("ns_{}", black_box(i % 10));
				let db = format!("db_{}", black_box(i % 500));

				if i % 5 == 0 {
					// 20% writes
					cache.insert(
						StringKey(ns.clone(), db.clone()),
						BenchValue {
							data: vec![0u8; 128],
						},
					);
				} else {
					// 80% reads - requires cloning strings even just to check
					let key = StringKey(ns, db);
					if let Some(val) = cache.get_clone(&key) {
						black_box(val);
					}
				}
			}
		});
	});

	// Benchmark: Borrowed key approach (zero allocation for reads)
	group.bench_function("borrowed_key", |b| {
		b.iter(|| {
			// Simulate 100 cache operations
			for i in 0..100 {
				let ns = format!("ns_{}", black_box(i % 10));
				let db = format!("db_{}", black_box(i % 500));

				if i % 5 == 0 {
					// 20% writes
					cache.insert(
						StringKey(ns.clone(), db.clone()),
						BenchValue {
							data: vec![0u8; 128],
						},
					);
				} else {
					// 80% reads - zero allocation lookup
					let key_ref = StringKeyRef(&ns, &db);
					if let Some(val) = cache.get_clone_by::<StringKey, _>(&key_ref) {
						black_box(val);
					}
				}
			}
		});
	});

	group.finish();
}

fn bench_borrowed_key_different_sizes(c: &mut Criterion) {
	let mut group = c.benchmark_group("borrowed_key/key_sizes");

	for key_size in [10, 50, 100, 200] {
		let cache = Arc::new(Cache::new(1024 * 1024));

		// Create keys of different sizes
		let large_prefix = "x".repeat(key_size);

		// Pre-populate
		for i in 0..100 {
			cache.insert(
				StringKey(format!("{}{}", large_prefix, i), format!("db_{}", i)),
				BenchValue {
					data: vec![0u8; 64],
				},
			);
		}

		group.bench_with_input(BenchmarkId::new("owned_key", key_size), &key_size, |b, &size| {
			let prefix = "x".repeat(size);
			b.iter(|| {
				for i in 0..100 {
					let key = StringKey(
						format!("{}{}", prefix, black_box(i)),
						format!("db_{}", black_box(i)),
					);
					black_box(cache.get(&key));
				}
			});
		});

		group.bench_with_input(
			BenchmarkId::new("borrowed_key", key_size),
			&key_size,
			|b, &size| {
				let prefix = "x".repeat(size);
				b.iter(|| {
					for i in 0..100 {
						let ns = format!("{}{}", prefix, black_box(i));
						let db = format!("db_{}", black_box(i));
						let key_ref = StringKeyRef(&ns, &db);
						black_box(cache.get_by::<StringKey, _>(&key_ref));
					}
				});
			},
		);
	}

	group.finish();
}

criterion_group!(
	benches,
	bench_insert,
	bench_get_hit,
	bench_get_vs_get_clone,
	bench_mixed_workload,
	bench_concurrent_reads,
	bench_eviction_pressure,
	bench_hit_rate_zipf,
	// Comparison benchmarks
	bench_comparison_insert,
	bench_comparison_get_hit,
	bench_comparison_mixed_workload,
	bench_comparison_concurrent_reads,
	bench_comparison_eviction_pressure,
	bench_comparison_zipf_distribution,
	// Borrowed key benchmarks
	bench_owned_vs_borrowed_get,
	bench_owned_vs_borrowed_get_clone,
	bench_owned_vs_borrowed_contains,
	bench_borrowed_key_realistic_workload,
	bench_borrowed_key_different_sizes
);

criterion_main!(benches);