[PEAK] Trellis + expensive operations

Phillip J. Eby pje at telecommunity.com
Wed Nov 26 12:26:55 EST 2008


At 06:29 PM 11/25/2008 -0500, Christopher Stelma wrote:
>I'm trying to avoid some unnecessary computation, and I feel I must 
>be missing something, either in terms of Trellis API or approach to 
>the problem.
>
>I have a value y which depends on x.  x would be a trellis managed 
>piece of data.  The computation of y from x is expensive.
>
>Right now, I have a pattern something like:
>
>class A(object):
>     x = None
>     last_x = None
>     last_y = None
>     @property
>     def y(self):
>         if self.x != self.last_x:
>             self.last_y = some_expensive_calculation(self.x)
>         return self.last_y
>
>I would like to accomplish something equivalent with Trellis 
>attributes or Cells, so that I don't have to repeat the computation 
>unless the computed value would change.  Using a maintenance rule, y 
>would be recomputed every time some dependency in x changed.  Using 
>a compute rule, y is recomputed every time I access y.  My goal is 
>to be able to set x some number of times, followed by accessing y 
>some number of times, but only ever do the calculation once.  I 
>realize that this effect may be achieved by an optional maintenance 
>rule, but I'd like the same effect for every set of x and every 
>access of y, not just the first access of y.

If I understand you correctly, you need to be able to tell whether y 
"would change", perhaps by using some partial calculation on x?  If 
that's the case, then create another maintenance rule z, that does 
that partial calculation, and write y to use z, rather than directly using x.

(Also, note that @compute rules are NOT recalculated on every access, 
*if* there is some other rule that depends upon them.  In other 
words, if you have some other cell depending on y, then y will only 
be recalculated when x changes.)




More information about the PEAK mailing list