<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Parallel Roads</title>
	<atom:link href="http://parallelroads.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://parallelroads.wordpress.com</link>
	<description>Thoughts on technology and design by Rick Molloy</description>
	<lastBuildDate>Thu, 12 Aug 2010 23:25:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='parallelroads.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Parallel Roads</title>
		<link>http://parallelroads.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://parallelroads.wordpress.com/osd.xml" title="Parallel Roads" />
	<atom:link rel='hub' href='http://parallelroads.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Continuations and Directed Graphs Part 1: implementing with the Parallel Pattern Library</title>
		<link>http://parallelroads.wordpress.com/2010/01/22/continuations-and-directed-graphs-part-1-implementing-with-the-parallel-pattern-library/</link>
		<comments>http://parallelroads.wordpress.com/2010/01/22/continuations-and-directed-graphs-part-1-implementing-with-the-parallel-pattern-library/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 06:22:45 +0000</pubDate>
		<dc:creator>parallelroadsblog</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://parallelroads.wordpress.com/2010/06/22/continuations-and-directed-graphs-part-1-implementing-with-the-parallel-pattern-library/</guid>
		<description><![CDATA[I’ve been thinking a lot about continuations and directed graphs the last couple days and different ways to implement them.  I’ve also been looking closer at std::thread, std::future and std::async to ensure I fully understand them.  So I decided I’d make a (small) multi-part series out of this and show some simple sketches of implementations [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=parallelroads.wordpress.com&amp;blog=14342427&amp;post=15&amp;subd=parallelroads&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I’ve been thinking a lot about continuations and directed graphs the last couple days and different ways to implement them.  I’ve also been looking closer at std::thread, std::future and std::async to ensure I fully understand them.  So I decided I’d make a (small) multi-part series out of this and show some simple sketches of implementations of continuation using the <a href="http://msdn.microsoft.com/en-us/library/dd492418(VS.100).aspx">Parallel Pattern Library</a>, <a href="http://www.stdthread.co.uk/">std::future and std::async</a> and the <a href="http://msdn.microsoft.com/en-us/library/dd492627(VS.100).aspx">Agents Library</a>.  </p>
<p>This is part 1 which will cover a simple implementation of ‘run_when’ and ‘wait_for_all’ using the PPL. </p>
<h3>First a simple dependency / continuation</h3>
<p>Here’s an incredibly simple dependency, task 2 depends on task 1.</p>
<div id="codeSnippetWrapper">
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;">
<span style="color:#0000ff;">void</span> ContinueableTask(<span style="color:#0000ff;">int</span> in){
       printf(<span style="color:#006080;">"building project: %d\n"</span>, in);
};

<span style="color:#0000ff;">void</span> SimpleContinuation(){
       ContinueableTask(1);

   <span style="color:#008000;">//task 2 depends on task 1
</span>   ContinueableTask(2);
}
</pre>
<p> </p>
<div>There’s not a lot of concurrency here with only two tasks, yet we can still make this execution ‘parallel’ with a task_group as follows:</div>
<div id="codeSnippetWrapper">
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#0000ff;">void</span> SimpleContinuationTasks(){
   <span style="color:#008000;">//task1</span>   task_group task1;
   task1.run([](){ContinueableTask(1);});

   <span style="color:#008000;">//task2 depends on task 1
 </span>   task_group task2;
    task2.run([&amp;](){
      task1.wait();
      ContinueableTask(2);
    });
   <span style="color:#008000;">//wait for task 2
</span>   task2.wait();
}</pre>
<p> </p>
<h3><span style="font-family:Arial;color:#222222;">Making it look a bit cleaner </span></h3>
<div> </div>
<div>The code here isn’t too bad, but is a little awkward.  It would sure be nice to write something like this instead:</div>
<div>&#8212;-</div>
<div id="codeSnippetWrapper">
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#0000ff;">void</span> SimpleContinuation(){
    <span style="color:#0000ff;">auto</span> task1 = run_task([](){ContinueableTask(1);});
    <span style="color:#008000;">//task 2 depends on task 1
</span>    <span style="color:#0000ff;">auto</span> task2 = run_when(task1, [](){ContinueableTask(2);});
    wait_for_all(task1, task2);
}</pre>
<p> </p>
<p>Implementing this is straightforward in the PPL (in part 2 you’ll see that it’s also straightforward with std::future and std::async and that run_task looks *a lot* like std::async). </p>
<div id="codeSnippetWrapper">
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#0000ff;">typedef</span> shared_ptr&lt;task_group&gt; shared_task_ptr;

<span style="color:#0000ff;">template</span>&lt;<span style="color:#0000ff;">typename</span> Func&gt;<span style="color:#0000ff;">inline</span> shared_task_ptr run_task(Func&amp; fn){
    shared_ptr&lt;task_group&gt; tasks = make_shared&lt;task_group&gt;();
    tasks-&gt;run(fn);
    <span style="color:#0000ff;">return</span> tasks;
}</pre>
<p> </p>
<p>I’m using a shared_ptr here because task_group is non-copyable and this allows me to return it by value without having to do anything unnatural in the interface (like create one then pass it in by reference).</p>
<p>run_when is also pretty straightforward, here is a template implementation:</p>
<div>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;">
<span style="color:#0000ff;">template</span>&lt;<span style="color:#0000ff;">typename</span> Func&gt;
<span style="color:#0000ff;">inline</span> shared_task_ptr run_when(shared_task_ptr tasks, Func fn){
    tasks-&gt;wait();
    <span style="color:#0000ff;">return</span> run_task(fn);
}
</pre>
</div>
<div> </div>
<div>and finally wait_for_all:</div>
<div> </div>
<div>
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#0000ff;">inline</span> <span style="color:#0000ff;">void</span> wait_for_all(shared_task_ptr t1, shared_task_ptr t2){    t1-&gt;wait();    t2-&gt;wait();}</pre>
</div>
<h3>Expanding this is straightforward</h3>
<div> </div>
<div>It should be pretty easy to see that you can expand run_when and wait_for_all to include overloads for more parameters and that writing an example with more complex dependencies is straightforward:</div>
<div id="codeSnippetWrapper">
<pre style="text-align:left;line-height:12pt;background-color:#f4f4f4;width:100%;"><span style="color:#0000ff;">void</span> MoreComplexContinuations(){    <span style="color:#0000ff;">auto</span> p1 = run_task([](){ContinueableTask(1);});    <span style="color:#0000ff;">auto</span> p2 = run_task([](){ContinueableTask(2);});    <span style="color:#0000ff;">auto</span> p3 = run_task([](){ContinueableTask(3);});    <span style="color:#0000ff;">auto</span> p4 = run_when(p1, [](){ContinueableTask(4);});    <span style="color:#0000ff;">auto</span> p5 = run_when(p2, p3, [](){ContinueableTask(5);});    <span style="color:#0000ff;">auto</span> p6 = run_when(p3, p4, [](){ContinueableTask(6);});    wait_for_all(p1, p2, p3, p4, p5, p6);}</pre>
<p> </p>
</div>
</div>
<h3>Next Time part2: std::async and std::future</h3>
<p>In the next few days, I’ll post part 2 and show how easy to implement this pattern in with std::async and std::future.</p>
</div>
</div>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/parallelroads.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/parallelroads.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/parallelroads.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/parallelroads.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/parallelroads.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/parallelroads.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/parallelroads.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/parallelroads.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/parallelroads.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/parallelroads.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/parallelroads.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/parallelroads.wordpress.com/15/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/parallelroads.wordpress.com/15/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/parallelroads.wordpress.com/15/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=parallelroads.wordpress.com&amp;blog=14342427&amp;post=15&amp;subd=parallelroads&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://parallelroads.wordpress.com/2010/01/22/continuations-and-directed-graphs-part-1-implementing-with-the-parallel-pattern-library/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/158b28fd14e0343cf3c9b23bfe10c784?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">parallelroadsblog</media:title>
		</media:content>
	</item>
		<item>
		<title>Parallel Debugger Windows (and colossal tasks)</title>
		<link>http://parallelroads.wordpress.com/2009/12/22/parallel-debugger-windows-and-colossal-tasks/</link>
		<comments>http://parallelroads.wordpress.com/2009/12/22/parallel-debugger-windows-and-colossal-tasks/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 06:21:57 +0000</pubDate>
		<dc:creator>parallelroadsblog</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://parallelroads.wordpress.com/2010/06/22/parallel-debugger-windows-and-colossal-tasks/</guid>
		<description><![CDATA[At the PDC I showed a demo of the new parallel stacks window in Visual Studio 2010 and how task view and method view can make navigating a multi-threaded program in the debugger more intuitive when compared to the existing threads and stacks windows (thought these got better too).  I thought I’d take a moment [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=parallelroads.wordpress.com&amp;blog=14342427&amp;post=14&amp;subd=parallelroads&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>At the PDC I showed a demo of the new parallel stacks window in Visual Studio 2010 and how task view and method view can make navigating a multi-threaded program in the debugger more intuitive when compared to the existing threads and stacks windows (thought these got better too).  I thought I’d take a moment and share the key points and the code I used for this.</p>
<p>The code is a simple deadlock of 5 tasks waiting on an event that is never set.  I don’t expect that you’ll be able to read the text on many of these pictures, but I believe the point will still clear (and you can always try this in VS 2010 yourself.</p>
<h3>Call Stack &amp; Thread Windows</h3>
<p>This is the way that we’ve been debugging multi-threaded programs via the Call Stack &amp; Thread windows.  If you’ve been here before it can be difficult if you have more than one thread to orient on and find your location. It’s often a matter of poking through each thread in the Threads window and checking the Call Stack window.</p>
<p>This is one of the reasons why in VS2010 you can now expand the Call Stack inline and search in the Threads window.  The inline expansion looking on and search actually can help significantly if you know what you’re looking for in a particular deep stack, but for this particular issue it’s not the most efficient way of seeing what’s going on…</p>
<p><a href="http://parallelroads.files.wordpress.com/2010/06/image.png"><img style="display:inline;border-width:0;" title="image" src="http://parallelroads.files.wordpress.com/2010/06/image_thumb.png?w=450&#038;h=116" border="0" alt="image" width="450" height="116" /></a></p>
<h3>The Parallel Stacks Window</h3>
<p>The Parallel Stacks Window shows you all the stacks for all the threads at once. To help with the immense amounts of screen real estate required for this, it trims the fully qualified callstack name (you can still see it in a tooltip) and it also groups the related stacks so you can start seeing any relationships and in this case hopefully seeing why this app is deadlocked.</p>
<p><a href="http://parallelroads.files.wordpress.com/2010/06/image1.png"><img style="display:inline;border-width:0;" title="image" src="http://parallelroads.files.wordpress.com/2010/06/image_thumb1.png?w=450&#038;h=182" border="0" alt="image" width="450" height="182" /></a> </p>
<h3>The Parallel Stacks Window: task view</h3>
<p>Since this is an application build on tasks, we can set the filter to show only the running tasks and now the picture starts getting more clear (and the text is almost readable).   This is because the tasks view filters out the runtime portions of the callstack and only shows the stack frames that are part of the task.  You can see that we have 4 tasks running and it looks like they are waiting in event::wait.</p>
<p><a href="http://parallelroads.files.wordpress.com/2010/06/image2.png"><img style="display:inline;border-width:0;" title="image" src="http://parallelroads.files.wordpress.com/2010/06/image_thumb2.png?w=450&#038;h=125" border="0" alt="image" width="450" height="125" /></a></p>
<h3>The Parallel Stacks Window: method view</h3>
<p>Switching the focus to event::wait and toggling ‘method view’ which will only show callers and callees in a particular method makes this incredibly obvious.</p>
<p><a href="http://parallelroads.files.wordpress.com/2010/06/image3.png"><img style="display:inline;border-width:0;" title="image" src="http://parallelroads.files.wordpress.com/2010/06/image_thumb3.png?w=450&#038;h=160" border="0" alt="image" width="450" height="160" /></a></p>
<h3>So here’s the code:</h3>
<p>This was implemented with template recursion (thanks go to <a href="http://twitter.com/twoscomplement">twoscomplement</a> who’s colossal cave inspired tweet: “You are in a maze of twisty template overloads, all alike” inspired the particular code example.</p>
<div id="codeSnippetWrapper"><span style="font-family:Lucida Console;font-size:x-small;">#include &lt;memory&gt;<br />
#include &lt;ppl.h&gt;<br />
#include &lt;iostream&gt;<br />
using namespace ::std;<br />
using namespace ::Concurrency;<br />
event e; </span></div>
<p><span style="font-family:Lucida Console;font-size:x-small;">template &lt;int N&gt;<br />
void youre_in_a_maze()<br />
{<br />
    printf(&#8220;%dth task\n&#8221;,N);<br />
    e.wait();<br />
} </span></p>
<p><span style="font-family:Lucida Console;font-size:x-small;">template &lt;int N&gt;<br />
void of_template_overloads()<br />
{<br />
    youre_in_a_maze&lt;N&gt;();<br />
}; </span></p>
<p><span style="font-family:Lucida Console;font-size:x-small;">template &lt;int N&gt;<br />
void all_alike()<br />
{<br />
    of_template_overloads&lt;N&gt;();<br />
}; </span></p>
<p><span style="font-family:Lucida Console;font-size:x-small;">template &lt;int N&gt;<br />
struct colossal_task<br />
{<br />
    shared_ptr&lt;colossal_task&lt;N &#8211; 1&gt;&gt; task;<br />
    task_group&amp; m_tasks;<br />
    colossal_task(task_group&amp; tasks):m_tasks(tasks),task(new colossal_task&lt;N &#8211; 1&gt; (tasks))<br />
    {<br />
        m_tasks.run(*task);<br />
    }<br />
    void operator ()() const<br />
    {<br />
        all_alike&lt;N&gt;();<br />
    }<br />
}; </span></p>
<p><span style="font-family:Lucida Console;font-size:x-small;">template &lt;&gt;<br />
struct colossal_task &lt;0&gt;<br />
{<br />
    task_group&amp; m_tasks;<br />
    colossal_task(task_group&amp; tasks):m_tasks(tasks)<br />
    {<br />
    }<br />
    void operator ()() const<br />
    {<br />
        printf(&#8220;0th task\n&#8221;);<br />
    }<br />
}; </span></p>
<p><span style="font-family:Lucida Console;font-size:x-small;">int main()<br />
{<br />
    task_group tasks;<br />
    colossal_task&lt;5&gt; t(tasks);<br />
    tasks.wait();<br />
    return 0;<br />
}</span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/parallelroads.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/parallelroads.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/parallelroads.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/parallelroads.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/parallelroads.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/parallelroads.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/parallelroads.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/parallelroads.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/parallelroads.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/parallelroads.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/parallelroads.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/parallelroads.wordpress.com/14/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/parallelroads.wordpress.com/14/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/parallelroads.wordpress.com/14/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=parallelroads.wordpress.com&amp;blog=14342427&amp;post=14&amp;subd=parallelroads&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://parallelroads.wordpress.com/2009/12/22/parallel-debugger-windows-and-colossal-tasks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/158b28fd14e0343cf3c9b23bfe10c784?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">parallelroadsblog</media:title>
		</media:content>

		<media:content url="http://parallelroads.files.wordpress.com/2010/06/image_thumb.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://parallelroads.files.wordpress.com/2010/06/image_thumb1.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://parallelroads.files.wordpress.com/2010/06/image_thumb2.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://parallelroads.files.wordpress.com/2010/06/image_thumb3.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>A simple refactoring to compile time interfaces</title>
		<link>http://parallelroads.wordpress.com/2009/11/22/a-simple-refactoring-to-compile-time-interfaces/</link>
		<comments>http://parallelroads.wordpress.com/2009/11/22/a-simple-refactoring-to-compile-time-interfaces/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 06:24:17 +0000</pubDate>
		<dc:creator>parallelroadsblog</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">https://parallelroads.wordpress.com/2010/06/22/a-simple-refactoring-to-compile-time-interfaces/</guid>
		<description><![CDATA[Recently I had an opportunity to do some refactoring in a raytracer that we use for demos at work.  I don’t how you feel about refactoring, but I personally find it an immensely calming and rewarding experience because it allows me to make improvements to quality and design in a very incremental and manner, even [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=parallelroads.wordpress.com&amp;blog=14342427&amp;post=17&amp;subd=parallelroads&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently I had an opportunity to do some refactoring in a raytracer that we use for demos at work.  I don’t how you feel about refactoring, but I personally find it an immensely calming and rewarding experience because it allows me to make improvements to quality and design in a very incremental and manner, even to old or large code bases.</p>
<p>I also really enjoy generic programming and I wanted to share this refactoring because it is a simple example of how using generic programming to rely on a compile time interface can reduce overall code. It also doesn’t rely on techniques that can make generic programming inaccessible like compile time polymorphism, typelists or the curiously recurring template pattern.</p>
<p>It’s borderline too simple to write down, but I’m doing it anyways so here it is…</p>
<h3>Two similar classes a float3 and a color3</h3>
<p>Before the refactoring, the raytracer had 2 classes that were used all over the place in the code, float3 and color.  Both of these are simple structs that had 3 data members.  Their definitions are incredibly similar and looked like this:</p>
<p><span style="font-family:Lucida Console;font-size:x-small;">struct color3{<br />
   float r;<br />
   float g;<br />
   float b;<br />
   color3(){}<br />
   color3(float r_, float g_, float b_):r(r_),g(g_),b(b_){}<br />
};<br />
struct float3{<br />
   float x;<br />
   float y;<br />
   float z;<br />
   float3(){}<br />
   float3(float x_, float y_, float z_):x(x_),y(y_),z(z_){}<br />
};</span></p>
<h3>Sadly, 2 sets of methods to operate on the classes</h3>
<p>Unfortunately, both of these classes also initially had strongly typed supporting functions like ‘add’ which were incredibly similar, only the float3 version used x,y and z while the color version used r, g, b:</p>
<p><span style="font-family:Lucida Console;font-size:x-small;">color3 add(color3&amp; c1, color3&amp; c2)<br />
{<br />
   return color3(c1.r + c2.r, c1.g + c2.g, c1.b + c2.b);<br />
}<br />
float3 add(float3&amp; v1, float3&amp; v2)<br />
{<br />
   return color3(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z);<br />
}</span></p>
<p>Unfortunately while incredibly straightforward and seemingly simple, this isn’t very desirable. There were 4 or 5 functions like this that were essentially identical with the only difference being the types and member variable names.</p>
<h3>These methods can be made generic</h3>
<p>What I wanted was a simple way to write the add function once that added no additional size or instruction overheads to the structs, since both of these structs are used frequently inside a very tight loop, even small changes will have a noticable performance impact.  For example one of the refactorings I did removed a single call to the copy constructor and that increased the performance by about 50ms per frame ~12.5% (seriously) the scene I was looking at went from ~4 fps to ~5 fps on my quad core.</p>
<p>Here’s what I decided to do, I’ll explain in a moment, first I created a template function ‘add’:</p>
<p><span style="font-family:Lucida Console;font-size:x-small;">template &lt;class vec&gt;<br />
inline vec add(vec&amp; v1, vec&amp; v2)<br />
{<br />
    return vec(v1.first()  + v2.first(),<br />
               v1.second() + v2.second(),<br />
               v1.third()  + v2.third());<br />
}</span></p>
<div> </div>
<div>This template function works with two references to type ‘vec’; and relys on the member functions first, second and third to be present in vec because it uses them.</div>
<div>Implementing each of these methods is straightforward:</div>
<p><span style="font-family:Lucida Console;color:#000000;font-size:x-small;">// implementation of first for float3<br />
inline float first()<br />
{<br />
    return x;<br />
} </span></p>
<p><span style="font-family:Lucida Console;color:#000000;font-size:x-small;">// implementation of first for color3<br />
inline float first()<br />
{<br />
    return r;<br />
}</span></p>
<div> </div>
<div>This may seem inefficient, but it turns out not to be. </div>
<div> </div>
<div>Because I’m relying on a compile time interface rather than a virtual function there is no additional overhead.  In this method the return value is a simple built in type, but even for larger classes when the value is constructed as part of the return statement a compiler may optimize away the copy construction, and most do.  This is known as the return value optimization and the C++ spec explicitly allows for this (section 12.8).</div>
<div> </div>
<h3>Did it make a difference?</h3>
<div>As far as code reduction goes, I removed 7 member functions from one class and turned the other 7 into free functions and I added a total of six member functions for the first, second and third.   So this was a net minor change from a lines of code perspective, but there are no longer 2 sets of functions with similar names and functionality.</div>
<div> </div>
<div>I also made it possible to provide mock classes to those free functions for testing, before they were strongly typed and this just wasn’t possible.  Now if I want I can test ‘add’ without relying on float3 or color3.</div>
<div> </div>
<div>Additionally, if new functions are needed that work on these types, I no longer have to add them to both classes. I can simply add the function that relies on first, second and third and this can instantly be used with both types.</div>
<div> </div>
<div>Finally, if I’m only looking at reading properties there is now no reason to expose publicly the member variables and this is another massive win because I can protect against potentially non-thread safe data writes.</div>
<div> </div>
<div>-Rick</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/parallelroads.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/parallelroads.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/parallelroads.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/parallelroads.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/parallelroads.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/parallelroads.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/parallelroads.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/parallelroads.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/parallelroads.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/parallelroads.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/parallelroads.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/parallelroads.wordpress.com/17/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/parallelroads.wordpress.com/17/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/parallelroads.wordpress.com/17/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=parallelroads.wordpress.com&amp;blog=14342427&amp;post=17&amp;subd=parallelroads&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://parallelroads.wordpress.com/2009/11/22/a-simple-refactoring-to-compile-time-interfaces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/158b28fd14e0343cf3c9b23bfe10c784?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">parallelroadsblog</media:title>
		</media:content>
	</item>
	</channel>
</rss>
