From: "Shot (Piotr Szotkowski)" Date: 2008-02-22T01:01:44+09:00 Subject: Re: Object#freeze as a basis for caching of method results? --jUyrq1hnz4WvHzxT Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable // Thanks a lot for that delegate tutorial in the other post =E2=80=93 // I finally understood what=E2=80=99s delegation for and how to do it! :) Robert Klemme: > 2008/2/21, Shot (Piotr Szotkowski) : >> + @@cache =3D Hash.new { |hash, key| hash[key] =3D {} } >> def beta_f >> - outputs.to_blanket >> + @@cache[Marshal.dump self][:beta_f] ||=3D outputs.to_blanket >> end > 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. I ran some very rough and unscientific tests with ruby-prof and the results are as follows: uncached: 44.42s Marshal.dump: 0.60s Marshal.dump.freeze: 0.59s freezing the object: 0.43s This is for 96 calls, though, so I assume freezing the dump will have a bigger impact on more-often-called methods (but then the gain from simply caching the value =E2=80=98as is=E2=80=99 and freezing the object in= question will be bigger as well, as marshalling takes its toll on every call). Running the whole application in Ruby 1.9 is roughtly three times faster than in 1.8, but I can=E2=80=99t=C2=B9 profile it. :( =C2=B9 http://groups.google.com/group/ruby-talk-google/browse_thread/thread= /b3d26892c642779c > 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). Ok, I=E2=80=99m totally lost here. I don=E2=80=99t see any demarshalling ha= ppening, just marshalling (and using that as a key)=E2=80=A6 What am I missing? If keeping the marshalled objects in the memory turns out to be a problem, I can try trading it for speed (and a tiny bit of confidence) by MD5-hashing them, but I assume MD5 is relatively slow, so it=E2=80=99d b= e an exchange at a rather bad rate. >> 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? :-) I=E2=80=99m a PHP programmer=C2=B2 by day. =C2=B2 http://civicrm.org/ -- Shot (seriously, though, it=E2=80=99s a sig of unknown origin from Stewa= rt Stremler=E2=80=99s collection: http://www-rohan.sdsu.edu/~stremler/sigs/sig= s.html) --=20 The most secure computer in the world is one not connected to the Internet. That's why I recommend Telstra ADSL. -- FreeFrag --jUyrq1hnz4WvHzxT Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (GNU/Linux) iD8DBQFHvZuZi/mCfdEo8UoRArfTAJ4x10p7/XbIdK/iOovNToaY5DZkrgCdHWDd idElaEIR3n2z1GpcJ5q34pU= =SxbB -----END PGP SIGNATURE----- --jUyrq1hnz4WvHzxT--