Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

algorithm LRU, how many bits needed for implement this algorithm?

I have a little question about the algorithm LRU. If you have a cache with four blocs , how many bits do you need to implement this algorithm ?

like image 887
Latsuj Avatar asked Nov 02 '13 11:11

Latsuj


1 Answers

Assuming you mean a 4-way set-associative cache:
A "perfect" LRU would essentially be assigning each line an exact index in the order of usage. You can also think of that as an "age". So each of the 4 elements would require an index of 2 bits (since we need to count 4 distinct ages) stating its location in the LRU order - this means 2 bits * 4 ways, per each set of the cache.
In the general case of n ways, you'd need log2(n) bits per line, or n*log2(n) bits per set.

By the way, there are cheaper ways to reach an almost-LRU behavior, see for e.g. Pseudo LRU which would only require 3 bits for the entire set in your case (or in general: #ways - 1)

like image 146
Leeor Avatar answered Oct 05 '22 11:10

Leeor