Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6086051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:42:29+00:00 2026-05-23T11:42:29+00:00

I assume it’s a quite frequent problem with well-known solutions, which I wasn’t able

  • 0

I assume it’s a quite frequent problem with well-known solutions, which I wasn’t
able to find. So I’m seeking advice here.

Problem Statement

Consider the following setting:

class A; // some class

const A f(const A&); // an _expensive_ function

void do_stuff()
{
    A 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 would like the return value of f(a) to be cached between the first and
second calls, but to be discarded after the second call to a.modify().
EDIT: In practice, the calls to f(a) will be in different scopes.

Here are the pieces of solutions I’ve explored, for what it’s worth.

Solution 1: Central Cache

Using time stamps

I can imagine a simple solution involving adding a time stamp to class A that
function f can check and decide if it needs to update its cached result,
stored somewhere in a central cache. I guess this also implies changing the
signature of f to:

const A& f(const A&);

Problem 1: with a central cache, we need a mechanism to destroy the
cached result of f(a) when a is destroyed.

Using hash codes

Aside from Problem 1, this seems simple enough. But it gets complicated when A
stands for std::vector<...>. I guess dynamic polymorphism should be excluded
here. So we forget about adding a time stamp to a subclass of std::vector<...> and all
the overriding that it would imply. However, we could compute some hash code or UUID
based on the contents of a — assuming that it is much cheaper than computing
f(a) — and base the central cache on these hash codes. But we’re facing
Problem 1 again.

Solution 2: Coupled Objects

I still haven’t found how to implement this, but the idea is to have a notify
the cache for f(a) when a is written to or destroyed, but not when it is
merely read from. I can’t figure how to do that without dynamic polymorphism,
and without slowing down single-element accesses using operator[] or
iterators by sending notifications to the cache for each modified element.

Problem 2: find a mechanism of delimiting sets of changes to a to invalidate the cache only once for each set of changes.

I’ve thought of proxies to enable write access on a (inspired by the concept
of mutex), but couldn’t come up with any working code.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:42:29+00:00Added an answer on May 23, 2026 at 11:42 am

    I’ve done similar stuff with interfaces like 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 from cache */ }
       F &f;
    };
    
    class Impl : public F
    {
       int f(int a) { /* real implementation here */ }
    };
    

    Then it’s just deciding where to use the caching logic:

       Impl i; 
       Cache c(i);
       c.f(10); // put to cache with key 10
       c.f(10); // found from cache
       c.f(11); // put to cache with key 11
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assume I've declared a function (or class, doesn't matter) in a header file, which
Assume I have a normal SQL procedure which has a few arguments. During debugging
Assume the following xml input... <incidents> <incident> <year>2011</year> <other data here> </incident> <incident> <year>2009</year>
Assume I have an Activity which contains two FrameLayouts (let's call them FrameA and
Assume we have a Menu class that has SubMenus (which is the same type
Assume I have a list of words, and I want to find the number
Assume I have two strings (or byte arrays) A and B which both have
Assume my string is http://www.test.com\r\nhttp://www.hello.com<some text here>http://www.world.com I want to extract all URLs in
Assume a regular expression, which, via a Java Matcher object, is matched against a
Assume we have a integer of bitsize n=4; The problem I am describing is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.