caching - C++ simple cache design for function output -
i assume it's quite frequent problem well-known solutions, wasn't able find. i'm seeking advice here.
problem statement
consider following setting:
class a; // class const f(const a&); // _expensive_ function void do_stuff() { a; a.modify(...); do_stuff1(f(a)); // compute f(a) do_stuff2(f(a)); // use cached value of f(a) a.modify(...); do_stuff3(f(a)); // recompute f(a) }
i return value of f(a)
cached between first , second calls, discarded after second call a.modify()
. edit: in practice, calls f(a)
in different scopes.
here pieces of solutions i've explored, it's worth.
solution 1: central cache
using time stamps
i can imagine simple solution involving adding time stamp class a
function f
can check , decide if needs update cached result, stored somewhere in central cache. guess implies changing signature of f
to:
const a& f(const a&);
problem 1: central cache, need mechanism destroy cached result of f(a)
when a
destroyed.
using hash codes
aside problem 1, seems simple enough. gets complicated when a
stands std::vector<...>
. guess dynamic polymorphism should excluded here. forget adding time stamp subclass of std::vector<...>
, overriding imply. however, compute hash code or uuid based on contents of a
--- assuming cheaper computing f(a)
--- , base central cache on these hash codes. we're facing problem 1 again.
solution 2: coupled objects
i still haven't found how implement this, idea have a
notify cache f(a)
when a
written or destroyed, not when merely read from. can't figure how without dynamic polymorphism, , without slowing down single-element accesses using operator[]
or iterators sending notifications cache each modified element.
problem 2: find mechanism of delimiting sets of changes a
invalidate cache once each set of changes.
i've thought of proxies enable write access on a
(inspired concept of mutex), couldn't come working code.
any ideas?
i've done similar stuff interfaces this:
class f { public: virtual int f(int a)=0; }; class cache : public f { public: cache(f &f) : f(f) { } int f(int a) { /*caching logic here, calls f.f() if not found cache */ } f &f; }; class impl : public f { int f(int a) { /* real implementation here */ } };
then it's deciding use caching logic:
impl i; cache c(i); c.f(10); // put cache key 10 c.f(10); // found cache c.f(11); // put cache key 11
Comments
Post a Comment