title: Memoizing based on call location
body: <p>I have a member function with two arguments. Both are pointers to complex objects. When called, the function performs some non-trivial computation and then returns an integer. Like this:</p>
  
  <pre><code>struct Fooer {
    int foo(const A* a, const B* b);
  };
  </code></pre>
  
  <p>The returned integer is always the same if <code>foo()</code> is given the same two arguments. This function is pretty heavily used, so it would make sense to memoize its result. Normally, some lookup table with the key being the pair of pointers would suffice. However, I'm in the unique position where I know all the call sites and I know that any given call site will <em>always</em> use the same pair of parameters during execution. This could greatly speed up memoization if only I could pass in a <em>third</em> parameter, a unique integer that is basically the cache hint:</p>
  
  <pre><code>struct Fooer {
    int foo(const A* a, const B* b, int pos) {
      if (cached_[pos] &gt; 0) return cached_[pos];
      cached_[pos] = /* Heavy computation. */ + 1;
      return cached_[pos];
    }
  
    std::vector&lt;int&gt; cached_;
  };
  </code></pre>
  
  <p>What I'm looking for is a mechanism to easily generate this 'cache hint'. But nothing comes to mind. For now, I'm manually adding this parameter to the call sites of <code>foo()</code>, but it's obviously ugly and fragile. The function is really heavily used so it's worth this kind of optimization, in case you're wondering.</p>
  
  <p>More generally, I'd like to have some kind of 'thunk' at each call site that performs the heavy lifting the first time is called, then just returns the pre-computed integer.</p>
  
  <p>Note that <code>foo()</code> is a member function so that different instances of <code>Fooer</code> should have different caches.</p>