10

I'm trying to implement LFU (Least Frequently Used) cache using pure STL (I don't want to use Boost!).

Requirements are:

  • Associative access to any element using a Key like with std::map.
  • Ability to release the lowest priority item (using its UsesCount attribute).
  • Ability to update priority (UsesCount) of any item.

The problems are:

  • If I use std::vector as container of items (Key, Value, UsesCount), std::map as a container of iterators to the vector for associative access and std::make_heap, std::push_heap and std::pop_heap as priority queue implementation within the vector, the itertors in the map are not valid after heap operations.
  • If I use std::list (or std::map) instead of std::vector in the previous configuration, std::make_heap etc. can't be compiled becasue their iterators does not support aritmetic.
  • If I'd like to use std::priority_queue, I don't have ability to update item priority.

The questions are:

  • Am I missing something obvious how this problem could be solved?
  • Can you point me to some pure C++/STL implementation of LFU cache meeting previous requirements as an example?

Thank you for your insights.

6
  • 3
    "the itertors in the map are not valid after heap operations" - fix this by doing it the other way around -- put the data in the map, where it won't get moved even if other elements are inserted/erased. Then put map iterators in your vector and build a heap out of it. You'd probably still be missing the ability to efficiently update the priority of an item, though, so this isn't an answer. Jul 10, 2012 at 8:47
  • Thank you for another idea that does not crossed my mind. But if I'd have std::vector of std::map iterators, how could I define their comparison operator, that would look inside the pointee object at UsesCount attribute, to be able to fix heap using std::make_heap after item insertion or UsesCount update?
    – Blackhex
    Jul 10, 2012 at 8:52
  • 3
    using a comparison functor like: bool operator()(MapIter a, MapIterB) { return a->second.UseCount < b->second.UseCount; }
    – Useless
    Jul 10, 2012 at 9:10
  • Just on the pedantic side, you're sure you don't mean Most frequently used (MFU) / Least recently used (LRU) ? Least frequently used, why would one ever want to cache these ?
    – FrankH.
    Jul 10, 2012 at 10:52
  • 1
    Quoting form Wikipedia "LFU counts how often an item is needed. Those that are used least often are discarded first.", this implies that it is what I want.
    – Blackhex
    Jul 10, 2012 at 11:20

2 Answers 2

3

Your make implementation using the *_heap functions and a vector seems to be a good fit. although it will result in slow updates. The problem about iterator invalidation you encounter is normal for every container using a vector as an underlying data structure. This is the approach also taken by boost::heap::priority_queue, but it does not provide a mutable interface for the reason mentioned above. Other boost::heap data-structures offer the ability to update the heap.

Something that seems a little odd: Even if you would be able to use std::priority_queue you will still face the iterator invalidation problem.

To answer your questions directly: You are not missing something obvious. std::priority_queue is not as useful as it should be. The best approach is to write your own heap implementation that supports updates . To make it fully STL compatible (especially allocator aware) is rather tricky and not a simple task. On top of that, implement the LFU cache.

For the first step, look at the Boost implementations to get an idea of the effort. I'm not aware of any reference implementation for the second.

To work around iterator invalidation you can always, choose indirection into another container, although you should try to avoid it as it creates an additional cost and can get quite messy.

6
  • How do you update priority with *_heap? I think it's not just priority_queue that can't do the job here: the standard heap functions can't. So the questioner needs a different heap implementation. I could be wrong, though. Jul 10, 2012 at 9:16
  • 1
    @SteveJessop Maybe I'm wrong here, but after an update of a priority calling make_heap again should fix the heap. This is probably far from optimal, though.
    – pmr
    Jul 10, 2012 at 9:22
  • Agreed. That will restore the heap invariant, but it's O(n). Other heap implementations can increase/decrease/update in O(log n). Jul 10, 2012 at 9:23
  • @SteveJessop I added that to the answer hopping it becomes a little better.
    – pmr
    Jul 10, 2012 at 9:40
  • 1
    @Blackhex: make_heap can be called on each update. All it does is re-order the elements in the range you give it. There's no need to call it on insert, because push_heap handles insertion efficiently. Jul 10, 2012 at 9:49
1

A somewhat simpler approach than keeping two data structures:

  • just keep a map, which maps your keys to their value/use-count pair.
  • when the cache is full:
    • create a vector of iterators to the map elements (O(n))
    • use std::nth_element to find the worst 10% (O(n))
    • remove them all from the map (O(n log n))

So, adding a new element to the cache is common case O(log n), worst case O(n log n), and amortized O(log n).

Removing the worst 10% might be a bit drastic in a LFU cache, because new entries have to make the top 90% or they're cut. Then again, if you only remove one element then new entries still need to get off the bottom before the next new entry, or they're cut, and they have less time to do so. So depending why LFU is the right caching strategy for you, my change to it might be the wrong strategy, or it might still be fine.

1
  • @DeadMG: good point, and the questioner specifies STL, so there's definitely one available. There isn't in C++03 (without Boost or TR1) Jul 10, 2012 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.