r/cpp • • Jan 03 '17

A feature complete LRU cache implementation in C++

https://github.com/goldsborough/lru-cache
7 Upvotes

19 comments sorted by

19

u/finalman Jan 03 '17

A least recently used (LRU) cache is a fixed size cache that behaves just like a regular lookup table, but remembers the order in which elements are inserted. Once its (user-defined) capacity is reached, it uses this information to replace the least recently used element with a newly inserted one.

Wouldn't an LRU cache keep track of the last accessed element and evict that?

3

u/goldsborough Jan 03 '17

Thanks everyone for the feedback. I've changed the behavior of the cache to be properly LRU -- lookup methods now move the element to the front. I wasn't trying to click-bait anyone by advertising LRU while it was LRI. I just didn't call my "move to front" method (it's just one line) inside lookup/find to see if people would think that's "OK". I figure it's not, so now it's what's expected.

-3

u/goldsborough Jan 03 '17

I thought about this for a while and decided not moving elements to the front in the access methods would make the lookup faster. Also often "LRI" is actually good enough or preferable. I was hoping people would share their thoughts. I could also add a boolean switch to make it optional.

27

u/Netzapper Jan 03 '17

You can't really call it a least-recently used cache if you're basing it on insert order. The semantics are not the same.

Anyway, you don't need to actually move the element to the front. Instead, you can have a separate lightweight index that handles the ordering and bookkeeping so you never have to move your actual data elements around.

1

u/goldsborough Jan 03 '17

I store the order in a linked list of references, so that's pretty lightweight (no data is moved around).

1

u/dodheim Jan 03 '17

Except that linked lists tend to be allocation-heavy. (I haven't looked at this code, so apologies if this doesn't apply.)

1

u/goldsborough Jan 04 '17 edited Jan 04 '17

I allocate new list nodes (i.e. list::insert) until the cache is full. At that point, I reuse old nodes by assigning their key to a new key (on insertion) and moving them to the front. As such there will only be C allocations ever, where C is the capacity of the cache.

1

u/Netzapper Jan 03 '17

Except that linked lists tend to be allocation-heavy

Really?

Moving a node's position inside the list should be O(1) with no allocation.

Actually, once you've got the node wrapper for your data element, what allocations need to be made? I mean, I'm sure some are made in the std::list implementation... but algorithmically, I don't see that it's necessary, or that it's a property of linked lists themselves.

2

u/ArunMu The What ? Jan 03 '17

Redis does something like that but with some added randomization. It also does not move elements to the front of list, but takes some random N samples of keys and removes the least recently accessed key from it. Atleast this is what the algorithm used to be some time back.

1

u/jpvienneau Jan 04 '17

Why would they use some randomization?

11

u/ZenEngineer Jan 03 '17

LRI can easily fall into pessimal usage patterns. It'll periodically evict the most used items, which kind of defeats the purpose of a cache.

Also it makes your post have a clickbait title as it's neither LRU or feature complete.

LRI is easy to implement. LRU is much harder to do right.

I guess using a linked list would speed up the moving up to front phase (with pre-allocated nodes of course), so you don't have to change your design too much.

1

u/goldsborough Jan 03 '17

I always had the LRU implementation ready, I had no bad in mind. The order is maintained in a linked list of references and I splice nodes, so there's no data duplication and moving to the front is fast.

0

u/matthieum Jan 03 '17 edited Jan 04 '17

I guess using a linked list would speed up the moving up to front phase (with pre-allocated nodes of course)

That's where I really wish C++ had intrinsic containers; with an intrinsic intrusive container, you can link/unlink a node without allocating in most cases, making it ideal for composing containers.

6

u/Benabik Jan 03 '17

You can move nodes between lists without allocation via std::list::splice.

Also, I think you mean intrusive containers. There's a convenient implementation in Boost.Intrusive.

2

u/matthieum Jan 04 '17

I did mean intrusive, thank you.

As for splicing... that's only half the story. The unordered_map is allocating for each element inserted much like the list is allocating for each element. That's two allocations per element, whereas with intrusive nodes it could be a single one per element... or even a single array of such elements.

Now, in this particular case, this might not be an issue. But in general for composing containers it helps.

With Boost on the table, however, one can jump directly to Boost.MultiIndex for composites :)

3

u/ArunMu The What ? Jan 03 '17

Maybe you could also add an eviction mechanism based on use frequency, least frequently used (LFU)

1

u/goldsborough Jan 03 '17

Sure, if people find that interesting I can add it. It would be quite straight forward to subclass another implementation from the BaseCache (just like TimedCache).

1

u/ArunMu The What ? Jan 04 '17

I just checked the Redis code base and it uses an interesting approach to LFU (uses a logarithmic counter) and it should be better than the LRU approach that was in use earlier.

0

u/[deleted] Jan 03 '17

[deleted]

1

u/Supadoplex Jan 03 '17

In general, cache is an associative structure. Keys have associated values, and you can look up values by the keys (more efficiently than producing the value). It is like a dictionary (an unordered map) with a strategy to evict old elements.

A circular buffer is not associative. It is simply a sequence of values.

Values of a cache are accessed in an arbitrary order. The values of a circular buffer are accessed in same order as they were inserted.

A FIFO cache (a LRI cache) could be implemented using a circular buffer, although the linear lookup complexity (in relation to the size of the ring) would be bad.