Live data from Hacker News

Show HN: A header only C++11 LRU Cache template class, with no dependencies

github.com

21–30 of 31 posts

Re: Show HN: A header only C++11 LRU Cache template class, with no dependencies

#21
post #15
post #10

Not really sure why they reimplemented std::list. Also that NoCopy base class adds a vptr for no reason. And so do all those other virtual destructors. I personally wouldn't trust this code at first glance.

the kv::List is not intended as a generic linked list implementation like std::list. The reason for the specific implementation is to get constant time removal and remove-add_to_end operations. Truth be told, it is a hold-over from the previous implementation. I shall be writing benchmarks for this next up and shall revisit the decision on whether or not to use std::list soon-ish :) The : private NoCopy is just to bl…

>The : private NoCopy is just to block the copy constructors.

If its C++11 why not just use the delete keyword to prevent auto generating members ?

Re: Show HN: A header only C++11 LRU Cache template class, with no dependencies

#23
post #15
post #10

Not really sure why they reimplemented std::list. Also that NoCopy base class adds a vptr for no reason. And so do all those other virtual destructors. I personally wouldn't trust this code at first glance.

the kv::List is not intended as a generic linked list implementation like std::list. The reason for the specific implementation is to get constant time removal and remove-add_to_end operations. Truth be told, it is a hold-over from the previous implementation. I shall be writing benchmarks for this next up and shall revisit the decision on whether or not to use std::list soon-ish :) The : private NoCopy is just to bl…

"The reason for the specific implementation is to get constant time removal and remove-add_to_end operations"

http://en.cppreference.com/w/cpp/container/list:

"std::list is a container that supports constant time insertion and removal of elements from anywhere in the container."

'The : private NoCopy is just to block the copy constructors."

But does it need a virtual destructor?

Re: Show HN: A header only C++11 LRU Cache template class, with no dependencies

#25
post #15
post #10

Not really sure why they reimplemented std::list. Also that NoCopy base class adds a vptr for no reason. And so do all those other virtual destructors. I personally wouldn't trust this code at first glance.

the kv::List is not intended as a generic linked list implementation like std::list. The reason for the specific implementation is to get constant time removal and remove-add_to_end operations. Truth be told, it is a hold-over from the previous implementation. I shall be writing benchmarks for this next up and shall revisit the decision on whether or not to use std::list soon-ish :) The : private NoCopy is just to bl…

Personally I would use a macro such as:

    #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
    TypeName(TypeName&) = delete;              \
    void operator=(TypeName) = delete;
Introducing a virtual base class for disabling copying introduces a vtable and un-needed inheritance.

Re: Show HN: A header only C++11 LRU Cache template class, with no dependencies

#26

Or if you want a non-obfuscated version https://gist.github.com/Quinny/09e34afb1d187e43dea2f3c3b0c04...

ah, now I remember why I had carried on with the linked list from the old c++ codebase

https://gist.github.com/Quinny/09e34afb1d187e43dea2f3c3b0c04...

the key refresh in std::list required a copy of the std::pair, where as I wished to keep it as an unlink / relink

Re: Show HN: A header only C++11 LRU Cache template class, with no dependencies

#27
post #19
post #15

Earlier quoted context omitted.

the kv::List is not intended as a generic linked list implementation like std::list. The reason for the specific implementation is to get constant time removal and remove-add_to_end operations. Truth be told, it is a hold-over from the previous implementation. I shall be writing benchmarks for this next up and shall revisit the decision on whether or not to use std::list soon-ish :) The : private NoCopy is just to bl…

You could have used std::list iterators instead of raw pointers to a node in the map to implement that.

now i remember why I went with the old linked list implementation.

with the std::list, everytime I refreshed a node... i did a copy.

basically to the tune of list.push_back(*iter) where as I wanted to keep it as a simple unlink and relink of the node

copy of the comment: https://news.ycombinator.com/item?id=12391069

Re: Show HN: A header only C++11 LRU Cache template class, with no dependencies

#28
post #27
post #19

Earlier quoted context omitted.

You could have used std::list iterators instead of raw pointers to a node in the map to implement that.

now i remember why I went with the old linked list implementation. with the std::list, everytime I refreshed a node... i did a copy. basically to the tune of list.push_back(*iter) where as I wanted to keep it as a simple unlink and relink of the node copy of the comment: https://news.ycombinator.com/item?id=12391069

To swap nodes in the list you need to use splice like this:

keys_.splice(keys_.begin(), keys_, iter->second);

Where iter is from the cache lookup.

Re: Show HN: A header only C++11 LRU Cache template class, with no dependencies

#30
One of two (or both) things would need to be changed for it to be generally useful:

- The return value should be a WeakPtr, not a bool and a reference. This allows for data to not be duplicated by all of the different `getter` threads. A WeakPtr is kind of C++'s equivalent of a Maybe.

- The getter function also need to be passed a functor capable of generating the object, which then creates a `named lock` that /only/ other threads requesting that key. This duplicates multiple logical copies of the same item from being created (this is important when the creation is expensive, presumably the core reason for using an LRU cache).

edit: formatting, I am not familiar with HN syntax.

Post reply on HN