<?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>Floorplanner Tech Blog &#187; Ruby on Rails</title>
	<atom:link href="http://techblog.floorplanner.com/category/ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://techblog.floorplanner.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Fri, 05 Mar 2010 20:29:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>RESTful uploading of files using XML</title>
		<link>http://techblog.floorplanner.com/2010/02/15/restful-uploading-of-files-using-xml/</link>
		<comments>http://techblog.floorplanner.com/2010/02/15/restful-uploading-of-files-using-xml/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 11:34:31 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[embed]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[upload]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=940</guid>
		<description><![CDATA[
			
				
			
		
After searching for too long to find any documentation on this topic, this is just as a reminder for myself. Hopefully, it&#8217;ll solve somebody else&#8217;s problem as well.
To send a file to a Rails application, using a RESTful XML API call, use the following XML snippet:

&#60;field type=&#34;file&#34; name=&#34;filename.ext&#34; content-type=&#34;mime/type&#34;&#62;
  base64-encoded file contents
&#60;/field&#62;

The file contents [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2010%2F02%2F15%2Frestful-uploading-of-files-using-xml%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2010%2F02%2F15%2Frestful-uploading-of-files-using-xml%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>After searching for too long to find any documentation on this topic, this is just as a reminder for myself. Hopefully, it&#8217;ll solve somebody else&#8217;s problem as well.</p>
<p>To send a file to a Rails application, using a RESTful XML API call, use the following XML snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family: Monaco,monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;field</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;filename.ext&quot;</span> <span style="color: #000066;">content-type</span>=<span style="color: #ff0000;">&quot;mime/type&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  base64-encoded file contents
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/field<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The file contents should be base64 encoded, and may be encapsulated in a CDATA block:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family: Monaco,monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;greeting</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;greeting.txt&quot;</span> <span style="color: #000066;">content-type</span>=<span style="color: #ff0000;">&quot;text/plain&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #339933;">&lt;![CDATA[SGVsbG8gd29ybGQh]]&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/greeting<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>You can test this by opening a console and feed the XML to <code>Hash.from_xml</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family: Monaco,monospace;">&gt;&gt; greeting = &lt;&lt;-EOXML
    &lt;greeting type=&quot;file&quot; name=&quot;greeting.txt&quot; content-type=&quot;text/plain&quot;&gt;
      &lt;![CDATA[SGVsbG8gd29ybGQh]]&gt;
    &lt;/greeting&gt;
  EOXML
&nbsp;
&gt;&gt; data = Hash.from_xml(greeting)
=&gt; {&quot;greeting&quot;=&gt;#&lt;StringIO:0x10617d9d0&gt;}
&gt;&gt; data['greeting'].content_type
=&gt; &quot;text/plain&quot;
&gt;&gt; data['greeting'].original_filename
=&gt; &quot;greeting.txt&quot;
&gt;&gt; data['greeting'].read
=&gt; &quot;Hello world!&quot;</pre></div></div>

<p>Now you can handle files using your XML API just like you would use file uploads in your HTML forms.</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2010/02/15/restful-uploading-of-files-using-xml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Two practical examples of Rack middleware in Rails</title>
		<link>http://techblog.floorplanner.com/2010/01/29/two-practical-examples-of-rack-middleware-in-rails/</link>
		<comments>http://techblog.floorplanner.com/2010/01/29/two-practical-examples-of-rack-middleware-in-rails/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 08:38:30 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[examples]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[middleware]]></category>
		<category><![CDATA[rack]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[redirect]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=824</guid>
		<description><![CDATA[
			
				
			
		
After Rails moved to Rack as server interface, the ability the use Rack middleware was one of the most touted advantages. At first, it wasn&#8217;t very clear to me why this was such a big deal. However, I have applied Rack middleware in the last month on several occasions. I thought it might be interesting [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2010%2F01%2F29%2Ftwo-practical-examples-of-rack-middleware-in-rails%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2010%2F01%2F29%2Ftwo-practical-examples-of-rack-middleware-in-rails%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>After Rails moved to Rack as server interface, the ability the use Rack middleware was one of the most touted advantages. At first, it wasn&#8217;t very clear to me why this was such a big deal. However, I have applied Rack middleware in the last month on several occasions. I thought it might be interesting for other Rails developers to see some practical examples of middleware, to see where they can be applied.</p>
<h3> Redirecting</h3>
<p>After a complete rewrite, we <a href="http://floorplanner.com/weblog/new-version-goes-live">released the new version of Floorplanner over a year ago</a>. We had to find a way to deal with the thousands of links that were in use by our customers and indexed by Google. </p>
<p>Some of these could simply redirect to their new location, e.g. <a rel="nofollow" href="http://www.floorplanner.com/tryit">http://www.floorplanner.com/tryit</a> can now be found on <a href="http://www.floorplanner.com/demo">http://www.floorplanner.com/demo</a>. These redirects can of course simply be implemented in either Rails or Apache. </p>
<p>However, we also had a long list of URLs on which floor plans were published using the old version. These plans cannot be migrated to the new version easily, so we had to keep an instance of the old Rails application running. Requests to these URLs should be redirected to this separate instance, now hosted on a different domain name. Moreover, it is important that it only tries to redirect to the old version if the URL cannot be resolved in the new version, because the plan may have been migrated to or recreated on the new version.</p>
<p>We previously implemented this by adding a catch all route to our application, and a custom handler for the <code>ActiveRecord::RecordNotFound</code> exception in our application. This was a rather ugly solution, and we rather not have a dependency on the old version&#8217;s database in the new version&#8217;s code. </p>
<p>I decided to rewrite this functionality with a piece of Rack middleware. It basically executes every request with our Rails application, and if it returns a 404 response, it checks if the URL exists on the old version. If so, it redirects the user to the old version, otherwise, it will respond with the 404 response that was generated by Rails.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> RedirectToOldVersionMiddleware
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>app<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@app</span> = app
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> call<span style="color:#006600; font-weight:bold;">&#40;</span>env<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;"># execute the request using our Rails app</span>
   status, headers, body = <span style="color:#0066ff; font-weight:bold;">@app</span>.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span>env<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">if</span> status == <span style="color:#006666;">404</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> url = find_redirect<span style="color:#006600; font-weight:bold;">&#40;</span>env<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'REQUEST_URI'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
      <span style="color:#008000; font-style:italic;"># Issue a &quot;Moved permanently&quot; response with the redirect location</span>
      <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">301</span>, <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#996600;">&quot;Location&quot;</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> url<span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#996600;">'Redirecting you to the new location...'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
&nbsp;
      <span style="color:#008000; font-style:italic;"># Not a 404 or no redirect found, just send the response as is</span>
      <span style="color:#006600; font-weight:bold;">&#91;</span>status, headers, body<span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  REDIRECT_HOST = <span style="color:#996600;">'http://old.floorplanner.version'</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> find_redirect<span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;"># See if we can find a valid URL on the old version.</span>
    Redirect.<span style="color:#9900CC;">exists</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span> ? <span style="color:#996600;">&quot;#{REDIRECT_HOST}#{path}&quot;</span> : <span style="color:#0000FF; font-weight:bold;">nil</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This way, all the code that is related to redirecting users to the old version is contained in a single file, and doesn&#8217;t pollute our application anymore. Moreover, when all the old plans are migrated, or we decide we do not longer support these old plans, we can simply disable the middleware in our <code>environment.rb</code>, without having to alter any code.</p>
<h3> Logging complete requests for debugging purposes </h3>
<p>Another use for Rack middleware is logging: one of our customers notified us that an API call they were using was failing, but only some of the time. Unfortunately, we did not find any errors in our exception log, which made it very hard to debug this issue. After some time, we discovered that Rails was returning a 422 response, without the request ever &#8220;arriving&#8221; in our own code. Apparently, the request was stopped from processing somewhere in the Rails framework.</p>
<p>Unfortunately, we were not able to recreate this issue ourselves, and we started suspecting that the request was not well-formed, because Rails sends a 422 response in this case. To discover if this really was the issue, I wrote a piece of middleware that logs the complete request, before it is passed to the Rails framework for further processing.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> RequestLoggerMiddleware
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>app<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@app</span> = app
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  REQUEST_LOG_DIR = Rails.<span style="color:#9900CC;">root</span>.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'log'</span>, <span style="color:#996600;">'api_calls'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> call<span style="color:#006600; font-weight:bold;">&#40;</span>env<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#008000; font-style:italic;"># log the request if it is a troublesome API call.</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> env<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'REQUEST_URI'</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#996600;">'/failing_api.xml'</span>
&nbsp;
      filename = <span style="color:#996600;">&quot;api_call_#{Time.now.strftime('%Y%m%d%H%M%S')}.log&quot;</span>
      <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>REQUEST_LOG_DIR.<span style="color:#9900CC;">join</span><span style="color:#006600; font-weight:bold;">&#40;</span>filename<span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#996600;">'wb'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>file<span style="color:#006600; font-weight:bold;">|</span>
        file.<span style="color:#CC0066; font-weight:bold;">puts</span><span style="color:#006600; font-weight:bold;">&#40;</span>env<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'rack.input'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>      
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># now, execute the request using our Rails app</span>
    response = <span style="color:#0066ff; font-weight:bold;">@app</span>.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span>env<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Using these request log files, API calls can be inspected exactly before any of the Rails magic happens. After resolving the issue, removing this logging was as simple as commenting a line in <code>environment.rb</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2010/01/29/two-practical-examples-of-rack-middleware-in-rails/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Ode to Array#pack and String#unpack</title>
		<link>http://techblog.floorplanner.com/2010/01/17/ode-to-array-pack-and-string-unpack/</link>
		<comments>http://techblog.floorplanner.com/2010/01/17/ode-to-array-pack-and-string-unpack/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 12:49:41 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[chunky_png]]></category>
		<category><![CDATA[decoding]]></category>
		<category><![CDATA[encoding]]></category>
		<category><![CDATA[integer math]]></category>
		<category><![CDATA[pack]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[png]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[unpack]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=861</guid>
		<description><![CDATA[
			
				
			
		
Remember my last post, where I representing a pixel with a Fixnum, storing the R, G, B and A value in its 4 bytes of memory? Well, I have been working some more on my PNG library and I am now trying loading and saving an image.
Using the PNG specification, building a PNG encoder/decoder isn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2010%2F01%2F17%2Fode-to-array-pack-and-string-unpack%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2010%2F01%2F17%2Fode-to-array-pack-and-string-unpack%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Remember <a href="http://techblog.floorplanner.com/2010/01/14/memory-efficiency-when-using-ruby/">my last post</a>, where I representing a pixel with a Fixnum, storing the R, G, B and A value in its 4 bytes of memory? Well, I have been working some more on <a href="http://github.com/wvanbergen/chunky_png">my PNG library</a> and I am now trying loading and saving an image.</p>
<p>Using <a href="http://www.w3.org/TR/PNG/">the PNG specification</a>, building a PNG encoder/decoder isn&#8217;t that hard, but the required algorithmic calculations make sure that performance in Ruby is <em>less than stellar</em>. I have rewritten all calculations to only use fast integer math (plus, minus, multiply and bitwise operators), but simply the amount of code that is getting executed is slowing Ruby down. What more can I do to improve the performance?</p>
<h3>Encoding RGBA images</h3>
<p>Optimizing loading images is very hard, because PNG images can have many variations, and taking shortcuts means that some images are no longer supported. Not so with saving images: as long an image is saved using one of the valid variations, every PNG decoder will be able to read the file. Let&#8217;s see if it is possible to optimize one of these encoding variations.</p>
<p>During encoding, the image get splits up into scanlines (rows) of pixels, which in turn get converted into bytes. These bytes can be filtered for optimal compression. For a 3&#215;3 8-bit RGBA image, the result looks like this:</p>
<pre>F R<sub>f</sub> G<sub>f</sub> B<sub>f</sub> A<sub>f</sub> R<sub>f</sub> G<sub>f</sub> B<sub>f</sub> A<sub>f</sub> R<sub>f</sub> G<sub>f</sub> B<sub>f</sub> A<sub>f</sub>
F R<sub>f</sub> G<sub>f</sub> B<sub>f</sub> A<sub>f</sub> R<sub>f</sub> G<sub>f</sub> B<sub>f</sub> A<sub>f</sub> R<sub>f</sub> G<sub>f</sub> B<sub>f</sub> A<sub>f</sub>
F R<sub>f</sub> G<sub>f</sub> B<sub>f</sub> A<sub>f</sub> R<sub>f</sub> G<sub>f</sub> B<sub>f</sub> A<sub>f</sub> R<sub>f</sub> G<sub>f</sub> B<sub>f</sub> A<sub>f</sub></pre>
<p>Every line starts with a byte <em>F</em> indicating the filter method, followed by the filtered R, G and B value for every pixel on that line. Now, if we choose filter method 0, which means no filtering, the result looks like this:</p>
<pre>0 R<sub>o</sub> G<sub>o</sub> B<sub>o</sub> A<sub>o</sub> R<sub>o</sub> G<sub>o</sub> B<sub>o</sub> A<sub>o</sub> R<sub>o</sub> G<sub>o</sub> B<sub>o</sub> A<sub>o</sub>
0 R<sub>o</sub> G<sub>o</sub> B<sub>o</sub> A<sub>o</sub> R<sub>o</sub> G<sub>o</sub> B<sub>o</sub> A<sub>o</sub> R<sub>o</sub> G<sub>o</sub> B<sub>o</sub> A<sub>o</sub>
0 R<sub>o</sub> G<sub>o</sub> B<sub>o</sub> A<sub>o</sub> R<sub>o</sub> G<sub>o</sub> B<sub>o</sub> A<sub>o</sub> R<sub>o</sub> G<sub>o</sub> B<sub>o</sub> A<sub>o</sub></pre>
<p>Now, the original R, G, B and A byte from the original pixel&#8217;s Fixnum, occur in <a href="http://en.wikipedia.org/wiki/Endianness">big-endian or network byte order</a>, starting with the top left pixel, moving left to right and then top to bottom. Exactly like the pixels are stored in our image&#8217;s pixel array! This means that we can use the <code>Array#pack</code> method to encode into this format. The <code>Array#pack</code>-notation for this is <code>"xN3"</code> in which <em>x</em> get translated into a null byte, and every <em>N</em> as 4-byte integer in network byte order. For optimal performance, it is best to not split the original array in lines, but to pack the complete pixel array at once. So, we can encode all pixels with this command:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">pixeldata = pixels.<span style="color:#9900CC;">pack</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;xN#{width}&quot;</span> <span style="color:#006600; font-weight:bold;">*</span> height<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>This way, the splitting the image into lines, splitting the pixels into bytes, and filtering the bytes can be skipped. In Ruby 1.8.7, this means a speedup of over 1500% (no typo)! Of course, because no filtering applied, the subsequent compression is not optimal, but that is a tradeoff that I am willing to make.</p>
<h3>Encoding RGB images</h3>
<p>What about RGB images without alpha channel? We can simply choose to encode these using the RGBA method, but that increases the file size with roughly 25%. Can we fix this somehow?</p>
<p>The unfiltered pixel data should look something like this:</p>
<pre>0 R<sub>o</sub> G<sub>o</sub> B<sub>o</sub> R<sub>o</sub> G<sub>o</sub> B<sub>o</sub> R<sub>o</sub> G<sub>o</sub> B<sub>o</sub>
0 R<sub>o</sub> G<sub>o</sub> B<sub>o</sub> R<sub>o</sub> G<sub>o</sub> B<sub>o</sub> R<sub>o</sub> G<sub>o</sub> B<sub>o</sub>
0 R<sub>o</sub> G<sub>o</sub> B<sub>o</sub> R<sub>o</sub> G<sub>o</sub> B<sub>o</sub> R<sub>o</sub> G<sub>o</sub> B<sub>o</sub></pre>
<p>This means that for every pixel that is encoded as a 4-byte integer, the last byte should be ditched. Luckily, the <code>Array#pack</code> method offers a modifier that does just that: <em>X</em>. Packing a 3 pixel line can be done with <code>"xNXNXNX"</code>. Again we would like to pack the whole pixel array at once:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">pixeldata = pixels.<span style="color:#9900CC;">pack</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;x&quot;</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'NX'</span> <span style="color:#006600; font-weight:bold;">*</span> width<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">*</span> height<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Because all the encoding steps can get skipped once again, the speed improvement is again 1500%! And the result is 25% smaller than the RGBA method. This method is actually so speedy, that saving an image using Ruby 1.9.1 is only a little bit slower (< 10%) than saving a PNG image using RMagick! See <a href="http://wiki.github.com/wvanbergen/chunky_png/performance-comparison">my performance comparison</a>.</p>
<h3> Loading images </h3>
<p>Given the promising results of the <code>Array#pack</code> method, using its counterpart <code>String#unpack</code> looks promising for speedy image loading, if you know the image&#8217;s size and the encoding format beforehand.</p>
<p>An RGBA formatted stream can be loaded quickly with this command:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">pixels = rgba_pixeldata.<span style="color:#9900CC;">unpack</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;N#{width * height}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
image = Image.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>width, height, pixels<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>For an RGB formatted stream, we can use the <em>X</em> modifier again, but we have to make sure to set the alpha value for every pixel to 255:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">pixels = rgb_pixeldata.<span style="color:#9900CC;">unpack</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;NX&quot;</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006600; font-weight:bold;">&#40;</span>width <span style="color:#006600; font-weight:bold;">*</span> height<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
pixels.<span style="color:#9900CC;">map</span>! <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>pixel<span style="color:#006600; font-weight:bold;">|</span> pixel <span style="color:#006600; font-weight:bold;">|</span> 0x000000ff <span style="color:#006600; font-weight:bold;">&#125;</span>
image = Image.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>width, height, pixels<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>You can even use little-endian integers to load streams in ABGR format!</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">pixels = abgr_pixeldata.<span style="color:#9900CC;">unpack</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;V#{width * height}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
image = Image.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>width, height, pixels<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Loading pixel data for an image like this is again over 1500% faster than decoding the same PNG image. However, this can only be applied if you have control over the input format of the image.</p>
<h3>To conclude</h3>
<p><code>Array#pack</code> and <code>String#unpack</code> really have increased the performance for my code. If you can apply them for project, don&#8217;t hesitate and spread the love! For all other cases, use as little code as possible, and upgrade to Ruby 1.9 for improved algorithmic performance. <img src='http://techblog.floorplanner.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2010/01/17/ode-to-array-pack-and-string-unpack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Memory efficiency when using Ruby</title>
		<link>http://techblog.floorplanner.com/2010/01/14/memory-efficiency-when-using-ruby/</link>
		<comments>http://techblog.floorplanner.com/2010/01/14/memory-efficiency-when-using-ruby/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 11:45:47 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[alpha composition]]></category>
		<category><![CDATA[bitwise operations]]></category>
		<category><![CDATA[efficiency]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[integer math]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[pixel]]></category>
		<category><![CDATA[rgb]]></category>
		<category><![CDATA[rgba]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=831</guid>
		<description><![CDATA[
			
				
			
		
I have been spending some time creating a pure Ruby PNG library. For this library, I need to have some representation of the image, which is composed of RGB pixels, supporting an alpha channel. Because images can be composed of a lot of pixels, I want the implementation to be as memory efficient as possible. [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2010%2F01%2F14%2Fmemory-efficiency-when-using-ruby%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2010%2F01%2F14%2Fmemory-efficiency-when-using-ruby%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>I have been spending some time creating a <a title="ChunkPNG, a pure ruby PNG library" href="http://github.com/wvanbergen/chunky_png">pure Ruby PNG library</a>. For this library, I need to have some representation of the image, which is composed of RGB pixels, supporting an alpha channel. Because images can be composed of a lot of pixels, I want the implementation to be as memory efficient as possible. I also would like decent performance.</p>
<p>A very naive Ruby implementation for an image represents the <em>red</em>, <em>green</em>, <em>blue</em> and <em>alpha</em> channel using a floating point number between 0.0 and 1.0, and might look something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Pixel
  attr_reader <span style="color:#ff3333; font-weight:bold;">:r</span>, <span style="color:#ff3333; font-weight:bold;">:g</span>, <span style="color:#ff3333; font-weight:bold;">:b</span>, <span style="color:#ff3333; font-weight:bold;">:a</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>r, g, b, a = <span style="color:#006666;">1.0</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@r</span>, <span style="color:#0066ff; font-weight:bold;">@g</span>, <span style="color:#0066ff; font-weight:bold;">@b</span>, <span style="color:#0066ff; font-weight:bold;">@a</span> = r, g, b, a
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Image
  attr_reader <span style="color:#ff3333; font-weight:bold;">:width</span>, <span style="color:#ff3333; font-weight:bold;">:height</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>width, height<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@width</span>, <span style="color:#0066ff; font-weight:bold;">@height</span> = width, height
    <span style="color:#0066ff; font-weight:bold;">@pixels</span> = <span style="color:#CC0066; font-weight:bold;">Array</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>width <span style="color:#006600; font-weight:bold;">*</span> height<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#40;</span>x,y<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@pixels</span><span style="color:#006600; font-weight:bold;">&#91;</span>y <span style="color:#006600; font-weight:bold;">*</span> width <span style="color:#006600; font-weight:bold;">+</span> x<span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>=<span style="color:#006600; font-weight:bold;">&#40;</span>x,y, pixel<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@pixels</span><span style="color:#006600; font-weight:bold;">&#91;</span>y <span style="color:#006600; font-weight:bold;">*</span> width <span style="color:#006600; font-weight:bold;">+</span> x<span style="color:#006600; font-weight:bold;">&#93;</span> = pixel
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>For a 10&#215;10 image, this representation requires 4 times 100 floating point numbers, which require 8 bytes each. That&#8217;s already over 3kB for such a small image just for the floating point numbers! Ouch.</p>
<p>A simple improvement is to decide that 8-bit color depth is enough in the case, in which case each channel can be represented by an integer between 0 and 255. Storing such a number only costs one byte of memory. Ruby&#8217;s Fixnum class typically uses 4-byte integers. If only the 4 channels of one byte each could be combined into a single Fixnum instance&#8230; Behold!</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Pixel
  attr_reader <span style="color:#ff3333; font-weight:bold;">:value</span>
  <span style="color:#9966CC; font-weight:bold;">alias</span> <span style="color:#ff3333; font-weight:bold;">:to_i</span> <span style="color:#ff3333; font-weight:bold;">:value</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>value<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@value</span> = value
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">rgba</span><span style="color:#006600; font-weight:bold;">&#40;</span>r, g, b, a = <span style="color:#006666;">255</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>r <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006666;">24</span> <span style="color:#006600; font-weight:bold;">|</span> g <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006666;">16</span> <span style="color:#006600; font-weight:bold;">|</span> b <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#006666;">8</span> <span style="color:#006600; font-weight:bold;">|</span> a<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> r; <span style="color:#006600; font-weight:bold;">&#40;</span>@value <span style="color:#006600; font-weight:bold;">&amp;</span> 0xff000000<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&gt;&gt;</span> <span style="color:#006666;">24</span>; <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> g; <span style="color:#006600; font-weight:bold;">&#40;</span>@value <span style="color:#006600; font-weight:bold;">&amp;</span> 0x00ff0000<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&gt;&gt;</span> <span style="color:#006666;">16</span>; <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> b; <span style="color:#006600; font-weight:bold;">&#40;</span>@value <span style="color:#006600; font-weight:bold;">&amp;</span> 0x0000ff00<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&gt;&gt;</span>  <span style="color:#006666;">8</span>; <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> a; <span style="color:#006600; font-weight:bold;">&#40;</span>@value <span style="color:#006600; font-weight:bold;">&amp;</span> 0x000000ff<span style="color:#006600; font-weight:bold;">&#41;</span>; <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Notice the bit operations, which are extremely fast. This only requires 100 times 4 bytes = 400 bytes for storing the RGBA values for a 10&#215;10 image, an 8 times improvement!</p>
<p>This implementation wraps every pixel inside an object. This is nice, because I want to access  the separate channels of every pixel easily using the <code>r</code>, <code>g</code>, <code>b</code>, and <code>a</code> methods, and every other method that is defined for every pixel. However, a Ruby object instance has <a href="http://eigenclass.org/hiki/ruby+space+overhead">an overhead of at least 20 bytes</a>. That&#8217;s 20 times 100 is about 2kB for our 10&#215;10 image!</p>
<p>To get rid of the object overhead, it is possible to simply store the Fixnum value for every pixel, and only wrapping it inside a Pixel object when it is accessed. This can be done by modifying the <code>Image</code> class:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Image
  <span style="color:#008000; font-style:italic;"># ...</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#40;</span>x,y<span style="color:#006600; font-weight:bold;">&#41;</span>
    Pixel.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>@pixels<span style="color:#006600; font-weight:bold;">&#91;</span>y <span style="color:#006600; font-weight:bold;">*</span> width <span style="color:#006600; font-weight:bold;">+</span> x<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># wrap</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#93;</span>=<span style="color:#006600; font-weight:bold;">&#40;</span>x,y, pixel<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@pixels</span><span style="color:#006600; font-weight:bold;">&#91;</span>y <span style="color:#006600; font-weight:bold;">*</span> width <span style="color:#006600; font-weight:bold;">+</span> x<span style="color:#006600; font-weight:bold;">&#93;</span> = pixel.<span style="color:#9900CC;">to_i</span> <span style="color:#008000; font-style:italic;"># unwrap</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>As you can see, some simply changes in the representation can really make a difference in the memory usage. Can this representation be improved further?</p>
<h3> Integer math calculations </h3>
<p>Because we are now using integers to represent a pixel, this can cause problems when the math requires you to use floating point numbers. For example, the formula for <a href="http://en.wikipedia.org/wiki/Alpha_compositing">alpha composition</a> of two pixels is as follows:</p>
<p><img src="http://upload.wikimedia.org/math/3/c/3/3c377902304f3e4c105ad360abbbc180.png" alt="Alpha compositing formula" /></p>
<blockquote><p>in which <em>C<sub>a</sub></em> is the color component of the foreground pixel, <em>α<sub>a</sub></em> the alpha channel of the foreground pixel, <em>C<sub>b</sub></em> and <em>α<sub>b</sub></em> the same values for the background pixel, all of which should be values between 0 and 1. </p></blockquote>
<p>A naive implementation could convert the integer numbers to their floating point equivalents:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> compose<span style="color:#006600; font-weight:bold;">&#40;</span>fg, bg<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#0000FF; font-weight:bold;">return</span> bg <span style="color:#9966CC; font-weight:bold;">if</span> fg.<span style="color:#9900CC;">a</span> == <span style="color:#006666;">0</span>
  <span style="color:#0000FF; font-weight:bold;">return</span> fg <span style="color:#9966CC; font-weight:bold;">if</span> fg.<span style="color:#9900CC;">a</span> == <span style="color:#006666;">255</span>
&nbsp;
  fg_alpha = fg.<span style="color:#9900CC;">a</span> <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#006666;">255.0</span>
  bg_alpha = fg.<span style="color:#9900CC;">a</span> <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#006666;">255.0</span>
  alpha_complement = <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1.0</span> <span style="color:#006600; font-weight:bold;">-</span> fg_alpha<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">*</span> bg_alpha
&nbsp;
  new_r = <span style="color:#006600; font-weight:bold;">&#40;</span>fg_alpha <span style="color:#006600; font-weight:bold;">*</span> fg.<span style="color:#9900CC;">r</span> <span style="color:#006600; font-weight:bold;">+</span> alpha_complement <span style="color:#006600; font-weight:bold;">*</span> bg.<span style="color:#9900CC;">r</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">round</span>
  new_g = <span style="color:#006600; font-weight:bold;">&#40;</span>fg_alpha <span style="color:#006600; font-weight:bold;">*</span> fg.<span style="color:#9900CC;">g</span> <span style="color:#006600; font-weight:bold;">+</span> alpha_complement <span style="color:#006600; font-weight:bold;">*</span> bg.<span style="color:#9900CC;">g</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">round</span>
  new_b = <span style="color:#006600; font-weight:bold;">&#40;</span>fg_alpha <span style="color:#006600; font-weight:bold;">*</span> fg.<span style="color:#9900CC;">b</span> <span style="color:#006600; font-weight:bold;">+</span> alpha_complement <span style="color:#006600; font-weight:bold;">*</span> bg.<span style="color:#9900CC;">b</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">round</span>
  new_a = <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>fg_alpha <span style="color:#006600; font-weight:bold;">+</span> alpha_complement<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">255</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">round</span>
&nbsp;
  Pixel.<span style="color:#9900CC;">rgba</span><span style="color:#006600; font-weight:bold;">&#40;</span>new_r, new_g, new_b, new_a<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This implementation is already a little bit optimized: no unnecessary conversions and calculations are being performed. However, this composition can be done a lot quicker after realizing that 255 is almost a power of two, in which computers excel because it can use bitwise operators and shifting for some calculations. </p>
<p>My new approach uses <a href="http://www.alvyray.com/memos/4_comp.pdf">a quicker implementation of multiplication of 8-bit integers</a> that represent floating numbers between 0 and 1:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> compose<span style="color:#006600; font-weight:bold;">&#40;</span>fg, bg<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#0000FF; font-weight:bold;">return</span> bg <span style="color:#9966CC; font-weight:bold;">if</span> fg.<span style="color:#9900CC;">a</span> == <span style="color:#006666;">0</span>
  <span style="color:#0000FF; font-weight:bold;">return</span> fg <span style="color:#9966CC; font-weight:bold;">if</span> fg.<span style="color:#9900CC;">a</span> == <span style="color:#006666;">255</span>
&nbsp;
  alpha_complement = multiply<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">255</span> <span style="color:#006600; font-weight:bold;">-</span> fg.<span style="color:#9900CC;">a</span>, bg.<span style="color:#9900CC;">a</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  new_r = multiply<span style="color:#006600; font-weight:bold;">&#40;</span>fg.<span style="color:#9900CC;">a</span>, fg.<span style="color:#9900CC;">r</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> multiply<span style="color:#006600; font-weight:bold;">&#40;</span>alpha_complement, bg.<span style="color:#9900CC;">r</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  new_g = multiply<span style="color:#006600; font-weight:bold;">&#40;</span>fg.<span style="color:#9900CC;">a</span>, fg.<span style="color:#9900CC;">g</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> multiply<span style="color:#006600; font-weight:bold;">&#40;</span>alpha_complement, bg.<span style="color:#9900CC;">g</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  new_b = multiply<span style="color:#006600; font-weight:bold;">&#40;</span>fg.<span style="color:#9900CC;">a</span>, fg.<span style="color:#9900CC;">b</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> multiply<span style="color:#006600; font-weight:bold;">&#40;</span>alpha_complement, bg.<span style="color:#9900CC;">b</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  new_a = fg.<span style="color:#9900CC;">a</span> <span style="color:#006600; font-weight:bold;">+</span> alpha_complement
&nbsp;
  Pixel.<span style="color:#9900CC;">rgba</span><span style="color:#006600; font-weight:bold;">&#40;</span>new_r, new_g, new_b, new_a<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Quicker alternative for (a * b / 255.0).round</span>
<span style="color:#9966CC; font-weight:bold;">def</span> multiply<span style="color:#006600; font-weight:bold;">&#40;</span>a, b<span style="color:#006600; font-weight:bold;">&#41;</span>
  t = a <span style="color:#006600; font-weight:bold;">*</span> b <span style="color:#006600; font-weight:bold;">+</span> 0x80
  <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>t <span style="color:#006600; font-weight:bold;">&gt;&gt;</span> <span style="color:#006666;">8</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span> t<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&gt;&gt;</span> <span style="color:#006666;">8</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Not that the new implementation is less precise in theory, but this precision is lost anyway because we have to convert the values back to 8 bit RGBA values. Your thoughts?</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2010/01/14/memory-efficiency-when-using-ruby/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Request-log-analyzer 1.6.0</title>
		<link>http://techblog.floorplanner.com/2010/01/08/request-log-analyzer-1-6-0/</link>
		<comments>http://techblog.floorplanner.com/2010/01/08/request-log-analyzer-1-6-0/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 13:56:28 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[delayed::job]]></category>
		<category><![CDATA[log analysis]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[release]]></category>
		<category><![CDATA[request-log-analyzer]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=828</guid>
		<description><![CDATA[
			
				
			
		
Bart &#38; I just released request-log-analyzer 1.6.0. New features since the 1.5.0 release:

PostgreSQL query log support;
Delayed::Job log support;
Small fixes in the Rails file format;
Various other small fixes and improvements.

As always, run the following command to install or upgrade to the latest version:

sudo gem install request-log-analyzer

]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2010%2F01%2F08%2Frequest-log-analyzer-1-6-0%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2010%2F01%2F08%2Frequest-log-analyzer-1-6-0%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Bart &amp; I just released <a href="http://wiki.github.com/wvanbergen/request-log-analyzer">request-log-analyzer</a> 1.6.0. New features since the 1.5.0 release:</p>
<ul>
<li>PostgreSQL query log support;</li>
<li>Delayed::Job log support;</li>
<li>Small fixes in the Rails file format;</li>
<li>Various other small fixes and improvements.</li>
</ul>
<p>As always, run the following command to install or upgrade to the latest version:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family: Monaco,monospace;">sudo gem install request-log-analyzer</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2010/01/08/request-log-analyzer-1-6-0/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Faster RESTful XML processing in Rails</title>
		<link>http://techblog.floorplanner.com/2010/01/03/faster-restful-xml-processing-in-rails/</link>
		<comments>http://techblog.floorplanner.com/2010/01/03/faster-restful-xml-processing-in-rails/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 19:29:09 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[libxml]]></category>
		<category><![CDATA[nokogiri]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[rexml]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=815</guid>
		<description><![CDATA[
			
				
			
		
The Floorplanner API uses XML-formatted requests and responses, so our servers process a lot of XML. In Rails, most XML parsing is done using the Hash.from_xml method. This method allows for different backends, but the current backends are either slow or buggy. I decided to fix this situation myself.
I fixed bugs in the current libxml [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2010%2F01%2F03%2Ffaster-restful-xml-processing-in-rails%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2010%2F01%2F03%2Ffaster-restful-xml-processing-in-rails%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>The Floorplanner API uses XML-formatted requests and responses, so our servers process a lot of XML. In Rails, most XML parsing is done using the <code>Hash.from_xml</code> method. This method allows for different backends, but the current backends are either slow or buggy. I decided to fix this situation myself.</p>
<p>I fixed bugs in <a href="https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/3641">the current libxml and nokogiri backends</a>, and I added some <a href="https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/3636">new SAX-based backends</a> for additional performance. <a href="http://github.com/rails/rails/commit/12f6fd0f2687f083bc23ad63fdc82c7e65cb8984">My</a> <a href="http://github.com/rails/rails/commit/37c51594b9610469173f3deee1ffdda4beb3e397">patches</a> <a href="http://github.com/rails/rails/commit/96a2b3905ce14df8f25b1646d3b110505bf8820b">are</a> <a href="http://github.com/rails/rails/commit/d7f9b9fd244228d3503d7d37ac2f07365d54df3c">already</a> <a href="http://github.com/rails/rails/commit/34b03cebf9c9f2ce2a53511a4b2160485e270f12">accepted</a> by the Rails team, so everybody will enjoy fast and bug-free XML parsing in Rails 2.3.6 and eventually in Rails 3!</p>
<h3> Performance </h3>
<p>I have benchmarked the new backends using an 1.8 MB XML file. The REXML, LibXML and Nokogiri backends currently ship with Rails, but are horribly slow or are buggy. The ++ variants are my improved versions of these backends, and the SAX variants are completely written from scratch using a SAX-based parser. </p>
<pre>
                  user     system      total        real
REXML        17.170000   0.060000  17.230000 ( 17.297263)
LibXML        2.100000   0.100000   2.200000 (  2.217380)
LibXML++      0.530000   0.000000   0.530000 (  0.531034)
LibXMLSAX     0.630000   0.010000   0.640000 (  0.632472)
Nokogiri      5.280000   0.020000   5.300000 (  5.322575)
Nokogiri++    1.840000   0.020000   1.860000 (  1.872055)
NokogiriSAX   0.770000   0.000000   0.770000 (  0.778777)
</pre>
<p>As you can see, LibXML++ is the fastest backend, but NokogiriSAX comes close if you want to stick to Nokogiri. </p>
<h3> No patience? </h3>
<p>Don&#8217;t want to wait for the next Rails version for this speed up? You don&#8217;t have to: just put <a href="http://github.com/rails/rails/tree/2-3-stable/activesupport/lib/active_support/xml_mini/">the appropriate backend file</a> in the <code>/lib/active_support/xml_mini/</code> folder of your Rails project, and set your backend accordingly in your environment:</p>
<pre>ActiveSupport::XmlMini.backend = 'NokogiriSAX'</pre>
<p>Happy coding in 2010!</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2010/01/03/faster-restful-xml-processing-in-rails/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Request-log-analyzer 1.5.0</title>
		<link>http://techblog.floorplanner.com/2009/11/18/request-log-analyzer-1-5-0/</link>
		<comments>http://techblog.floorplanner.com/2009/11/18/request-log-analyzer-1-5-0/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 17:51:42 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[request-log-analyzer]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[slow query log]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=801</guid>
		<description><![CDATA[
			
				
			
		
Bart and I just released request-log-analyzer version 1.5.0. New features include:

MySQL slow query log format support to analyze what queries are slowing down your database.
Format autodetection: with all those supported file formats, remembering the right --format parameter gets tricky. With format autodetection, this usually is not needed anymore!

As always, run the following command to install [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2009%2F11%2F18%2Frequest-log-analyzer-1-5-0%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2009%2F11%2F18%2Frequest-log-analyzer-1-5-0%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Bart and I just released <a href="http://github.com/wvanbergen/request-log-analyzer">request-log-analyzer</a> version 1.5.0. New features include:</p>
<ul>
<li><a href="http://wiki.github.com/wvanbergen/request-log-analyzer/mysql-slow-query-log">MySQL slow query log</a> format support to analyze what queries are slowing down your database.</li>
<li><strong>Format autodetection:</strong> with all those supported file formats, remembering the right <code style="white-space: nowrap">--format</code> parameter gets tricky. With format autodetection, this usually is not needed anymore!</li>
</ul>
<p>As always, run the following command to install or upgrade to the latest version:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family: Monaco,monospace;">$ gem install request-log-analyzer</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2009/11/18/request-log-analyzer-1-5-0/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Case-insensitive validates_uniqueness_of slowness</title>
		<link>http://techblog.floorplanner.com/2009/11/17/case-insensitive-validates_uniqueness_of-slowness/</link>
		<comments>http://techblog.floorplanner.com/2009/11/17/case-insensitive-validates_uniqueness_of-slowness/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 14:01:02 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[ActiveRecord]]></category>
		<category><![CDATA[case insensitive]]></category>
		<category><![CDATA[index]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[validates_uniqueness_of]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=787</guid>
		<description><![CDATA[
			
				
			
		
Watch out when using validates_uniqueness_of :field, :case_sensitive => false. Rails transforms this in a query that cannot be supported by an index, which will really slow validation down if the underlying table grows larger.
For example, we use validates_uniqueness_of to check for duplicate e-mail addresses. Because email addresses are case-insensitive, adding :case_sensitive => false seems like [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2009%2F11%2F17%2Fcase-insensitive-validates_uniqueness_of-slowness%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2009%2F11%2F17%2Fcase-insensitive-validates_uniqueness_of-slowness%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Watch out when using <code>validates_uniqueness_of :field, :case_sensitive => false</code>. Rails transforms this in a query that cannot be supported by an index, which will really slow validation down if the underlying table grows larger.</p>
<p>For example, we use <code>validates_uniqueness_of</code> to check for duplicate e-mail addresses. Because email addresses are case-insensitive, adding <code>:case_sensitive => false</code> seems like a natural choice. However, this results in the following queries:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family: Monaco,monospace;"><span style="color: #808080; font-style: italic;"># For a new User instance:</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> id <span style="color: #993333; font-weight: bold;">FROM</span> users 
 <span style="color: #993333; font-weight: bold;">WHERE</span> LOWER<span style="color: #66cc66;">&#40;</span>users<span style="color: #66cc66;">.</span>email<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">BINARY</span> <span style="color: #ff0000;">'user@example.com'</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># For an existing User instance:</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> id <span style="color: #993333; font-weight: bold;">FROM</span> users 
 <span style="color: #993333; font-weight: bold;">WHERE</span> LOWER<span style="color: #66cc66;">&#40;</span>users<span style="color: #66cc66;">.</span>email<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">BINARY</span> <span style="color: #ff0000;">'user@example.com'</span> 
   <span style="color: #993333; font-weight: bold;">AND</span> users<span style="color: #66cc66;">.</span>id <span style="color: #66cc66;">&lt;&gt;</span> <span style="color: #cc66cc;">42</span></pre></div></div>

<p>This query cannot be optimized by a (unique) index on the email field and thus has to scan the full table. As our users table grew larger, these queries started to show up in our slow query log. </p>
<p>However, MySQL uses case-insensitive comparison by default. (To be exact, case-sensitiveness depends on the current collation, which can vary. Rails generates the weird query to make sure the check works, regardless of the current collation.) The conversion to lowercase therefore is not necessary for a uniqueness check (as long as the field has a case-insensitive collation like <code>utf8_general_ci</code>). I decided to write my own validation method that issues a query that can be optimized by a query.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;">  <span style="color:#008000; font-style:italic;"># Alternative for validates_uniqueness_of :email, :case_sensitive =&gt; false</span>
  validate <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>user<span style="color:#006600; font-weight:bold;">|</span>
    conditions = <span style="color:#996600;">&quot;users.email = :email&quot;</span>
    conditions <span style="color:#006600; font-weight:bold;">&lt;&lt;</span> <span style="color:#996600;">&quot; AND users.id != :id&quot;</span> <span style="color:#9966CC; font-weight:bold;">unless</span> user.<span style="color:#9900CC;">new_record</span>?
    conditions = <span style="color:#006600; font-weight:bold;">&#91;</span>conditions, <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> user.<span style="color:#9900CC;">email</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> user.<span style="color:#9900CC;">id</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> User.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:first</span>, <span style="color:#ff3333; font-weight:bold;">:select</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:id</span>, <span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> conditions<span style="color:#006600; font-weight:bold;">&#41;</span>
      user.<span style="color:#9900CC;">errors</span>.<span style="color:#9900CC;">add</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#996600;">'Already in use'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>There is <a href="https://rails.lighthouseapp.com/projects/8994/tickets/2503-validates_uniqueness_of-is-horribly-inefficient-in-mysql">a ticket for this issue in Rails&#8217;s Lighthouse</a>, but as of yet this issue is unresolved. For now, this solution works to keep our slow query log nice and quiet!</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2009/11/17/case-insensitive-validates_uniqueness_of-slowness/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Adyen payment services for Rails</title>
		<link>http://techblog.floorplanner.com/2009/09/27/adyen-payment-services-for-rails/</link>
		<comments>http://techblog.floorplanner.com/2009/09/27/adyen-payment-services-for-rails/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 18:27:23 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[adyen]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[payments]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rspec]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[signature calculation]]></category>
		<category><![CDATA[soap]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=726</guid>
		<description><![CDATA[
			
				
			
		
Michel and I have been playing around with integrating Adyen payment services in Rails applications. We have assembled some of the pieces of code we have written, combined them, written specs for them and released the result as a gem. The package is also included on the Adyen support site.
Currently, the gem provides the following:

Simple [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2009%2F09%2F27%2Fadyen-payment-services-for-rails%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2009%2F09%2F27%2Fadyen-payment-services-for-rails%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>Michel and I have been playing around with integrating <a href="http://www.adyen.com/">Adyen payment services</a> in Rails applications. We have assembled some of the pieces of code we have written, combined them, written specs for them and <a href="http://github.com/wvanbergen/adyen">released the result as a gem</a>. The package is also included on the <a href="https://support.adyen.com/index.php?_m=downloads&amp;_a=viewdownload&amp;downloaditemid=43">Adyen support site</a>.</p>
<p>Currently, the gem provides the following:</p>
<ul>
<li>Simple configuration and setup.</li>
<li>Uses Adyen&#8217;s test or production environment based on your Rails environment.</li>
<li>Generating hidden form fields for redirecting to Adyen for a payment.</li>
<li>Calculating the signature to sign these redirects.</li>
<li>Checking Adyen&#8217;s signature when the user gets redirected back.</li>
<li>Matchers to easily test your payment forms using RSpec.</li>
<li>Receiving and storing notifications from Adyen.</li>
<li>Calling the Adyen SOAP services (requires the <a href="http://github.com/troelskn/handsoap">Handsoap gem</a>).</li>
</ul>
<p>Currently, not all SOAP services are implemented (because we didn&#8217;t need them all). It should be quite easy to implement them as well based on the other services that are implemented already. Don&#8217;t hesitate to submit patches!</p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2009/09/27/adyen-payment-services-for-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Performance tweaking of Ruby algorithms</title>
		<link>http://techblog.floorplanner.com/2009/09/27/performance-tweaking-of-ruby-algorithms/</link>
		<comments>http://techblog.floorplanner.com/2009/09/27/performance-tweaking-of-ruby-algorithms/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 06:02:42 +0000</pubDate>
		<dc:creator>Willem van Bergen</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[analysis]]></category>
		<category><![CDATA[blocks]]></category>
		<category><![CDATA[parallelization]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[profiling]]></category>
		<category><![CDATA[regular expressions]]></category>
		<category><![CDATA[request-log-analyzer]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[ruby-prof]]></category>

		<guid isPermaLink="false">http://techblog.floorplanner.com/?p=716</guid>
		<description><![CDATA[
			
				
			
		
I have been working on request-log-analyzer quite a lot recently. One of the things I focused on was improving the parsing performance: because it parses log files that often are very big, processing times tend to be long. So all savings are very welcome.
Improving the performance of a command line application that does a lot [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2009%2F09%2F27%2Fperformance-tweaking-of-ruby-algorithms%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ftechblog.floorplanner.com%2F2009%2F09%2F27%2Fperformance-tweaking-of-ruby-algorithms%2F&amp;style=normal" height="61" width="50" /><br />
			</a>
		</div>
<p>I have been working on <a href="http://github.com/wvanbergen/request-log-analyzer">request-log-analyzer</a> quite a lot recently. One of the things I focused on was improving the parsing performance: because it parses log files that often are very big, processing times tend to be long. So all savings are very welcome.</p>
<p>Improving the performance of a command line application that does a lot of processing is very different from optimizing the performance of a web application. Request-log-analyzer basically reads a file line by line and processes it, so small performance improvements in the line processing algorithm can really add up when the file has a lot of lines. I used <a href="http://ruby-prof.rubyforge.org/">ruby-prof</a> to get information about the performance of our algorithms split out by method to focus my tweaking efforts. I have written down some of my findings below; hopefully they can be helpful.</p>
<h3>Parallelization</h3>
<p>My first thought for improving performance was parallelization: parse multiple lines at the same time. Unfortunately, this did not yield the results I was hoping for: it instead become slower. Probably, this is because Ruby implements its own in-process threading and thus only uses one core of my processor.</p>
<h3>Block overhead</h3>
<p>The overhead of using a block should not be underestimated. Consider the following simple change, which improved the performance of request-log-analyzer by 1.5-2% on large log files:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#008000; font-style:italic;"># with block</span>
io.<span style="color:#9900CC;">each_line</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>line<span style="color:#006600; font-weight:bold;">|</span> process_line<span style="color:#006600; font-weight:bold;">&#40;</span>line<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># without block</span>
process_line<span style="color:#006600; font-weight:bold;">&#40;</span>line<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">while</span> line = io.<span style="color:#CC0066; font-weight:bold;">gets</span></pre></div></div>

<h3>Regular expressions</h3>
<p>If you&#8217;re using complex regular expressions, and you do not expect that every string will match it successfully, it can be beneficial to test the string with a simpler regexp first. For example, request-log-analyzer uses a complex regexp to see if a line in a log file is a &#8220;Completed in&#8230;&#8221; line with the request duration in it:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#008000; font-style:italic;"># Check every line to see if it is a &quot;completed&quot; line and capture the values</span>
<span style="color:#9966CC; font-weight:bold;">if</span> line =~ <span style="color:#006600; font-weight:bold;">/</span>Completed <span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#006600; font-weight:bold;">&#40;</span>\d<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>ms \<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>?:View: <span style="color:#006600; font-weight:bold;">&#40;</span>\d<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#006600; font-weight:bold;">&#41;</span>?DB: <span style="color:#006600; font-weight:bold;">&#40;</span>\d<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>\<span style="color:#006600; font-weight:bold;">&#41;</span> \<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#40;</span>\d\d\d<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#006600; font-weight:bold;">+</span>\<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#40;</span>http.<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>\<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">/</span>
  <span style="color:#008000; font-style:italic;"># do something with the captured values</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>I improved this by first checking for a superficial regexp that tells me with 99% certainty that the complex regexp will match the line successfully as well:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#9966CC; font-weight:bold;">if</span> line =~ <span style="color:#006600; font-weight:bold;">/</span>Completed <span style="color:#9966CC; font-weight:bold;">in</span><span style="color:#006600; font-weight:bold;">/</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> line =~ <span style="color:#006600; font-weight:bold;">/</span>Completed <span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#006600; font-weight:bold;">&#40;</span>\d<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>ms \<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>?:View: <span style="color:#006600; font-weight:bold;">&#40;</span>\d<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#006600; font-weight:bold;">&#41;</span>?DB: <span style="color:#006600; font-weight:bold;">&#40;</span>\d<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>\<span style="color:#006600; font-weight:bold;">&#41;</span> \<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#40;</span>\d\d\d<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#006600; font-weight:bold;">+</span>\<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006600; font-weight:bold;">&#40;</span>http.<span style="color:#006600; font-weight:bold;">+</span><span style="color:#006600; font-weight:bold;">&#41;</span>\<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">/</span>
    <span style="color:#008000; font-style:italic;"># do something with the captured values</span>
  <span style="color:#9966CC; font-weight:bold;">else</span>
    <span style="color:#ff6633; font-weight:bold;">$stderr</span>.<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{line.inspect} expected to match 'Completed' regexp!&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Depending on the log file, this can increase performance by ~3%. Another benefit of this approach is that it will give feedback on lines that matched the simple regexp, but not the complex one. This information can be used to correct the regular expressions.</p>
<h3>Calculate things that do not change only once</h3>
<p>Calculate things that do not change only once is easier said than done. For instance, request-log analyzer can aggregate durations in a category. A category can be based on a request field that is parsed from the log, or a <code>Proc</code> that calculates it:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#008000; font-style:italic;"># during parsing:</span>
<span style="color:#9966CC; font-weight:bold;">if</span> categorizer.<span style="color:#9900CC;">respond_to</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:call</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  category = categorizer.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span>request<span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">else</span>
  category = request<span style="color:#006600; font-weight:bold;">&#91;</span>categorizer<span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
categories<span style="color:#006600; font-weight:bold;">&#91;</span>category<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">aggregate_request</span><span style="color:#006600; font-weight:bold;">&#40;</span>request<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>With this implementation, <code>categorizer</code> will be checked to be a <code>Proc</code> for every request, but as the value of <code>categorizer</code> will not change, the result of the check will be constant as well. I solved this my making sure that it is always a <code>Proc</code> beforehand, so the check is no longer necessary during parsing:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#008000; font-style:italic;"># before parsing:</span>
<span style="color:#9966CC; font-weight:bold;">if</span> categorizer.<span style="color:#9900CC;">respond_to</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:call</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  categorizer_proc = categorizer
<span style="color:#9966CC; font-weight:bold;">else</span>
  categorizer_proc = <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>request<span style="color:#006600; font-weight:bold;">|</span> request<span style="color:#006600; font-weight:bold;">&#91;</span>categorizer<span style="color:#006600; font-weight:bold;">&#93;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># during parsing:</span>
category = categorizer_proc.<span style="color:#9900CC;">call</span><span style="color:#006600; font-weight:bold;">&#40;</span>request<span style="color:#006600; font-weight:bold;">&#41;</span>
categories<span style="color:#006600; font-weight:bold;">&#91;</span>category<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">aggregate_request</span><span style="color:#006600; font-weight:bold;">&#40;</span>request<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Performance gain: ~1%! And because on several instances a similar technique could be applied, the performance got improved by about 4% in total.</p>
<h3>Check the most common case first</h3>
<p>Consider the following example, which converts a number in any traffic unit (kilobytes, MB, etc) to bytes:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> convert_traffic<span style="color:#006600; font-weight:bold;">&#40;</span>value, unit<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">case</span> unit
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#ff3333; font-weight:bold;">:GB</span>, <span style="color:#ff3333; font-weight:bold;">:G</span>, <span style="color:#ff3333; font-weight:bold;">:gigabyte</span>      <span style="color:#9966CC; font-weight:bold;">then</span> <span style="color:#006600; font-weight:bold;">&#40;</span>value.<span style="color:#9900CC;">to_f</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">1000</span>_000_000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">round</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#ff3333; font-weight:bold;">:MB</span>, <span style="color:#ff3333; font-weight:bold;">:M</span>, <span style="color:#ff3333; font-weight:bold;">:megabyte</span>      <span style="color:#9966CC; font-weight:bold;">then</span> <span style="color:#006600; font-weight:bold;">&#40;</span>value.<span style="color:#9900CC;">to_f</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">1000</span>_000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">round</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#ff3333; font-weight:bold;">:KB</span>, <span style="color:#ff3333; font-weight:bold;">:K</span>, <span style="color:#ff3333; font-weight:bold;">:kilobyte</span>, <span style="color:#ff3333; font-weight:bold;">:kB</span> <span style="color:#9966CC; font-weight:bold;">then</span> <span style="color:#006600; font-weight:bold;">&#40;</span>value.<span style="color:#9900CC;">to_f</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">1000</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">round</span>
  <span style="color:#008000; font-style:italic;"># ... even more units here</span>
  <span style="color:#9966CC; font-weight:bold;">else</span> value.<span style="color:#9900CC;">to_i</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>In most cases, the value will simply given in bytes, which will be returned by the final <code>else</code> after all possibilities have been checked. This can be improved by checking for this possibility first:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family: Monaco,monospace;"><span style="color:#008000; font-style:italic;"># Converts traffic in any unit to bytes.</span>
<span style="color:#9966CC; font-weight:bold;">def</span> convert_traffic<span style="color:#006600; font-weight:bold;">&#40;</span>value, unit<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">case</span> unit
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#0000FF; font-weight:bold;">nil</span>, <span style="color:#ff3333; font-weight:bold;">:B</span>, <span style="color:#ff3333; font-weight:bold;">:byte</span>          <span style="color:#9966CC; font-weight:bold;">then</span> value.<span style="color:#9900CC;">to_i</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#ff3333; font-weight:bold;">:GB</span>, <span style="color:#ff3333; font-weight:bold;">:G</span>, <span style="color:#ff3333; font-weight:bold;">:gigabyte</span>      <span style="color:#9966CC; font-weight:bold;">then</span> <span style="color:#006600; font-weight:bold;">&#40;</span>value.<span style="color:#9900CC;">to_f</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">1000</span>_000_000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">round</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#ff3333; font-weight:bold;">:MB</span>, <span style="color:#ff3333; font-weight:bold;">:M</span>, <span style="color:#ff3333; font-weight:bold;">:megabyte</span>      <span style="color:#9966CC; font-weight:bold;">then</span> <span style="color:#006600; font-weight:bold;">&#40;</span>value.<span style="color:#9900CC;">to_f</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">1000</span>_000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">round</span>
  <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#ff3333; font-weight:bold;">:KB</span>, <span style="color:#ff3333; font-weight:bold;">:K</span>, <span style="color:#ff3333; font-weight:bold;">:kilobyte</span>, <span style="color:#ff3333; font-weight:bold;">:kB</span> <span style="color:#9966CC; font-weight:bold;">then</span> <span style="color:#006600; font-weight:bold;">&#40;</span>value.<span style="color:#9900CC;">to_f</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">1000</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">round</span>
  <span style="color:#008000; font-style:italic;"># ... even more units here</span>
  <span style="color:#9966CC; font-weight:bold;">else</span> <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#996600;">&quot;Unknown unit: #{unit.inspect}!&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Again this change adds up to a ~1% performance increase if this method is called very often.</p>
<p>These kinds of changes really improved the performance of request-log-analyzer by quite a bit, so upgrade to the latest version to get some of your valuable time back! <img src='http://techblog.floorplanner.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://techblog.floorplanner.com/2009/09/27/performance-tweaking-of-ruby-algorithms/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
