<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>Marks Blog</title>
 <link href="http://markijbema.github.io/" rel="self"/>
 <link href="http://markijbema.github.io"/>
 <updated>2013-09-01T14:39:26-07:00</updated>
 <id>http://markijbema.github.io</id>
 <author>
   <name>Mark IJbema</name>
   <email>markijbema@gmail.com</email>
 </author>

 
 <entry>
   <title>Memoize all the things</title>
   <link href="http://markijbema.github.io/memoize-all-the-things"/>
   <updated>2013-09-01T00:00:00-07:00</updated>
   <id>http://markijbema.github.io/memoize-all-the-things</id>
   <content type="html">&lt;p&gt;Today Henrik Nyh posted on his blog about &lt;a href=&quot;http://thepugautomatic.com/2013/08/memoization-is-a-liability/&quot;&gt;the dangers of memoization&lt;/a&gt;. While I agree with the premise that there are dangers with memoization, I don&amp;#39;t believe they are to blame on memoization. I added a comment, but I think my motivation for using memoization deserves a post of its own. But please read &lt;a href=&quot;http://thepugautomatic.com/2013/08/memoization-is-a-liability/&quot;&gt;Henrik&amp;#39;s post&lt;/a&gt; first, as it makes an excellent point, and I think you should look at both sides of the issue.&lt;/p&gt;

&lt;h2&gt;Why use memoization?&lt;/h2&gt;

&lt;p&gt;I will start out with a probably first version of the example Henrik used on his blog:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Word&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;string&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;do_something&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;letters&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chars&lt;/span&gt;

    &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;times&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;whatever&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;letters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Since converting a string to characters probably has nothing to do with doing something, it is probably worthwhile to extract this to its own method. But now we run into a problem. Since the value of letters is used multiple times, we will calculate it multiple times if we don&amp;#39;t memoize it:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot; data-lang=&quot;ruby&quot;&gt;  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;do_something&lt;/span&gt;
    &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;times&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;whatever&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;letters&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;letters&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chars&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The alternative is to memoize it, and not calculate it multiple times:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;ruby language-ruby&quot; data-lang=&quot;ruby&quot;&gt;  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;letters&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@letters&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||=&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@string&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chars&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Either way we&amp;#39;re going to break compatibility with the current implementation, so to me the sentiment of &amp;#39;premature optimization&amp;#39;, which was suggested in the comments doesn&amp;#39;t hold.&lt;/p&gt;

&lt;h2&gt;When use memoization?&lt;/h2&gt;

&lt;p&gt;I think you should only use memoization in immutable objects. Combining mutable objects with memoization opens up a world of pain. In my opinion, this is caused by the immutability though.&lt;/p&gt;

&lt;p&gt;Lets look at what happens if you add a setter for string to our Word class? The memoized version of Word is broken: it will always return the result based on the first string. &lt;strong&gt;But the non-memoized version is broken as well&lt;/strong&gt;: &lt;code&gt;do_something&lt;/code&gt; suddenly isn&amp;#39;t thread safe anymore.&lt;/p&gt;

&lt;p&gt;To me, the latter feels more dangerous that former. Suddenly you have a program which only &lt;em&gt;sometimes&lt;/em&gt; fail (in multiple ways), which your unit tests can (and probably will) miss, as opposed to a program which fails in a very predictable way.&lt;/p&gt;

&lt;h2&gt;Is memoization dangerous?&lt;/h2&gt;

&lt;p&gt;I think in most cases memoization is perfectly safe. There are some pit-falls, and you should be aware of them&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;don&amp;#39;t memoize values which can be falsey (as they won&amp;#39;t memoize)&lt;/li&gt;
&lt;li&gt;don&amp;#39;t memoize in mutable classes (or be prepared to solve cache invalidation problems).&lt;/li&gt;
&lt;li&gt;don&amp;#39;t memoize methods with parameters (unless you&amp;#39;re very sure you&amp;#39;ll call it with the same parameters again)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But in most cases memoization solves more problems than it causes.&lt;/p&gt;

&lt;h2&gt;Is mutability dangerous?&lt;/h2&gt;

&lt;p&gt;Yes&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>New blog</title>
   <link href="http://markijbema.github.io/blog/new-blog"/>
   <updated>2013-08-10T00:00:00-07:00</updated>
   <id>http://markijbema.github.io/blog/new-blog</id>
   <content type="html">&lt;p&gt;I had two blogs before, but I believe I had less to write about then.
Recently I felt the urge to write again, and that&amp;#39;s what I&amp;#39;ll be doing here.&lt;/p&gt;
</content>
 </entry>
 
 
</feed>