The Dynamic-Way LRU (Least Recently Used) cache is a memory caching mechanism that dynamically allocates and evicts cache blocks based on access patterns. Unlike traditional set-associative caches that pre-allocate all blocks with valid field all set to false, this approach only creates cache blocks when needed, ensuring efficient memory usage while maintaining LRU-based eviction.
Dynamic Allocation: Cache blocks are created on demand instead of being preallocated.
LRU Eviction: The cache maintains a doubly linked list to efficiently track (update) and evict the least recently used blocks.
Upon accessing a block:
If the block already exists in the cache (Hit), it is moved to the front of the doubly linked list.
If it does not exist (Miss) and the set still have space, a new block is dynamically allocated.
If the set is full, the LRU block (tail of the list) is evicted before adding a new one. Afterwards, the new block is also moved to the front of the list.
Empty Cache Set
: Before any memory accesses, the set only has dummy head and dummy tail.
Miss!
A1115550 is not in the set.
Insert it at the front of the list.
Miss!
A1115551 is also missing.
Insert it at the front, shifting A1115550 back.
Hit!
A1115550 is in the set.
Move it to the front.
More blocks (A1115552, A1115553) are accessed and added until the set is full (4-way associativity).
The set is now full, next miss will trigger LRU eviction.
Miss!
Because A1115560 is not in the set.
The set is full => Evict LRU block (A1115551).
Insert A1115560 at the front.
Hit!
A1115550 is in the set.
Move it to the front (MRU).
O(1)
.Insert at the head of the doubly linked list (O(1)).
Insert into HashMap (O(1)).
Overall: O(1) + O(1) = O(1)
.
Find in HashMap: O(1).
Remove from current position: O(1).
Insert at the front: O(1).
Overall: O(1) + O(1) + O(1) = O(1)
.
Identify LRU block (always at the tail): O(1).
Remove from doubly linked list: O(1).
Remove from HashMap: O(1).
Insert new block at head: O(1).
Overall: O(1) + O(1) + O(1) + O(1) = O(1)
.
Operation | Complexity |
---|---|
Lookup | O(1) |
Insert | O(1) |
Move to Head | O(1) |
Eviction | O(1) |
Overall Complexity: O(1)
.
cargo
Clone the repository
git clone https://github.com/mellivorandy/cache-miss-analyzer.git
From the project root, run:
cargo build
To execute the program with custom arguments, use the following command:
cargo run -p dynamic_way_lru -- <trace_file_name> <cache_size> <block_size> <set_degree>
In this project structure, the trace.txt file is located in cache-miss-analyzer/data, change the path if the trace file is moved or new trace files are added.
Note: If the file path remains unchanged, use the path as given. Simply copy and paste the following command into your terminal:
cargo run -p dynamic_way_lru -- ./data/trace.txt 128 4 1
A [cfg(test)]
test module is included, referencing a trace.txt file for unit tests. To run them:
cargo test -p dynamic_way_lru -- --nocapture
This prints the non-empty sets.
I sincerely appreciate the incredible work behind Mermaid.js, an open-source diagramming tool that makes visualizing complex ideas effortless. Huge thanks to the developers and contributors who maintain and improve this fantastic project!