Robert Klemme
2/21/2008 10:47:00 AM
2008/2/21, Shot (Piotr Szotkowski) <shot@hot.pl>:
> Robert Klemme:
>
> > An alternative approach would be to use current state as cache key,
> > i.e. create an immutable copy and stuff that along with calculation
> > results into a Hash.
>
> What would you say about the following approach? I'm not sure it scales
> well, but seems to work properly, have the smallest diff impact on the
> existing codebase and take object state into account=85
>
> shot@devielle:~/work/PhD/bzr/trunk$ bzr diff
> =3D=3D=3D modified file 'trunk/lib/art-decomp/fsm.rb'
> --- trunk/lib/art-decomp/fsm.rb 2008-02-17 21:36:39 +0000
> +++ trunk/lib/art-decomp/fsm.rb 2008-02-21 09:03:53 +0000
> @@ -1,5 +1,7 @@
> class ArtDecomp::FSM < ArtDecomp::TruthTable
>
> + @@cache =3D Hash.new { |hash, key| hash[key] =3D {} }
> +
> attr_accessor :q, :qp
>
> def self.from_kiss string
> @@ -31,7 +33,7 @@
> end
>
> def beta_f
> - outputs.to_blanket
> + @@cache[Marshal.dump self][:beta_f] ||=3D outputs.to_blanket
> end
>
> def beta_q
Funny that you mention it: I had thought of copying the key via
Marshal#dump and #load. Using the marshaled string is a nice idea!
You can make this a tad more efficient by doing
@@cache[Marshal.dump(self).freeze][:beta_f] ||=3D outputs.to_blanket
because there is an optimization in Hash that copies unfrozen Strings
that are used as Hash keys in order to avoid aliasing effects.
Now, whether you use the String or demarshal probably mainly depends
on memory usage. If the String is short enough that approach is
certainly preferable because it incurs less processing overhead
(demarshaling).
> So the Ether Bunny goes hippety hopity down the garden path, waylaying
> innocent fieldmice and anesthetising them, so he can sell their teeth
> to the Tooth Fairy to support his milk-and-cookies habit.
What kind of dope are *you* smoking? :-)
Cheers
robert
--=20
use.inject do |as, often| as.you_can - without end