Skip to content

Commit 47c61e7

Browse files
authored
Add policy feature flags (#15)
* Add eviction policy feature flags for modular builds - Introduced feature flags for various eviction policies in `Cargo.toml`, allowing users to enable only the necessary policies for smaller builds. - Updated documentation to reflect the new feature flags and their descriptions, enhancing clarity on usage and configuration. - Modified `bench-support/Cargo.toml` to utilize the `policy-all` feature for comprehensive benchmarking. - Ensured that the new feature flags are integrated into the cache builder and prelude for seamless access to different policies. * Update default feature flags in Cargo.toml for eviction policies - Changed the default feature flags to include specific eviction policies: `policy-s3-fifo`, `policy_lru`, `policy-fast-lru`, `policy_lru_k`, and `policy-clock`. - This update allows for more targeted usage of eviction strategies, enhancing modularity and performance based on expected workload patterns. * Fix typo in default feature flags in Cargo.toml - Corrected the naming convention of the `policy_lru` feature flag to `policy-lru` for consistency with other feature flags. - This change enhances clarity and maintains uniformity in the feature flag naming scheme. * Enhance documentation for eviction policy feature flags - Updated README and various documentation files to clarify that each eviction policy is gated behind a feature flag, improving modularity for builds. - Added feature flag details for each policy in the documentation, ensuring users can easily identify and enable the necessary policies for their use cases. - Included examples for minimal builds and emphasized the benefits of targeted feature usage for performance optimization.
1 parent 27f3c65 commit 47c61e7

29 files changed

Lines changed: 415 additions & 38 deletions

Cargo.toml

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,49 @@ doc = true
3333
crate-type = ["lib"]
3434

3535
[features]
36-
default = []
36+
default = ["policy-s3-fifo", "policy-lru", "policy-fast-lru", "policy-lru-k", "policy-clock"]
3737
metrics = []
3838
concurrency = ["parking_lot"]
3939

40+
# Eviction policy feature flags. Enable only the policies you need for smaller builds.
41+
# Use `default-features = false` and select specific policies, or use `policy-all` for every policy.
42+
policy-all = [
43+
"policy-fifo",
44+
"policy-lru",
45+
"policy-fast-lru",
46+
"policy-lru-k",
47+
"policy-lfu",
48+
"policy-heap-lfu",
49+
"policy-two-q",
50+
"policy-s3-fifo",
51+
"policy-arc",
52+
"policy-lifo",
53+
"policy-mfu",
54+
"policy-mru",
55+
"policy-random",
56+
"policy-slru",
57+
"policy-clock",
58+
"policy-clock-pro",
59+
"policy-nru",
60+
]
61+
policy-fifo = []
62+
policy-lru = []
63+
policy-fast-lru = []
64+
policy-lru-k = []
65+
policy-lfu = []
66+
policy-heap-lfu = []
67+
policy-two-q = []
68+
policy-s3-fifo = []
69+
policy-arc = []
70+
policy-lifo = []
71+
policy-mfu = []
72+
policy-mru = []
73+
policy-random = []
74+
policy-slru = []
75+
policy-clock = []
76+
policy-clock-pro = []
77+
policy-nru = []
78+
4079
[dependencies]
4180
parking_lot = { version = "0.12", optional = true }
4281
rustc-hash = "2.1"

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,44 @@ cachekit = { git = "https://github.com/OxidizeLabs/cachekit" }
3333

3434
## Feature Flags
3535

36+
### General
37+
3638
| Feature | Enables |
3739
|---------|---------|
3840
| `metrics` | Hit/miss metrics and snapshots |
3941
| `concurrency` | Concurrent wrappers (requires `parking_lot`) |
4042

43+
### Per-Policy (Eviction Policies)
44+
45+
Each eviction policy is gated behind a feature flag. Use `default-features = false` and enable only the policies you need for smaller builds.
46+
47+
| Feature | Policy | Description |
48+
|---------|--------|-------------|
49+
| `policy-fifo` | FIFO | First In, First Out |
50+
| `policy-lru` | LRU | Least Recently Used (Arc-wrapped, concurrent wrapper available) |
51+
| `policy-fast-lru` | Fast LRU | Optimized single-threaded LRU (~7–10× faster than LRU) |
52+
| `policy-lru-k` | LRU-K | Scan-resistant with K-th access |
53+
| `policy-lfu` | LFU | Least Frequently Used (bucket-based) |
54+
| `policy-heap-lfu` | Heap LFU | LFU with heap-based eviction |
55+
| `policy-two-q` | 2Q | Two-Queue |
56+
| `policy-s3-fifo` | S3-FIFO | Scan-resistant three-queue FIFO |
57+
| `policy-arc` | ARC | Adaptive Replacement Cache |
58+
| `policy-lifo` | LIFO | Last In, First Out |
59+
| `policy-mfu` | MFU | Most Frequently Used |
60+
| `policy-mru` | MRU | Most Recently Used |
61+
| `policy-random` | Random | Random eviction |
62+
| `policy-slru` | SLRU | Segmented LRU |
63+
| `policy-clock` | Clock | Second-chance clock |
64+
| `policy-clock-pro` | Clock-PRO | Scan-resistant clock |
65+
| `policy-nru` | NRU | Not Recently Used |
66+
67+
**Convenience:** `policy-all` enables every policy above.
68+
69+
```toml
70+
# Minimal build: only LRU and S3-FIFO
71+
cachekit = { version = "0.3", default-features = false, features = ["policy-lru", "policy-s3-fifo"] }
72+
```
73+
4174
## Overview
4275

4376
CacheKit is a Rust library that provides:
@@ -85,7 +118,7 @@ fn main() {
85118

86119
### Available Policies
87120

88-
All policies are available through the unified builder API:
121+
All policies are available through the unified builder API. Enable the corresponding feature flag for each policy (e.g. `policy-lru` for `CachePolicy::Lru`). See [Feature Flags](#per-policy-eviction-policies) above.
89122

90123
```rust
91124
use cachekit::builder::{CacheBuilder, CachePolicy};

bench-support/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ name = "render_docs"
99
path = "src/bin/render_docs.rs"
1010

1111
[dependencies]
12-
cachekit = { path = ".." }
12+
cachekit = { path = "..", features = ["policy-all"] }
1313
criterion = "0.8"
1414
rand = { version = "0.9", features = ["small_rng"] }
1515
rand_distr = "0.5"

docs/getting-started/integration.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,18 @@ The `Cache<K, V>` wrapper requires:
6363

6464
### Policy Selection
6565

66-
| Policy | Use Case | Trade-offs |
67-
|--------|----------|------------|
68-
| `Fifo` | Simple, predictable eviction | No recency/frequency tracking |
69-
| `Lru` | Temporal locality | Vulnerable to scans |
70-
| `LruK { k }` | Scan resistance | Extra memory for history |
71-
| `Lfu { bucket_hint }` | Stable hot spots | Slow to adapt to changes |
72-
| `HeapLfu` | Large caches, frequent evictions | O(log n) eviction |
73-
| `TwoQ { probation_frac }` | Mixed workloads | Two-queue overhead |
74-
| `S3Fifo { small_ratio, ghost_ratio }` | Scan-heavy workloads | Small + ghost queues |
66+
Each policy requires its feature flag (e.g. `policy-lru` for `CachePolicy::Lru`). See [Compatibility and Features](../guides/compatibility-and-features.md).
67+
68+
| Policy | Feature | Use Case | Trade-offs |
69+
|--------|---------|----------|------------|
70+
| `Fifo` | `policy-fifo` | Simple, predictable eviction | No recency/frequency tracking |
71+
| `Lru` | `policy-lru` | Temporal locality | Vulnerable to scans |
72+
| `FastLru` | `policy-fast-lru` | Maximum single-threaded speed | No Arc wrapping, no concurrent wrapper |
73+
| `LruK { k }` | `policy-lru-k` | Scan resistance | Extra memory for history |
74+
| `Lfu { bucket_hint }` | `policy-lfu` | Stable hot spots | Slow to adapt to changes |
75+
| `HeapLfu` | `policy-heap-lfu` | Large caches, frequent evictions | O(log n) eviction |
76+
| `TwoQ { probation_frac }` | `policy-two-q` | Mixed workloads | Two-queue overhead |
77+
| `S3Fifo { small_ratio, ghost_ratio }` | `policy-s3-fifo` | Scan-heavy workloads | Small + ghost queues |
7578

7679
## Direct Policy Access
7780

docs/getting-started/quickstart.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,18 @@ Add CacheKit to your `Cargo.toml`:
88

99
```toml
1010
[dependencies]
11-
cachekit = "0.2.0-alpha"
11+
cachekit = "0.3"
1212
```
1313

14+
Each eviction policy is gated behind a feature flag. The default features include `policy-s3-fifo`, `policy-lru`, `policy-fast-lru`, `policy-lru-k`, and `policy-clock`. For minimal builds, use `default-features = false` and enable only the policies you need:
15+
16+
```toml
17+
[dependencies]
18+
cachekit = { version = "0.3", default-features = false, features = ["policy-lru"] }
19+
```
20+
21+
See [Compatibility and Features](../guides/compatibility-and-features.md) for the full list of per-policy feature flags.
22+
1423
## Build Your First Cache
1524

1625
Use `CacheBuilder` with an eviction policy:

docs/guides/api-surface.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ This page maps the major modules and how they fit together.
1717
3. Use the unified `Cache<K, V>` API for standard operations.
1818
4. Drop into `policy::*` or `ds::*` for advanced or policy-specific operations.
1919

20+
## Feature Flags
21+
22+
Policies are gated behind feature flags (e.g. `policy-lru`, `policy-s3-fifo`). Use `default-features = false` and enable only the policies you need. See [Compatibility and Features](compatibility-and-features.md).
23+
2024
## Where to Start
2125

2226
- [Builder overview](../getting-started/integration.md)

docs/guides/choosing-a-policy.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
This guide summarizes practical trade-offs and mirrors the benchmark-driven guidance
44
in the [latest benchmark guide](../benchmarks/latest/index.md).
55

6+
**Feature flags:** Each policy is gated behind a feature flag (e.g. `policy-lru`, `policy-s3-fifo`). Enable only the policies you need for smaller builds. See [Compatibility and Features](compatibility-and-features.md).
7+
68
## Quick Picks
79

810
- **General purpose, skewed workloads**: `LRU` or `S3-FIFO`

docs/guides/compatibility-and-features.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,46 @@ CacheKit targets the Rust MSRV listed in `Cargo.toml` and reflected in the READM
66

77
## Feature Flags
88

9+
### General
10+
911
- `metrics` — Enables hit/miss metrics and snapshots.
1012
- `concurrency` — Enables concurrent wrappers (requires `parking_lot`).
1113

14+
### Eviction Policies (Per-Policy Feature Flags)
15+
16+
Each eviction policy can be enabled or disabled via its own feature flag. This keeps builds smaller when you only need specific policies.
17+
18+
| Feature | Policy | Description |
19+
|---------|--------|-------------|
20+
| `policy-fifo` | FIFO | First In, First Out |
21+
| `policy-lru` | LRU | Least Recently Used (Arc-wrapped, `ConcurrentLruCache` available) |
22+
| `policy-fast-lru` | Fast LRU | Optimized single-threaded LRU (~7–10× faster than LRU) |
23+
| `policy-lru-k` | LRU-K | Scan-resistant with K-th access |
24+
| `policy-lfu` | LFU | Least Frequently Used (bucket-based) |
25+
| `policy-heap-lfu` | Heap LFU | LFU with heap-based eviction |
26+
| `policy-two-q` | 2Q | Two-Queue |
27+
| `policy-s3-fifo` | S3-FIFO | Scan-resistant three-queue FIFO |
28+
| `policy-arc` | ARC | Adaptive Replacement Cache |
29+
| `policy-lifo` | LIFO | Last In, First Out |
30+
| `policy-mfu` | MFU | Most Frequently Used |
31+
| `policy-mru` | MRU | Most Recently Used |
32+
| `policy-random` | Random | Random eviction |
33+
| `policy-slru` | SLRU | Segmented LRU |
34+
| `policy-clock` | Clock | Second-chance clock |
35+
| `policy-clock-pro` | Clock-PRO | Scan-resistant clock |
36+
| `policy-nru` | NRU | Not Recently Used |
37+
38+
**Default features** include `policy-s3-fifo`, `policy-lru`, `policy-fast-lru`, `policy-lru-k`, and `policy-clock`.
39+
40+
**Convenience feature:** `policy-all` enables every policy above.
41+
42+
**Minimal builds:** Use `default-features = false` and select only the policies you need:
43+
44+
```toml
45+
[dependencies]
46+
cachekit = { version = "0.3", default-features = false, features = ["policy-lru", "policy-s3-fifo"] }
47+
```
48+
1249
## Optional Dependencies
1350

1451
- `parking_lot` — Used for concurrent wrappers behind the `concurrency` feature.

docs/policies/2q.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# 2Q
22

3+
**Feature:** `policy-two-q`
4+
35
## Goal
46
Reduce scan pollution with a simple two-queue design that approximates "access twice before protection".
57

docs/policies/README.md

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,27 @@ If you can only implement one “general purpose” policy for mixed workloads,
3434

3535
### Implemented Policies (CacheKit)
3636

37-
| Policy | Summary | Doc |
38-
|--------|---------|-----|
39-
| LRU | Strong default for temporal locality | [LRU doc](lru.md) |
40-
| MRU | Evicts most recent (niche: cyclic patterns) | [MRU doc](mru.md) |
41-
| SLRU | Segmented LRU with probation/protected | [SLRU doc](slru.md) |
42-
| LFU | Frequency-driven, stable hot sets | [LFU doc](lfu.md) |
43-
| Heap-LFU | LFU with heap eviction | [Heap-LFU doc](heap-lfu.md) |
44-
| MFU | Evicts highest frequency (niche/baseline) | [MFU doc](mfu.md) |
45-
| LRU-K | Scan-resistant recency | [LRU-K doc](lru-k.md) |
46-
| 2Q | Probation + protected queues | [2Q doc](2q.md) |
47-
| ARC | Adaptive recency/frequency balance | [ARC doc](arc.md) |
48-
| FIFO | Simple insertion-order (oldest first) | [FIFO doc](fifo.md) |
49-
| LIFO | Stack-based (newest first) | [LIFO doc](lifo.md) |
50-
| Clock | Approximate LRU | [Clock doc](clock.md) |
51-
| Clock-PRO | Scan-resistant Clock variant | [Clock-PRO doc](clock-pro.md) |
52-
| NRU | Coarse recency tracking | [NRU doc](nru.md) |
53-
| S3-FIFO | Scan-resistant FIFO | [S3-FIFO doc](s3-fifo.md) |
54-
| Random | Baseline: uniform random eviction | [Random doc](random.md) |
37+
Enable the corresponding feature flag for each policy. See [Compatibility and Features](../guides/compatibility-and-features.md).
38+
39+
| Policy | Feature | Summary | Doc |
40+
|--------|---------|---------|-----|
41+
| LRU | `policy-lru` | Strong default for temporal locality | [LRU doc](lru.md) |
42+
| Fast LRU | `policy-fast-lru` | Optimized single-threaded LRU ||
43+
| MRU | `policy-mru` | Evicts most recent (niche: cyclic patterns) | [MRU doc](mru.md) |
44+
| SLRU | `policy-slru` | Segmented LRU with probation/protected | [SLRU doc](slru.md) |
45+
| LFU | `policy-lfu` | Frequency-driven, stable hot sets | [LFU doc](lfu.md) |
46+
| Heap-LFU | `policy-heap-lfu` | LFU with heap eviction | [Heap-LFU doc](heap-lfu.md) |
47+
| MFU | `policy-mfu` | Evicts highest frequency (niche/baseline) | [MFU doc](mfu.md) |
48+
| LRU-K | `policy-lru-k` | Scan-resistant recency | [LRU-K doc](lru-k.md) |
49+
| 2Q | `policy-two-q` | Probation + protected queues | [2Q doc](2q.md) |
50+
| ARC | `policy-arc` | Adaptive recency/frequency balance | [ARC doc](arc.md) |
51+
| FIFO | `policy-fifo` | Simple insertion-order (oldest first) | [FIFO doc](fifo.md) |
52+
| LIFO | `policy-lifo` | Stack-based (newest first) | [LIFO doc](lifo.md) |
53+
| Clock | `policy-clock` | Approximate LRU | [Clock doc](clock.md) |
54+
| Clock-PRO | `policy-clock-pro` | Scan-resistant Clock variant | [Clock-PRO doc](clock-pro.md) |
55+
| NRU | `policy-nru` | Coarse recency tracking | [NRU doc](nru.md) |
56+
| S3-FIFO | `policy-s3-fifo` | Scan-resistant FIFO | [S3-FIFO doc](s3-fifo.md) |
57+
| Random | `policy-random` | Baseline: uniform random eviction | [Random doc](random.md) |
5558

5659
### Roadmap Policies (Planned)
5760

0 commit comments

Comments
 (0)