On Mon, Dec 09, 2002 at 02:20:01PM -0800, Austin Hastings wrote:
> 
> --- Paul Johnson <[EMAIL PROTECTED]> wrote:
> > On Mon, Dec 09, 2002 at 01:58:11PM -0800, Austin Hastings wrote:
> > > Ahh. This is better. How does one implement a more sophisticated
> > > cache management strategy?
> > > 
> > > That is, what is the mechanism for manipulating the run-time system
> > > behavior of subs?
> > 
> > How about the same way as one would do it now?  Presumably we won't
> > all
> > forget how to program when Perl 6 comes out.
> 
> Paul,
> 
> I think you've missed the point. The original poster (Smylers) asked if
> there was a benefit to only cacheing certain values, presuming the
> remainder would be either trivial to compute or internally cached, or
> both.
> 
> The suggestion was that a more advanced cache management strategy could
> be attached, presumably changing the behavior of the
> function-return-caching subsystem. 
> 
> I'm all in favor of that, but it's a new rock to turn over looking for
> details.
> 
> If you're proposing that the right answer is to not cache the function,
> but rather implement an internal cache, then cool - you've got a friend
> in Smylersvania. 
> 
> But if not, then presumably you know something I do not know: enlighten
> me, please?

What I'm suggesting is that instead of:

  sub days_in_month(Str $month, Int $year)
  {
    $month = lc $month;
    if $month eq 'feb'
    {
      # Do 'complicated' calculation and cache the result for later use:
      my $leap = $year % 4 == 0
          && ($year % 100 != 0 || $year % 400 == 0);
      cache_and_return $leap ? 29 : 28;
    }

    else
    {
      # Simple look-up, so caching would be counter-productive:
      return %days{$month};  # %days was declared above (honest)  
    }
  }

We get:

  sub days_in_month(Str $month, Int $year)
  {
    $month = lc $month;
    if $month eq 'feb'
    {
      # Do 'complicated' calculation and cache the result for later use:
      my %leap_cache is static;  # or something ...
      my $leap = $leap_cache{$year} //=
          $year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0);
      return $leap ? 29 : 28;
    }

    else
    {
      # Simple look-up, so caching would be counter-productive:
      return %days{$month};  # %days was declared above (honest)  
    }
  }

OK, the cache isn't identical, but it could be programmed to be
identical, or it could be better by caching lc $month rather than
$month.

That way I can use constructs I already (will) know and do the caching
just how I want.  Of course, I can do this anyway, and maybe there are
benefits to having a partial caching system built into the language, but
I don't see it at the moment.

-- 
Paul Johnson - [EMAIL PROTECTED]
http://www.pjcj.net

Reply via email to