<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Merbist &#187; performance</title>
	<atom:link href="http://merbist.com/tag/performance/feed/" rel="self" type="application/rss+xml" />
	<link>http://merbist.com</link>
	<description>Random thoughts of a software developer</description>
	<lastBuildDate>Tue, 03 Jan 2012 02:34:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Ruby optimization example and explanation</title>
		<link>http://merbist.com/2011/09/05/ruby-optimization-example-and-explaination/</link>
		<comments>http://merbist.com/2011/09/05/ruby-optimization-example-and-explaination/#comments</comments>
		<pubDate>Mon, 05 Sep 2011 22:58:21 +0000</pubDate>
		<dc:creator>Matt Aimonetti</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[scalability]]></category>

		<guid isPermaLink="false">http://merbist.com/?p=1124</guid>
		<description><![CDATA[Recently I wrote a small DSL that allows the user to define some code that then gets executed later on and in different contexts. Imagine something like Sinatra where each route action is defined in a block and then executed in context of an incoming request. The challenge is that blocks come with their context and [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I wrote a small DSL that allows the user to define some code that then gets executed later on and in different contexts. Imagine something like Sinatra where each route action is defined in a block and then executed in context of an incoming request.</p>
<p>The challenge is that blocks come with their context and you can&#8217;t execute a block in the context of another one.</p>
<p>Here is a reduction of the challenge I was trying to solve:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> SolutionZero
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>origin, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@origin</span> = origin
    <span style="color:#0066ff; font-weight:bold;">@block</span> = block
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> dispatch
    <span style="color:#0066ff; font-weight:bold;">@block</span>.<span style="color:#9900CC;">call</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
SolutionZero.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">42</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#0066ff; font-weight:bold;">@origin</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">dispatch</span>
<span style="color:#008000; font-style:italic;"># undefined method `+' for nil:NilClass (NoMethodError)</span></pre></div></div>

<p>The problem is that the block refers to the @origin instance variable which is not available in its context.<br />
My first workaround was to use instance_eval:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> SolutionOne
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>origin, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@origin</span> = origin
    <span style="color:#0066ff; font-weight:bold;">@block</span> = block
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> dispatch
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">instance_eval</span> <span style="color:#006600; font-weight:bold;">&amp;</span>@block
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
SolutionOne.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">40</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#0066ff; font-weight:bold;">@origin</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">dispatch</span>
<span style="color:#008000; font-style:italic;"># 42</span></pre></div></div>

<p>My workaround worked fine, since the block was evaluated in the context of the instance and therefore the @origin ivar is made available to block context. Technically, I was good to go, but I wasn&#8217;t really pleased with this solution. First using instance_eval often an indication that you are trying to take a shortcut. Then having to convert my block stored as a block back into a proc every single dispatch makes me sad. Finally, I think that this code is probably not performing as well as it could, mainly due to unnecessary object allocations and code evaluation.<br />
I did some benchmarks replacing <a title="instance_eval" href="https://github.com/ruby/ruby/blob/trunk/vm_eval.c#L1323" target="_blank">instance_eval</a> by <a title="instance_exec" href="https://github.com/ruby/ruby/blob/trunk/vm_eval.c#L1355" target="_blank">instance_exec</a> since looking at the C code, instance_exec should be slightly faster. Turns out, it is not so I probably missed something when reading the implementation code.</p>
<p>I wrote some more benchmarks and profiled a loop of 2 million dispatches (only the #disptach method call on the same object). The GC profiler report showed that the GC was invoked 287 times and each invocation was blocking the execution for about 0.15ms.<br />
Using Ruby&#8217;s <a href="http://ruby-doc.org/core/classes/ObjectSpace.html#M001526" target="_blank">ObjectSpace</a> and <a href="http://ruby-doc.org/core/classes/GC.html#M001373" target="_blank">disabling the GC</a> during the benchmark, I could see that each loop allocates an object of type T_NODE which is more than likely our @block ivar converted back into a block. This is quite a waste. Furthermore, having to evaluate our block in a different context every single call surely isn&#8217;t good for performance.</p>
<p>So instead of doing the work at run time, why not doing it at load time? By that I mean that we can optimize the #dispatch method if we could &#8220;precompile&#8221; the method body instead of &#8220;proxying&#8221; the dispatch to an instance_eval call. Here is the code:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> SolutionTwo
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>origin, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@origin</span> = origin
    implementation<span style="color:#006600; font-weight:bold;">&#40;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  private
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> implementation<span style="color:#006600; font-weight:bold;">&#40;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    mod = <span style="color:#9966CC; font-weight:bold;">Module</span>.<span style="color:#9900CC;">new</span>
    mod.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:define_method</span>, <span style="color:#ff3333; font-weight:bold;">:dispatch</span>, block<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">extend</span> mod
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
SolutionTwo.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">40</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#0066ff; font-weight:bold;">@origin</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">dispatch</span>
<span style="color:#008000; font-style:italic;"># 42</span></pre></div></div>

<p>This optimization is based on the fact that the benchmark (and the real life usage) creates the instance once and then calls #dispatch many times. So by making the initialization of our instance a bit slower, we can drastically improve the performance of the method call. We also still need to execute our block in the right context. And finally, each instance might have a different way to dispatch since it is defined dynamically at initialization. To work around all these issues, we create a new module on which we define a new method called dispatch and the body of this method is the passed block. Then we simply our instance using our new module.</p>
<p>Now every time we call #dispatch, a real method is dispatched which is much faster than doing an eval and no objects are allocated. Running the profiler and the benchmarks script used earlier, we can confirm that the GC doesn&#8217;t run a single time and that the optimized code runs 2X faster!</p>
<p>&nbsp;</p>
<p>Once again, it&#8217;s yet another example showing that you <a href="http://merbist.com/2010/07/29/object-allocation-why-you-should-care/" target="_blank">should care about object allocation</a> when dealing with code in the critical path. It also shows how to work around the block bindings. Now, it doesn&#8217;t mean that you have to obsess about object allocation and performance, even if my last implementation is 2X faster than the previous, we are only talking about a few microseconds per dispatch. That said microseconds do add up and creating too many objects will slow down even your faster code since the GC will stop-the-world as its cleaning up your memory. In real life, you probably don&#8217;t have to worry too much about low level details like that, unless you are working on a framework or sharing your code with others. But at least you can learn and understand why one approach is faster than the other, it might not be useful to you right away, but if you take programming as a craft, it&#8217;s good to understand how things work under the hood so you can make educated decisions.<br />
&nbsp;</p>
<h3>Update:</h3>
<p>@apeiros in the comments suggested a solution that works &#038; performs the same as my solution, but is much cleaner:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> SolutionTwo
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>origin, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@origin</span> = origin
    define_singleton_method<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:dispatch</span>, block<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> block_given?
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://merbist.com/2011/09/05/ruby-optimization-example-and-explaination/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>News from the front line &#8211; Sept 24 2008</title>
		<link>http://merbist.com/2008/09/24/news-from-the-front-line-sept-24-2008/</link>
		<comments>http://merbist.com/2008/09/24/news-from-the-front-line-sept-24-2008/#comments</comments>
		<pubDate>Wed, 24 Sep 2008 16:47:24 +0000</pubDate>
		<dc:creator>Matt Aimonetti</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[bundling]]></category>
		<category><![CDATA[edge]]></category>
		<category><![CDATA[head]]></category>
		<category><![CDATA[merb]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[router]]></category>

		<guid isPermaLink="false">http://merbist.com/?p=36</guid>
		<description><![CDATA[Dear Merbivores/Merbists/Merbians, It&#8217;s hard to believe that in less that 20 days, Merb 1.0 will be released! We are all really happy to to be almost there but we have to be honest and admit that we are also under pressure. We are all dreaming of a post 1.0 world but in the meantime we [...]]]></description>
			<content:encoded><![CDATA[<p>Dear Merbivores/Merbists/Merbians,</p>
<p>It&#8217;s hard to believe that in <a title="merbcamp" href="http://merbcamp.com" target="_blank">less that 20 days</a>, Merb 1.0 will be released! We are all really happy to to be almost there but we have to be honest and admit that we are also under pressure.</p>
<p>We are all dreaming of a post 1.0 world but in the meantime we have to focus on last minutes bugs and optimization.</p>
<p><a href="http://flickr.com/photos/16596714@N00/355242291/"><img class="alignleft" style="border: 2px solid black; margin: 20px;" title="News from the front" src="http://farm1.static.flickr.com/128/355242291_40cf729cd9_m.jpg" alt="" width="240" height="180" /></a></p>
<p>During the last week or so, we made a lot of progress, the API is now &#8220;almost&#8221; frozen and <a title="Yehuda Katz" href="http://yehudakatz.com/" target="_blank">General Katz</a> is focusing on making sure everything will be fine for D Day.</p>
<p>That reminds me that Katz showed me something amazing yesterday! I shouldn&#8217;t really talk about it but I&#8217;m sure it will stay between us. He was been working on optimizing the general memory consumption and my testing app (real app) went from 120MB of Private Memory used, to 70MB (using 4 processes). I can&#8217;t wait to use that on the field. I also hope my old Rails comrades will realize that running ~100Mb processes (x4) really isn&#8217;t efficient and event dangerous for the free Ruby world!</p>
<p>I also heard rumors that the higher officers are now using a new strategic tool called <a title="PIvotaltracker" href="http://www.pivotaltracker.com" target="_blank">http://www.pivotaltracker.com</a> which should help us streamline the process. We are still using <a title="lighthouse" href="http://merb.lighthouseapp.com" target="_blank">LightHouse</a> to track bugs and patches though. I&#8217;m not sure if this new &#8220;agile&#8221; tool will help, but I thought the approach is pretty interesting. What do you think?</p>
<p><a href="http://flickr.com/photos/celtico/2556999427/"><img class="alignright" style="margin: 20px;" title="Captain at the gym" src="http://farm4.static.flickr.com/3138/2556999427_546c5004f6_m.jpg" alt="" width="184" height="240" /></a></p>
<p>You probably also saw my early report on <a title="bundling a merb app" href="http://merbist.com/2008/09/23/deploying-a-bundled-merb-app-merb-097/" target="_blank">bundling Merb apps</a>, I&#8217;m quite happy about the process. Do you think you will deploy bundled/frozen apps or just use the system-wide gems?</p>
<p><a href="http://farm4.static.flickr.com/3097/2884670329_15385e8516.jpg"><br />
</a></p>
<p>Finally I hear a lot of talk about the <a href="http://github.com/carllerche/merb-core-enterprise-edition/wikis/whats-new-with-the-router" target="_blank">new Route</a>r that&#8217;s available on <a title="merb HEAD" href="http://github.com/wycats/merb-core/tree/master" target="_blank">Edge/HEAD</a>. Officer Lerch wrote a <a title="new merb router" href="http://github.com/carllerche/merb-core-enterprise-edition/wikis/whats-new-with-the-router" target="_blank">nice wiki article</a> covering the changes, you might want to <a title="new merb router" href="http://github.com/carllerche/merb-core-enterprise-edition/wikis/whats-new-with-the-router" target="_blank">read it</a>.</p>
<p>Ohh before I forget, some courageous privates went to <a href="http://github.com/wycats/merb-core/tree/master" target="_blank">HEAD</a> and use the 3rd party plugin called <a title="merb auth" href="http://github.com/hassox/merb-auth/tree/master" target="_blank">merb-auth</a>. What they don&#8217;t know is that they need to change their routes to use the slices with the new router. (the new router requires no block variable) Also, if they look at the merb-auth branches they will notice a new <a title="mauth" href="http://github.com/hassox/merb-auth/tree/mauth" target="_blank">mauth branch</a> which is the new version of merb-auth, even better, more flexible than the previous version.</p>
<p>I hope everything is well for you, say Hi! to our friends for me.</p>
<p>- Merbist</p>
]]></content:encoded>
			<wfw:commentRss>http://merbist.com/2008/09/24/news-from-the-front-line-sept-24-2008/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

