Smylers wrote:

I was wondering whether it'd be better to have this specified per
C<return> rather than per C<sub>.
I doubt it. There's no performance gain from partial caching since
you have to check the cache anyway to detect that a particular result
isn't cached.

And in those rare cases where you really do need partial caching, the
simplest solution is to split the partially cached subroutine into a
fully cached sub and an uncached sub:

  sub days_in_month(Str $month, Int $year)
  {
    $month = lc $month;
    if $month eq 'feb'
    {
      my sub feb_days (Int $year) is cached {
          my $leap = $year % 4 == 0
          && ($year % 100 != 0 || $year % 400 == 0);
          return $leap ? 29 : 28;
      }
      return feb_days($year);
    }

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

Damian


Reply via email to