<?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>jalf.dk</title>
	<atom:link href="http://jalf.dk/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://jalf.dk/blog</link>
	<description>Musings and thoughts on programming and other geeky stuff</description>
	<lastBuildDate>Sun, 14 Feb 2010 11:20:44 +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>Privacy: Or why I don’t trust Google with my personal information</title>
		<link>http://jalf.dk/blog/2010/02/privacy-or-why-i-dont-trust-google-with-my-personal-information/</link>
		<comments>http://jalf.dk/blog/2010/02/privacy-or-why-i-dont-trust-google-with-my-personal-information/#comments</comments>
		<pubDate>Sun, 14 Feb 2010 01:50:14 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Meanwhile]]></category>
		<category><![CDATA[buzz]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[privacy]]></category>
		<category><![CDATA[twitter]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=506</guid>
		<description><![CDATA[So Google launched their Twitter/MySpace/Facebook killer, Buzz, and apparently subscribed every GMail user to it without asking anyone for permission.

The result is that a lot of people now have sensitive personal information floating around in public. An example of this (found via ArsTechnica) is this woman, who starts her post like this:


  I use [...]]]></description>
			<content:encoded><![CDATA[<p>So Google launched their Twitter/MySpace/Facebook killer, Buzz, and apparently subscribed every GMail user to it without asking anyone for permission.</p>

<p>The result is that a lot of people now have sensitive personal information floating around in public.<span id="more-506"></span> An example of this (found via <a href="http://arstechnica.com/tech-policy/news/2010/02/a-frustrated-user-lashes-out.ars">ArsTechnica</a>) is <a href="http://fugitivus.wordpress.com/2010/02/11/fuck-you-google/">this woman</a>, who starts her post like this:</p>

<blockquote>
  <p>I use my private Gmail account to email my boyfriend and my mother.</p>
  
  <p>There’s a BIG drop-off between them and my other “most frequent” contacts.</p>
  
  <p>You know who my third most frequent contact is?</p>
  
  <p>My abusive ex-husband.</p>
  
  <p>Which is why it’s SO EXCITING, Google, that you AUTOMATICALLY allowed all my most frequent contacts access to my Reader, including all the comments I’ve made on Reader items, usually shared with my boyfriend, who I had NO REASON to hide my current location or workplace from, and never did.</p>
</blockquote>

<p>Ouch.</p>

<p>Others, with less at stake personally, are <a href="http://news.cnet.com/8301-31322_3-10451428-256.html">also pissed</a>:</p>

<blockquote>
  <p>See, I love the idea of neat new tech innovations that lead to streamlined communication, real-time updating, in-line video and photo posting, and supersimple friend and contact integration. I do not, however, like a product that bursts through my door like a tornado and opts me in to wanton in-box clutter and spam (or, more precisely, bacn) publicly reveals my personal contact list without asking me, threatens to broadcast my e-mail address anytime someone wants to @ me in a Buzz, and even appears to grab photos off my Android phone that I’ve never uploaded.</p>
</blockquote>

<p>or <a href="http://ventspace.wordpress.com/2010/02/10/i-have-google-buzz-now-apparently/">this one</a></p>

<blockquote>
  <p>So…yeah, I guess I’m on Google Buzz. It’s linked to my Picasa and WordPress accounts, so you can follow everything I do. Cause that’s not creepy or anything. The best part is that the defaults for everything are public, and you end up broadcasting to a bunch of random people unless you sit down and sort through. I’m expecting this to backfire for a bunch of people, and not just eventually but almost immediately. It might not be a bad idea to start a betting pool on when the first child porn charges are filed as some highschool student accidentally sends herself to the entire school.</p>
</blockquote>

<p>I could go on, but I really don’t want this to turn into some kind of link farm.</p>

<p>I’m not personally affected by this. I do have a GMail account, and yes, they opted me in to Buzz, but the account contains no personal information whatsoever, and no personal emails. I use it exclusively as a dumping ground for spam,  and form mails I don’t want cluttering up my <em>real</em> email inbox. I’ve never even sent an email from the account.</p>

<p>I use the Google search engine, but I am not signed in to it, and have never created a profile or a customized homepage on it. I’m sure they could still identify me just by examining cookies or my IP address, but at least they’d have to work for it. And it’s not like my Google searches are state secrets anyway. As long as people are not able to search for my name and bring up a list of everything I’ve searched for, I’m satisfied.</p>

<p>I also use Google Analytics for this blog. I feel OK about that because this blog is already my public face on the internet. Google already knows a lot about it simply by indexing it for their search engine. I have no problem with them generating statistics on where my visitors come from, as long as they make the information available to me too. The only sensitive information associated with this blog is my login password, and I’m pretty sure Google doesn’t have that. And they’re not getting it, even if they launched a GPassword service tomorrow.</p>

<p>I use the Wordpress software, but not hosted on Wordpress.com. I don’t use Picasa or Google Reader. I don’t use Google Documents.</p>

<p>So all in all, yes, Google certainly knows a lot of fragments of information about me. Google searches can turn up quite a bit, they can collect a few more bits and pieces through cookies when I use their search engine, and they have a lot of statistics on who reads my blog. But they can’t read my emails. They don’t have any really sensitive information about me. Nothing related to my work, personal life or studies is tied to Google.</p>

<p>And this brings us to the point of this post:</p>

<h1>Don’t blame Buzz, blame GMail</h1>

<p>A lot of people are furious at Google for the mixture of incompetence and indifference towards users’ privacy with which Buzz was launched, and while that might be justified, it is missing a fundamental point.</p>

<p>Buzz is just doing what Google does best, what they’ve always done, and what they <em>should</em> be doing. Here’s what Google’s own <a href="http://www.google.com/corporate/">website</a> has to say on the company’s mission:</p>

<blockquote>
  <p>Google’s mission: to organize the world’s information and make it universally accessible and useful</p>
</blockquote>

<p>Google is dedicated to making information <em>universally accessible</em>. For a lot of information, that’s a good thing. Their search engine turned the internet upside down — for the first time ever, users were able to actually <em>find</em> the information they needed. Google is good at this, and we’ve benefited hugely from it.</p>

<p>And social networking is right up Google’s alley as well: Social networking is all about making information about you and me accessible to the world in an organized manner. A lot of Facebook’s popularity relies on their ability to analyze our existing relations, friendships and networks, and use this to suggest new friends. My Twitter would be useless if I couldn’t follow the people I wanted to keep up with, and if others couldn’t find my tweets through searches. Buzz is simply more of the same, and there is nothing wrong with that. It’s another social networking service, and Google is <em>exactly</em> the right company to do something like this. No one is better at organizing information and telling us exactly what we want to know.</p>

<p>The problem is that another of their services is not so well suited for the company. Email is something almost everyone considers personal and private. Even the US government, in its desperate war on people who wear turbans, speak funny and pray to Allah, has only given itself permission to sniff the subject lines of people’s mails sent over GMail. This is considered the equivalent of reading the envelope, without opening it and looking at the letter inside. Because that letter is personal. And so are the bodies of our emails.</p>

<p>But if we consider our emails to be sensitive personal information, then <em>why do so many people entrust them to a company whose stated mission is “to make the world’s information universally accessible”</em>?</p>

<p>A company like that should <em>never</em> be entrusted with our sensitive information.</p>

<p>Facebook has made some major blunders regarding privacy, but their mission seems to be something like “can’t we just all get along”. In Facebook’s perfect world, everyone are friends with everyone else. This doesn’t excuse their privacy issues, but at least it tells us that they’re not directly opposed to the idea of privacy. They’re just clumsy and don’t think things through.</p>

<p>Google, however, is different. In the perfect Google world, <strong>privacy does not exist</strong>. In Google’s dream world, I could go take a look at Bill Gates’ emails or Steve Jobs’ search history. or Bono’s shopping list. It is information. It should be made available to the world.</p>

<p>So no, there’s nothing wrong with Google Buzz. It should absolutely broadcast everything Google knows about us to the world. The problem is that Google has been given sensitive information <em>in the first place</em>. Google shouldn’t know anything about us that can’t safely be published through Buzz. If GMail had never existed, Google would not know that the woman in the first example has received emails from her abusive ex-husband, and so they couldn’t have caused her any problems. The only things Buzz would have known about us would be what we told it.</p>

<p>Imagine if Twitter or Facebook had been built by Google, based on their search engine and their ability to categorize and organize information. That is what Buzz could potentially become, and that’d be nothing short of amazing. At least as long as we all take care to keep our emails and other sensitive information <em>far</em> away from Google.</p>

<p>Don’t opt out of Buzz because of privacy concerns. Opt out of GMail instead. Expect every new service Google launches to do as Buzz. Their mission is to make all information available to the world, and they’re going to keep trying. You’re fighting a losing battle. You can keep opting out of their services till the cows come home. It’s always a temporary solution at best. Instead, fix the root issue: Make sure Google is not given any sensitive information about you in the first place.</p>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2010/02/privacy-or-why-i-dont-trust-google-with-my-personal-information/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The downside to “dogfooding”</title>
		<link>http://jalf.dk/blog/2010/01/the-downside-to-dogfooding/</link>
		<comments>http://jalf.dk/blog/2010/01/the-downside-to-dogfooding/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 17:00:05 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[dogfooding]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[visual-studio]]></category>
		<category><![CDATA[Windows Mobile]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=433</guid>
		<description><![CDATA[A term that’s become very popular, and which especially Microsoft’s developers seem to champion, is “dogfooding”. The idea that as a developer, you should use your own products on a daily basis, even during development. This exposes you to all the weaknesses and flaws of the product, and makes you much better equipped to deliver [...]]]></description>
			<content:encoded><![CDATA[<p>A term that’s become very popular, and which especially Microsoft’s developers seem to champion, is “dogfooding”. The idea that as a developer, you should use your own products on a daily basis, even during development. This exposes you to all the weaknesses and flaws of the product, and makes you much better equipped to deliver a product that’s actually <em>worth using</em>.
<span id="more-433"></span></p>

<p>But perhaps there’s a counter-argument that people seem to miss. If you use a lousy piece of software on a daily basis, <em>you get used to it</em>. You stop thinking about how it <em>should</em> be, and only consider <em>how it is</em>.</p>

<p>I think the first place I heard of the term “dogfooding” was on the <a href="https://blogs.msdn.com/windowsmobile/archive/2007/05/04/dogfood-doesn-t-always-taste-good.aspx">Windows Mobile team blog</a>. And let’s be honest, is Windows Mobile really a competitive product? Is it worth using? Perhaps in a vacuum. If all you know is Windows Mobile, then, well, it’s not <em>too</em> bad. But there’s an obvious reason why the product is struggling in the marketplace. Compared to <em>everything else</em>, it feels horrible to use.</p>

<p>Perhaps the recipe for fixing Windows Mobile would be <em>less</em> dogfooding. Windows Mobile developers shouldn’t be forced to use their own buggy, slow, in-development OS all the time on their phones. Perhaps they should be given iPhones and Blackberries. Perhaps some of them should even be given simple old-school non-smartphones. The ones that didn’t need to be rebooted, and didn’t “feature” load times for opening your contacts list, or to write a new SMS (text message). Perhaps they need to be shaken up a bit, and see what <em>else</em> a phone can feel like when you use it. Windows Mobile 6.5 might be better than WM6.0. But that’s not the competition they need to beat. They need to beat the iPhone, they need to beat Android, Blackberry and Symbian. So those are the products they should use at least as much as they use Windows Mobile.</p>

<p>The same may be true for Visual Studio. It’s great that the team <a href="https://blogs.msdn.com/ricom/archive/2009/10/19/my-history-of-visual-studio-part-10-final.aspx">uses Visual Studio 2010 internally</a> as much as possible during development. But that also means that they get used to its performance issues. And it means they get used to the assumption that “this is what an IDE is like”.</p>

<p>Perhaps Visual Studio would be a better product if the team was forced to use Emacs, Vim and Eclipse. Or perhaps even Notepad and makefiles.</p>

<p>And how much better would TFS be, if the developers had used Git or Bazaar instead of <a href="http://blogs.msdn.com/somasegar/archive/2007/06/18/so-what-does-microsoft-use-for-software-development.aspx">dogfooding</a> <a href="http://blogs.msdn.com/granth/archive/2009/08/27/vsts-pioneer-tfs2010-dogfood-server.aspx">TFS</a> during development?</p>

<p>Dogfooding has its advantages, certainly, but I don’t think it <em>alone</em> is a recipe for a good, competitive product. It leads to an incremental improvement over the previous version of your product, but it doesn’t take into account what <em>else</em> is happening in the world. It doesn’t give you the opportunity to question your basic assumptions<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup>. Sometimes, incremental improvement is not what your product <em>needs</em>.</p>

<p>Just a thought.</p>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>Of course I’m not claiming that Microsoft’s developers <em>never</em> use or examine competing products. And likewise, there are obvious benefits to dogfooding, and I’m certainly not claiming that the practice should be eliminated. But I think it is telling that their blog posts frequently mention how heavily they dogfood their products. But they never mention “for this release of Visual Studio, we actually went back and looked at why many people still prefer Vim.”, or “In developing Windows Mobile 7, the entire team was issued phones running various other OS’es, and this taught us what we need to do to finally ship an OS that will take over the world”. <a href="#fnref:1" rev="footnote">↩</a></p>
</li>

</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2010/01/the-downside-to-dogfooding/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>The meaning of RAII — or why you never need to worry about resource management again</title>
		<link>http://jalf.dk/blog/2010/01/the-meaning-of-raii-or-why-you-never-need-to-worry-about-resource-management-again/</link>
		<comments>http://jalf.dk/blog/2010/01/the-meaning-of-raii-or-why-you-never-need-to-worry-about-resource-management-again/#comments</comments>
		<pubDate>Sat, 02 Jan 2010 05:00:52 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[raii]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=340</guid>
		<description><![CDATA[I tried really hard to come up with some witty title or pun to weave into the title of this post. I couldn’t. RAII is just a terrible name, and it isn’t really clever or funny. Unfortunately, it is also the single most important key to C++. It is not just an idiom but a [...]]]></description>
			<content:encoded><![CDATA[<p>I tried <em>really</em> hard to come up with some witty title or pun to weave into the title of this post. I couldn’t. RAII is just a terrible name, and it isn’t really clever or funny. Unfortunately, it is also <em>the</em> single most important key to C++. It is not just an idiom but a fundamental philosophy used to solve almost any problem in the language. So we can’t really avoid it.</p>

<p>If I had to pinpoint one thing that marked the difference between a skilled and an unskilled C++ programmer, it would be “do they understand RAII”. Many people don’t, hence this post.<span id="more-340"></span></p>

<p>RAII is, apart from being badly named, one of those deceptively simple concepts that you <em>think</em> you understand when you first hear of it, think “well duh, that’s obvious”, and then proceed to write code as usual, because you just don’t see how widely applicable it is.</p>

<p>But let’s get the name out of the way first. <a href="http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization">RAII</a> stands for “Resource Acquisition Is Initialization”. And if you’re not already familiar with the idiom, then this has told you <em>nothing at all</em>. If you did know about RAII in advance, then you can, when you stop and think about it, kind of see how the name relates to it… vaguely… sort of.</p>

<p>What it actually <em>means</em> is simple: Resources should be managed by classes. When the class is initialized, the resource is acquired (hence the name). When the class is destroyed, the resource is released. And the lifetime of the object should exactly match the desired lifetime of the resource. That sounds obvious, and many programmers will (assuming they’re working in a language that <em>has</em> classes), say that this is what they always do.</p>

<p>Often, C++ developers think this just means “smart pointers. Wrap your memory allocation in a <code>boost::shared_ptr</code> and you’re done”. I see that as one not-very-often used border case though, rather than a typical example of RAII. So let’s take a step back instead.</p>

<p>The key idea isthat any kind of resource, not just memory, but file handles, sockets, database connections, or even more abstract resources like loggers or profiling timers or textures, really <em>any</em> concept or process which has a lifetime, should be mapped to an object.</p>

<p>Unlike the typical object-oriented line of thought which goes that “everything must be an object, because then.… well, everything will be an object, and your code will be better”, here we actually have a concrete <em>reason</em>: We want to use the object to manage the lifetime of the resource.</p>

<p>When I allocate memory with <code>new</code>, I have to deallocate it again sooner or later, with <code>delete</code>. (Or in C, with <code>malloc()</code> and <code>free()</code> respectively). And I have to make sure that this is done. And I have to make sure that it is not done twice. And that the object is not accessed after this is done. There are a lot of constraints we have to obey, all related to the lifetime of the resource. And this is why unmanaged programs have a reputation of leaking memory left and right. If we allocate memory, and it is to be used by a dynamic number of objects or functions all referencing the same allocations, which of the users is responsible for deleting it? And how do we know when it is safe to delete, when no users remain?</p>

<p>Ironically, most managed languages have <em>not</em> solved the problem. They have added a garbage collector (which yes, is very useful for a wide number of reasons), but that only solves one specific instance of the problem. It takes care of avoiding memory leaks, but it doesn’t avoid resource leaks <em>in general</em>.</p>

<p>The garbage collector ensures that this code won’t leak memory:</p>

<pre><code>void foo() {
  SomeObject* obj = new SomeObject();
  bar(obj);
}
</code></pre>

<p>where without a garbage collector, we’d (at least without RAII) have to write code such as</p>

<pre><code>void foo() {
  SomeObject* obj = new SomeObject();
  try {
    bar(obj);
    delete obj;
  }
  catch(...){ delete obj; }
}
</code></pre>

<p>In the garbage collected case, we don’t know what <code>bar</code> does, and we don’t <em>need</em> to know. It doesn’t have to delete the object. And neither does the <code>foo</code> function. So we have successfully dodged the problem of managing the lifetime of memory allocations. We haven’t really <em>solved</em> the problem though. We still don’t have any good tools to <em>manage</em> the lifetime. We’re just guaranteed by the system that it’ll last <em>long enough</em>.</p>

<p>In C++, this effect can be approximated using some kind of smart pointer<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup>.</p>

<p>Smart pointers allow us to write code like this:</p>

<pre><code>void foo() {
  boost::shared_ptr&lt;SomeObject&gt; ptr = new SomeObject();
  bar(ptr);
}
</code></pre>

<p>and be sure we won’t leak memory. Of course, this solution isn’t perfect — reference counting is much more expensive than a good garbage collector, and if we create cyclic references, the objects will never be deleted, as the reference counts never reach zero. It is a decent approximation, but nowhere near as good and reliable as the garbage collector in managed languages.</p>

<p>But the problem shows up again if we use another type of resource. What if we’d opened a database connection instead?
We’d have to write code such as this:
(The following Java-like pseudocode is copied almost verbatim from <a href="http://stackoverflow.com/questions/161177/does-c-support-finally-blocks-and-whats-this-raii-i-keep-hearing-about/161247#161247">this StackOverflow.com answer</a>, courtesy of <a href="http://stackoverflow.com/users/14065/martin-york">Martin York</a>.)</p>

<pre><code>void writeToDb()
{
  Db db = new Db("DBDesciptionString");
  try
  {
    // Use the db object.
  }
  finally
  {
    db.close();
  }
}
</code></pre>

<p>(And of course it gets even worse if <code>db.close()</code> can throw exceptions. Then we have to catch <em>that</em> exception, just to avoid it propagating out from the <code>finally</code> clause if we reached <code>finally</code> because of an exception being thrown in the <code>try</code> clause.)</p>

<p>The resource management problem still exists. We still have to wrap the code in exception handling just to make sure that the connection is closed as soon as we’re done with it. And we have to do this at <em>every</em> use. And it gets complicated fast.</p>

<p>Of course, .NET makes this a bit simpler:</p>

<pre><code>using (Db db = new Db("DbDescriptionString"))
{
  // use the database object.
}
</code></pre>

<p>But the onus is still on the user of the class to ensure it is closed correctly. There is no obvious way to encode into the <code>Db</code> class that “once we’re done with an object of this type, the connection must be closed immediately”.</p>

<p>And in C++, smart pointers are no longer suitable solutions, since the resource to be managed is no longer a pointer allocated with <code>new</code>.</p>

<p>Instead, a more basic flavor of RAII comes to the fore:</p>

<pre><code>void someFunc()
{
    Db db("DBDesciptionString");
    // Use the db object.
} 
</code></pre>

<p>Yes, that’s all. When the <code>db</code> object goes out of scope, at the end of the function, its destructor is called. The destructor internally calls <code>this-&gt;Close()</code> for us, so we don’t need to do it! We just have to trust the scoping rules of C++, which guarantee that destructors are called on local variables when they go out of scope, and on class members when the class is destroyed.</p>

<p>So in a sense, the key idea in RAII is simply that “resources should behave sensibly”. They should get copied safely if an assignment is made (or otherwise, assignments should be prevented), they should be available if their owning object is successfully created (if it can’t create the resource, it should throw an exception, aborting the creation of the object), and when they are no longer used, they should be cleaned up.</p>

<p>The C++ standard library class template <code>std::vector</code> is a wonderful example of RAII in action. The resources being managed by a <code>vector</code> are memory (the array allocated internally to hold the objects being contained in the vector, as well as the objects themselves. When the <code>vector</code> is destroyed, every object it holds must be destroyed too, and the array in which they were placed must be deallocated.</p>

<p>In the following examples, assume that a function <code>foo</code> is passed a vector of <code>MyClass</code> objects by value. We don’t know how many, if any, objects are stored in it, but since we are passed a copy of the original <code>vector</code>, we take ownership of it. It exists only in the function <code>foo</code>, and must be destroyed afterwards.</p>

<pre><code>void foo(std::vector&lt;MyClass&gt; vec) {
  ...
 //  when we get to the end of the function, all local variables, including vec, 
 // are automatically destroyed by having their destructors invoked.
 // So no matter how many MyClass objects were stored in the vector, it ensures that they too have their destructors called.
 // And the vector also deallocates its internal array, leaving neither of its resources alive at the end of the function
}

void foo(std::vector&lt;MyClass&gt; vec) {
  throw std::exception("Oops");
  // as above, vec is automatically destroyed when we leave the function,
  // regardless of *how* we leave it. Even if we leave it because an exception was thrown and not caught.
} 

void foo(std::vector&lt;MyClass&gt; vec) {
  // other is constructed as a copy of vec. std::vector ensures that both of vecs resources are copied as well
  std::vector&lt;MyClass&gt; other = vec;
  // we now have two vectors, each owning a dynamically allocated array and a number of MyClass objects
  // and again, at the end of the function, both are deallocated cleanly
} 

void foo(std::vector&lt;MyClass&gt; vec) {
  std::vector&lt;MyClass&gt; other; // a second, empty, vector

  // perform an assignment, setting vec to be an empty vector
  // std::vector makes sure that if you do this, the resources previously held by vec are cleanly released
  // before copies are made of the resources held by other
  vec = other;

  // and so when the function ends, the MyClass objects originally held by vec
  // have already been destroyed, so their destructors are *not* invoked now
} 
</code></pre>

<p>As the above shows, <code>vec</code> owns its resources, and manages them tightly. Whenever a change happens to <code>vec</code>, it reflects this by updating its owned resources. If it is destroyed, it destroys its owned resources. If it is copied, it copies the resources it owns. If it is assigned to hold something else, it first destroys its existing resources. And so on. Nothing you do can bring it “out of balance”. It just works. <em>That</em> is RAII. Smart pointers are just convenient adapters turning raw pointers into RAII objects. But RAII is much more than smart pointers.</p>

<p>It is the broad and general idea that <em>resources should be mapped to objects</em>, so that the object can not be created unless it succeeded in acquiring its resource, and it can not be destroyed without also releasing its resource. This effectively saves C++ programmers from having to worry about resource management.</p>

<p>Take an example that’s guaranteed to cause pain without the use of RAII: Handling exceptions being through halfway through constructors. Say you have a class with multiple members which are initialized in its constructor. After the first member has been initialized, but before all of them have been initialized, an exception is thrown. Let’s use the following contrived example:</p>

<pre><code>class Foobar {
  Foo f;
  Bar b;
  MyClass c;

public:
  Foobar() : f(42), b("hello world), c('a') {}
};
</code></pre>

<p>unfortunately, <code>b</code>’s constructor throws an exception. How to handle this? We know that in C++, partially constructed objects do not automatically have their destructors called. when the construction is aborted.</p>

<p>And since we want to avoid any resource leaks, we require that the following must happen:
– <code>a</code> must have its destructor called (because <code>a</code> was successfully initialized before the error occurrd)
– <code>b</code> must release any resources it acquired in its constructor before it threw the exception
– <code>c</code> must do nothing. Its construction was not yet begun when the error ocurred, so it would be an error to attempt any kind of cleanup of <code>c</code>.
– The <code>Foobar</code> object (the object pointed to by the <code>this</code> pointer) must ensure that the above, and nothing else, happens, and it must do so without relying on its own destructor (which won’t be called, as construction did not successfully complete).</p>

<p>And of course, pretending that only <code>b</code> can throw an exception may be a simplification over the real world. Perhaps every member could throw one from its constructor. Care to write a <code>Foobar</code> constructor which takes all this into account, providing enough <code>try</code>/<code>catch</code> blocks to correctly catch every exception that might be thrown, and release exactly the resources that have been allocated until then, and <em>nothing</em> else? A tall order, and an open invitation for bugs. And of course, it’d lead to a huge, bloated and error-prone constructor. It’d also prevent us from using the <em>initializer list</em>. We’d have to perform some kind of “safe” non-throwing default construction of both <code>a</code>, <code>b</code> and <code>c</code> before entering the constructor body, where exception handling is possible, and from there, attempt to perform assignments to bring the three members into the desired state.</p>

<p>In pseudocode, the constructor might look something like this:</p>

<pre><code>Foobar() {
  a = new Foo(42);
  try {
    b = new Bar("hello world");
  }
  catch {
    destroy a;
    throw;
  }
 try {
    c = new MyClass();
  }
  catch {
    destroy b;
    destroy a;
    throw;
  }
}
</code></pre>

<p>Note that all this complexity is only necessary because we want to handle several different resources. <code>a</code>, <code>b</code> and <code>c</code> all contain resources that must be attempted acquired, and properly released if this fails. If there’d been only one resource, the job would have been much simpler. There wouldn’t be any point at which <em>some</em> resources have been acquired, and others have not. If we succeeded in acquiring that one resource, there’d be no risk of errors occurring afterwards, so we wouldn’t need complex conditional cleanup code. And if we failed to acquire the one resource, there’d be nothing to clean up — after all, the resource was never acquired!</p>

<p>So to keep down the complexity, the only safe way to define a class is to make it own <em>at most one</em> resource. And this one-to-one mapping of resources to classes is exactly what RAII is all about. If <code>a</code>, <code>b</code> and <code>c</code> had all been RAII objects, then the above code <em>would work</em>. Regardless of which members could or couldn’t throw exceptions. According to the rules of C++, we know that in the above case,</p>

<ul>
<li>the <code>Foobar</code> destructor (<code>this-&gt;Foobar::~Foobar()</code> will not be called, as <code>*this</code> was not successfully constructed.</li>
<li>the <code>a</code> destructor will be called, as this member was fully constructed at the time of the error.</li>
<li>the <code>b</code> and <code>c</code> destructors will not be called, as these members were not fully constructed at the time of the error.</li>
</ul>

<p>So assuming that <code>b</code>’s constructor takes care of releasing any resources successfully allocated when the error occurred (the number of which, as pointed out above, should ideally be zero), we’re actually home free! What happens is exactly what we listed earlier as our goal. <code>a</code> has its destructor called, <code>c</code>’s constructor was never run in the first place, so it doesn’t have to do anything, and <code>*this</code> doesn’t have to do <em>anything</em> special in its constructor. All of its members take care of their own resources, so the number of resources managed by <code>*this</code> is zero!</p>

<p>We don’t even need to write a destructor for <code>Foobar</code> now, if all its members are RAII objects. Whether the <code>Foobar</code> object is partially or fully constructed, its members take care of themselves. That is the power of RAII. Once a resource has been mapped to a class, we can use it as much as we like, and even in very complex situations, and never have to worry about the resource being leaked. It is managed by its wrapping RAII object, and the C++ lifetime and scope rules ensure that this wrapper object gets destroyed when it goes out of scope</p>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>A smart pointer is an object which behaves as a pointer (meaning that it overloads the <code>*</code> and <code>-&gt;</code> operators, so it can be dereferenced to yield the pointed-to value), but also enforces some kind of ownership semantics on the value. A plain pointer does nothing when it goes out of scope. If it pointed to some dynamically allocated memory, nothing happens to that memory. And if no one else have a pointer to it, then that memory is lost, and can not be reclaimed.
A smart pointer does <em>something</em> when it is destroyed. Some variants simply free the memory they point to (<code>boost::scoped_ptr</code>, <code>std::auto_ptr</code> or <code>std::unique_ptr</code> all fall into this category, although with some important differences), while others implement reference counting, so that the memory is only destroyed when <em>all</em> smart pointers pointing to it have been destroyed. <code>boost::shared_ptr</code> is by far the best known implementation of this concept. <a href="#fnref:1" rev="footnote">↩</a></p>
</li>

</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2010/01/the-meaning-of-raii-or-why-you-never-need-to-worry-about-resource-management-again/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Hopes for 2010: Games for Windows Live</title>
		<link>http://jalf.dk/blog/2009/12/hopes-for-2010-games-for-windows-live/</link>
		<comments>http://jalf.dk/blog/2009/12/hopes-for-2010-games-for-windows-live/#comments</comments>
		<pubDate>Thu, 31 Dec 2009 17:00:32 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Games]]></category>
		<category><![CDATA[gfwl]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[new-year]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=389</guid>
		<description><![CDATA[I’m sorry. This isn’t going to be pretty.

But then again, neither is the “service” known as Games for Windows Live we PC gamers are being forced to swallow. So far, you guys have done an amazing job trying to eliminate the inconvenience known as PC gaming. You’ve done more than I’d thought possible to make [...]]]></description>
			<content:encoded><![CDATA[<p>I’m sorry. This isn’t going to be pretty.<span id="more-389"></span></p>

<p>But then again, neither is the “service” known as Games for Windows Live we PC gamers are being forced to swallow. So far, you guys have done an <strong>amazing</strong> job trying to eliminate the inconvenience known as PC gaming. You’ve done more than I’d thought possible to make it as painful as humanly possible. The mere presence of your logo is enough to make every PC gamer I know want to go have a lie down.</p>

<p>GFWL isn’t just the infamous polished turd. Partly because it is in no way polished, but mainly because it is not just a turd, which is a passive object that, while smelly, can be easily walked around and simply <em>avoided</em>, but a turd-being-thrown-in-your-face, an entity which actively tries to ruin your day. Most bad PC software can simply be avoided — we just choose not to use it — no such luck with GfWL. We don’t have a choice in the matter.</p>

<p>Here is a short, and incomplete, list of what you need to fix to even reach neutral ground. Fixing these atrocities is enough to nearly make your service tolerable. To actually make it an <em>asset</em>, something that enhances the value of the PC platform, you have to reach far far beyond this. But just making it less nauseating to use would be a wonderful start, and should mean that you’ll have your hands full for the entire year.</p>

<p>But enough ranting. On with the list:</p>

<ul>
<li>Fix the friend list. When I want to invite a friend to a game, the correct approach is: 1) Hit a key to pull up the friends list. 2) Click on my friend. 3) click “invite to game”. You may have noted that this is what Steam does, and has done for years. Inviting a friend to a game takes perhaps one second there. Now, since it’s pretty obvious none of you have ever attempted to do this in GFWL, I should probably explain how it works (or fails to work) there: First, you hit a key which conflicts with <em>every</em> game that has a chat box: <code>home</code>, the key which is normally used to move the caret to the beginning of the line/text input. Then you wait for the screen to go dim and the slow and painful animation of the GWFL client unfolding. You now get to some kind of “main menu”, from which you can do absolutely nothing. Here, you click on the “Friends” button, and again, wait for the animation to finish. <em>Now</em> you get your friends list, from which you may.….. click on a friend, and wait for yet another animation to finish. You may then click “Invite”, and you get, once the next animation has finished, a goddamn email interface! Then you click send, and the request is sent off, in a semi-random language (see one of the following points). Or, of course, you may sometimes go directly to the email interface, from where you can either 1) fill out your friends name from memory, or 2) click the “to” field to bring up (slowly, after another animation), a list of every goddamned person you’ve ever played a game with. In short: When I want to invite a friend to a game, I do not want to send an email. I don’t want to see the list of several hundred jerks whom I was matched against in earlier games. I want to see a list of my friends, the ones I have personally indicated an interest in play with by adding them to my friends list, click on the right one, and click “invite”. </li>
<li>Fix the language setting. I don’t want to get game invites in Norwegian just because someone from Norway is hosting the game (as pointed out above, I don’t need an actual message from them at all! Just give me a choice of clicking “accept/decline invitation, not an actual email message). I also want to be able to set the language myself. Yes, I use Danish regional settings, but that’s because I want the time and date formatted that way, not because I want to read your dodgy Danish translations in the GFWL interface. And despite those regional settings, I’m running an English copy of Windows, for precisely this reason: My english is good enough that I prefer your products in their original language, sidestepping all the inevitable translation issues. Said Norwegian friend literally didn’t understand what GFWL was trying to tell him when he first logged in. The so-called translation was unintelligible. Of course, another Norwegian friend, and god knows why, as they’ve compared all regional settings they could find and couldn’t find any meaningful difference, got GFWL in english. Why? I’ve gotten Danish on a few occasions, but luckily, so far, it has mostly been in English. But even so, I want 1) to be in control of the language to use, and 2) proper translations if you’re going to translate. And 3) as long as you don’t allow me to choose the language, I at least want it documented <em>how</em> GFWL determines which language to use. Then I can change that particular setting in Windows, and get the language I actually wanted to begin with.</li>
<li>Make the GFW logo (the non-Live one, to begin with) synonymous with actual quality. Gears of War, one of your flagship titles was so badly ported it’s scary. Not only did the installer take the better part of two hours to complete, it was also buggy and required a patch to even launch the game. Which then didn’t work on 3 out of the 4 computers I’ve tried it on. This may surprise you, but to enforce a minimum quality across PC games, it is not enough to design a new logo, you also have to verify that the games that get the logo actually do behave sanely and <em>actually work on a PC</em>. You have to ensure not just that the game works with a 360 controller, <strong>but that the game works better than those without the logo</strong>. </li>
<li>The services you provide to games tied to your service should be better than what they’d have made themselves otherwise. Dawn of War 2’s matchmaking is an atrocity. It matches new players against the most hardcore, it frequently takes minutes to find a match <em>even in Last Stand games where there’s only one team and it was full to begin with, so the total number of players it has to find is a big fat <strong>zero</strong></em>. And NAT errors are frighteningly common. More so than Relic’s previous game, Company of Heroes. Or Dawn of War before it. Relic’s own homebrewed matchmaking and NAT-traversal code worked better than that being made available by you for the betterment of PC gaming.</li>
<li><a href="http://www.rockpapershotgun.com/2009/01/27/fallout-3-new-content-adventures-in-gfwl/">Make it possible to navigate and use your Marketplace.</a></li>
<li>Listen to <a href="http://www.rockpapershotgun.com/2008/03/03/boycott-games-for-windows-live/">community feedback</a>. When high-profile games journalists have nothing positive to say about your service, you have a problem. When “ordinary” gamers feel just as bad about it, you have a <em>big</em> problem. Perhaps a good start would be to provide somewhere for users to leave feedback. Put it on Microsoft Connect. Or perhaps open a blog for GFWL specifically (rather than extending whatever XBL-related blogs you already have to also cover GFWL). Perhaps just add a “Contact” or “Feedback” link on the GFWL website, even. I’ve spent far more time than is reasonable looking for a place to provide feedback, and failed to find anything.</li>
<li>Listen to <a href="http://www.rockpapershotgun.com/2009/10/01/stardocks-wardell-slams-gfwl/">developer feedback</a>. Even if you manage to convince games journalists and actual gamers of the benefits of your service, you need to get developers on board as well. </li>
<li>Make the service for PC users. Showing us images of the 360 controller is just a bad joke. Yes, we <em>may</em> have bought one of those, and it may even be connected to our PC, but the default mode of control is mouse and keyboard. Deal with it. Telling us to press the A button is not helpful. Showing an icon of the 360 controller in the main GFWL bar only serves to make it look like you ported the service straight from the XBox, without changing a line of code.</li>
<li>And finally, your service has to provide us with some kind of value. What exactly do I gain from one of my games being GFWL-enabled that I wouldn’t have gotten if it was 1) Steam-enabled instead, or 2) old-fashioned not-tied-to-any-thirdparty-online-service?</li>
</ul>

<p>So dear Games For Windows Live team… Is 2010 going to be the year when you finally cancel out the pain caused by your service? I’m not asking for miracles, I don’t want you to makeanything that adds <em>positive</em> value — I just want you to stop subtracting value from the product that uses your service. Please? Is it at least going to be the year when you start soliciting feedback? Open a blog? Open a connection on MS Connect? Provide a feedback email address? A Twitter account? Anything that might show that you’re not just a bunch of monkeys banging blindly on keyboards.</p>

<p>Well, like I said, harsh, but sometimes, the truth hurts. And it is nothing compared to how much your product currently hurts PC gamers.</p>

<p>So please, tell me that when 2011 rolls around, I’ll be able to write a more upbeat post about my hopes for you in the following year.</p>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2009/12/hopes-for-2010-games-for-windows-live/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hopes for 2010: Microsoft Visual C++</title>
		<link>http://jalf.dk/blog/2009/12/hopes-for-2010-microsoft-visual-c/</link>
		<comments>http://jalf.dk/blog/2009/12/hopes-for-2010-microsoft-visual-c/#comments</comments>
		<pubDate>Wed, 30 Dec 2009 17:00:24 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[ide]]></category>
		<category><![CDATA[intellisense]]></category>
		<category><![CDATA[msvc]]></category>
		<category><![CDATA[new-year]]></category>
		<category><![CDATA[visual-studio]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=387</guid>
		<description><![CDATA[As I mentioned earlier, I’d like to celebrate the new year by calling out a few products I’d like to see improved in the new year.

First in line is Microsoft’s C++ compiler and IDE.

From you, what I’d like to see in 2010 is actually fairly simple (at least conceptually): rethink your IDE. The Visual Studio [...]]]></description>
			<content:encoded><![CDATA[<p>As I mentioned <a href="http://jalf.dk/blog/?p=352">earlier</a>, I’d like to celebrate the new year by calling out a few products I’d like to see improved in the new year.</p>

<p>First in line is Microsoft’s C++ compiler and IDE.<span id="more-387"></span></p>

<p>From you, what I’d like to see in 2010 is actually fairly simple (at least conceptually): rethink your IDE. The Visual Studio team as a whole is already <a href="http://blogs.msdn.com/ricom/archive/2009/10/19/my-history-of-visual-studio-part-10-final.aspx">doing this in a big way</a> with VS10. I’m hoping you can find the time to reinvent the C++ IDE specifically as well.</p>

<p>For the past decade (except for the 5 years you wasted trying to eliminate native C++), you’ve been trying very hard to write the ultimate IDE for the wrong language. You’ve got something that works pretty well for C++ code anno 1993 or so. And which completely falls apart when used for more modern C++. Why do you persist in putting so many resources into making Intellisense better, when it <em>still</em> has no way to deal with a simple template function? Isn’t that a hint that you should rethink your approach? Modern C++ has quite a bit in common with dynamic languages. The type of a function parameter may not be known just by looking at the function definition. Often a full-fledged “interactive mode” as is common in dynamic languages, is desired, for example if we want to see what the actual type of some template parameter is. Or when instantiating template metaprograms, we may wish to step through them line by line at compile-time, rather than being limited to looking at the compiler errors — the metaprogramming equivalent of <code>printf</code>–debugging. What I’d like to see from the MSVC IDE in the coming years is an acceptance and support of modern C++ paradigms — generic programming, template metaprogramming and all the difficulties that implies. Don’t give me an IDE that tries to provide Intellisense for a program with a completely static structure. If you’re going to do Intellisense, make it able to handle the very flexible type system enabled by templates. take a leaf from the JavaScript support in your IDE, which supports intellisense even though the language is dynamic and types generally aren’t known until runtime. So to display type information in the IDE while the code is being written, they have to be clever. But they can do it.</p>

<p>In C++, the types aren’t known until compile-time, so from the IDE’s point of view, the problem is similar. To display type information while the code is being written (and before it is compiled), the IDE has to be clever. And at the moment, it isn’t. At the moment, Intellisense just gives up.</p>

<p>Let’s take a simple example. What should the IDE do about this function:</p>

<pre><code>template &lt;typename T&gt;
typename T::return_type foo(T arg){
  bar(arg);
  return arg.baz();
}
</code></pre>

<p>If we play it by the book, and demand a “perfect” solution, there is nothing the IDE can do. It doesn’t know what <code>T</code> is, so it can’t help us with autocompletion, suggesting members after the dot, or anything else.
But if we’re willing to think outside the box, and accept a success rate lower than 100%, there are several strategies the IDE <em>could</em> use to provide meaningful Intellisense information:</p>

<ul>
<li>we could look at the call sites. They must logically provide types that are valid in this context. We could find one call site, and provide Intellisense on the assumption that the type <code>T</code> is whatever was passed at <em>that</em> call site. That wouldn’t be 100% accurate in all cases, of course, but it would give us a type that works with the function, so it would be useful. It could even look at several call sites, and compute the union of the types used. If they all provide a <code>frobnicate()</code> method, then the IDE could assume that <code>T</code> inside the function <code>foo</code> always contains such a member.</li>
<li>We could look at how the type is used in the function. It must have a copy constructor (because it is passed by value), it must have some nested type <code>return_type</code>, and it must have a no-arg <code>baz</code> member function, which returns something convertible to that type. And it must be convertible to whatever arguments <code>bar</code> expects. This probably isn’t a complete description of the type, but it would be enough to give us some limited Intellisense information at least. The compiler might be able to deduce some information about the type. We could even generate some kind of ad-hoc “concepts” implementation — perhaps not as extensive as that which was proposed in C++0x (and subsequently dropped), but a kind of helper datastructure that the IDE can attempt to map onto unknown template types.</li>
<li>Or we could allow the user to specify an example of a valid parameter type, and then use that to generate Intellisense information from.</li>
</ul>

<p>But an alternative approach (and these aren’t mutually exclusive) might be to reduce the reliance on Intellisense, which is essentially a static analysis tool. Perhaps a better approach would be to bring the “Immediate” pane up to date, and make it useful, not just during debugging, but while programming as well. Why can’t I through the intermediate window ask for the class <code>std::vector&lt;bool&gt;</code> to be instantiated, for example, so that I can inspect its structure? Perhaps I’m curious what its <code>iterator</code> type will resolve to, or perhaps I want to know the size of the class or other static information. Why can’t I just ask the IDE for this information? Again, modern C++ has a lot in common with dynamic languages. Very little information can be reliably extracted without compiling the code. So give me the tools for optionally and temporarily compiling bits and pieces.</p>

<p>When I write silly template metaprograms to compute the N’th prime number, and the result is wrong, why doesn’t MSVC provide a compile-time debugger? One which lets me step through the instantiation of this maze of templates, inspect the members of each, and find out where it went wrong, where it instantiated the wrong template, or where I forgot to write the specialization I intended.</p>

<p>In far too many ways, the C++ IDE really feels like a C IDE. Most of it doesn’t seem to know that there’s this new-fangled thing called “templates”, or that they change how people write code. The Immediate window or the debugger, do not recognize template parameter names. If I am debugging a function <code>template &lt;int I&gt; void foo()</code>, why can’t I get the debugger to tell me the value of  <code>I</code>? It should be absolutely trivial to do. But the debugger can’t seem to do it. Intellisense can’t seem to do it. The Immediate pane can’t seem to do it. There’s a clear mismatch between the compiler, which is clearly a C++ compiler, and pretty much hasn’t bothered about the C side for close to a decade, and the IDE which still seems to be trying to be the perfect C IDE, completely disregarding every feature unique to C++.</p>

<p>I know you’re used to being told that you have one of the best IDE’s in existence. I beg to differ. You may have got one of the best C IDE’s, and your C# and VB IDE’s kick some serious butt. But your C++ IDE is essentially nonexistent. Your IDE does not support C++. It supports a marginally and conservatively extended C.</p>

<p>So far, I’ve dealt exclusively with the IDE issues, and that’s not a coincidence. On the whole, I’m quite happy with the MSVC compiler. The <a href="http://blogs.msdn.com/vcblog/archive/2009/11/02/visual-c-code-generation-in-visual-studio-2010.aspx">performance</a> of generated code is good; you’re making great progress on <a href="http://blogs.msdn.com/vcblog/archive/2009/04/22/decltype-c-0x-features-in-vc10-part-3.aspx">C++0x support</a>, and overall, you’ve got a compiler I’m happy with. Of course there are still a couple of areas where the lack of standards-conformance is embarassing (never mind the <code>export</code> keyword, I’m more bothered about two-phase name lookup and other <em>relevant</em> features), and there are some features I wish you’d borrow from GCC, but on the whole, and I wish you’d tighten up your warning messages a bit (some of them are nothing more than noise, or are impossible to avoid in “good” healthy code), I have relatively few <em>serious</em> complaints about the compiler. I do, however, have a few suggestions.</p>

<p>It seems to me that the source/header compilation mechanism could use a makeover. We can’t change the actual semantics (yet — hopefully the proposal for a module system for C++ gains traction), but the compiler <em>can</em> change how it actually processes the code. And yet, major compilers still process the source files in the exact same manner they did 20 years ago. Even though this is, on today’s machines, and with today’s huge codebases, ridiculously inefficient.</p>

<p>Ages ago, precompiled headers were invented, but I’m not really a fan of them. It’s a hackish solution which sometimes helps, but may also hurt, due to the tendency towards including everything in one single “blob” header. Even if that header is precompiled, it still means everything that includes it has to deal with these bloated monolithic symbol tables and other data structures. More importantly, it is a fragile solution, as <a href="http://blogs.msdn.com/vcblog/archive/2009/11/12/visual-c-precompiled-header-errors-on-windows-7.aspx">the VC Team’s own blog shows</a>.</p>

<p>But why can’t this mechanism be generalized?
Why can’t the compiler process every header in isolation, build a complete parse tree of each one, and store those on disk? And then, when the header is included, rather than reading and parsing the header again, simply load this parse tree and merge it into the rest of the compilation unit. Of course, it is easy to come up with cases where the file may have to be parsed differently depending on where it is included, but in 99.9% of all cases, the inclusion mechanism is straightforward and simple: The header is typically not included in the middle of a class definition or from inside a namespace. It usually only reacts to a few fixed macros that may be defined before the header’s inclusion. So <em>most</em> of the time, the header could be precompiled in isolation and reused. And for the few cases where the changed state actually matters, where the header is included in the middle of a function definition or with no include guards or where a macro (say <code>CreateWindow</code>, or a similarly common name, <em>cough cough</em>) mangles the contents of the header, in <em>those</em> cases, the compiler can simply fall back to the traditional source code inclusion and subsequent compilation of the translation unit. Even if these precompilation passes aren’t stored to disk in the manner of precompiled headers, they could still be kept in memory, and reused between translation units during a build. If N <code>.cpp</code> files all include a certain header, it would allow that header to be compiled once, rather than N times.</p>

<p>Once again, we have something that feels like a leftover from C. In C, headers were mostly forward declarations and little actual <em>code</em>, so naive processing of headers worked fairly efficiently. in C++, it is getting more and more common to put huge amounts of code in headers, which means that the naive compilation strategy traditionally used for C becomes ridiculously slow and inefficient. Creating a truly <em>general</em> replacement strategy is nearly impossible, true, but it seems like it’d be possible to create a heuristic that’d enable more efficient processing of header files 99% of the time, and which could then fall back to the traditional method of copy/pasting headers into the translation unit for the last percent of cases.</p>

<p>And why does every translation unit have to read every file every time? Can’t their contents be kept in memory, at least for a short time? Those hundreds or thousands of file accesses are painfully slow. Windows already exposes APIs for monitoring file changes, so it should be fairly simple to determine when a source file has been modified, and only then flush it from memory.</p>

<p>And of course, everyone’s favorite nitpick: Why is <code>windows.h</code> so absolutely horrible? Why does it have to be one monolithic header which gives us <em>everything</em> Windows has to offer? Why doesn’t it compile as standard C++? Why does it include so many other headers (as above, slowing down compilation)? Why does it pollute the global namespace with macros for ridiculously common names?</p>

<p>Well, it does, and it’d be silly to expect this to change, due to backwards compatibility concerns.
But why then, is there not a <code>windows.hpp</code> or similar? Why isn’t there a separate cleaned-up, C++-compatible header? One which uses function overloading instead of macros, for example? Or which just defines simple forwarding functions instead of macros? One which compiles even with the non-standard language extensions disabled? Or why isn’t there a <em>set of</em> these headers, allowing us to access the bits of the Windows API we’re interested in, without having to include *everything?</p>

<p>In short, I think the MSVC IDE (and in some cases the compiler too) is in desperate need of a rethink. Out with those 12-year-old project wizards, which create complex predefined project structures accumulating every bad practice and unexpected project setting in one place. I’ve lost count of how many beginners I’ve seen choke because their tiny little projects automatically get a precompiled headers thrown in for absolutely no reason. Out with the idea that C++ can best be presented like C#, as a static language where every piece of code can be understood in isolation. Instead, give us an IDE that treats C++ as a more dynamic language, where many types of information are just not available until the program has been compiled, and which embraces the unique features of C++ — one which supports and encourages use of templates, one which accepts that in modern C++, most code ends up in header files, and these header files become expensive to compile, and so are an area ripe for optimization. An IDE which treats C++ compilation as an interactive process, where template instantiation can be stepped through and inspected at each stage, and where interactive queries can be made statically or during debugging to inspect not just data, but also types.</p>

<p>Another addition that would really boost the usefulness of MSVC would be to provide facilities for template metaprogramming in unit tests: For example, it is common to use metaprograms to force compilation failures if a template is instantiated with a specific type. But how do we test that this works as intended? Give us the hooks and language extensions necessary to specify that “this function is expected to fail to compile, and if it does, that’s not an error, just remove the function and compile the remainder of the file”. Again, consider compilation process a part of the language — it is something that must be inspected and debugged, and for which we may wish to write tests.</p>

<pre><code>void my_testcase() __declspec(wontcompile){
  // perhaps we want to ensure that the template can not be instantiated with a reference,
  // so we expect this test to fail
  frobnicator&lt;int&amp;&gt; f; 
}

// if the above test fails to compile (as it should), the compiler should simply ignore it, and proceed to compile the other tests, rather than aborting
void next_testcase() {... } 
</code></pre>

<p>Target your IDE at Modern C++, rather than C with classes. Impress the world by being the first IDE to even think about this. Embrace, and provide support for, the changes that have happened in the C++ language, in best practices and in the mindset of the C++ community. Accept that yes, headers are ridiculously heavy these days, and blindly recompiling them for every translation unit doesn’t scale. Accept that the C++ language needs IDE support to inspect what happens in our compile-time metaprograms as well as at runtime. And face up to the fact that traditional intellisense is a lost cause. There is no way to statically produce all the information we expect from intellisense. Some can be improvised by various heuristics, or as in the template function case, by assuming some suitable dummy value for the function’s template parameters, but others may be nearly impossible to provide until the program has been compiled. So perhaps you need to think beyond Intellisense to provide this information to the programmer. Perhaps an “interactive mode” would be more suitable. If the IDE can’t provide the information I need automatically, it could at least allow me to query for the information. Perhaps it can’t tell me anything about the template type <code>T</code>, but why can’t I tell it to assume that <code>T</code> is a <code>std::wstring</code>, and provide information based on this assumption. Or something else entirely. You already have a pretty good C++ compiler. It’s time to start working on a C++ IDE, and call it a day on the C IDE you’ve been polishing until now.</p>

<p>So dear MSVC team, in case you can’t think of anything useful to do with your time in the year 2010 (as if… I know you’ve got C++0x support to work on, and that’s infintely more important to me than IDE improvements), here’s a new year’s resolution for you: Amaze the world, by showing what a C++ IDE <strong>should</strong> work like. Reinvent the role of the C++ IDE, instead of trying to force the C# or C IDE to work for C++ as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2009/12/hopes-for-2010-microsoft-visual-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Happy new year — Hopes for 2010</title>
		<link>http://jalf.dk/blog/2009/12/happy-new-year-hopes-for-2010/</link>
		<comments>http://jalf.dk/blog/2009/12/happy-new-year-hopes-for-2010/#comments</comments>
		<pubDate>Tue, 29 Dec 2009 17:00:29 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Meta]]></category>
		<category><![CDATA[new-year]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=352</guid>
		<description><![CDATA[So it’s the new year. Yay! How time flies when you’re having fun.

I don’t know about you, but I had a blast this last year. New (awesome) apartment, started on my thesis, launched this blog, and just generally had a good time.

So how to follow it up? What can 2010 do to beat this?

Rather than [...]]]></description>
			<content:encoded><![CDATA[<p>So it’s the new year. Yay! How time flies when you’re having fun.</p>

<p>I don’t know about you, but I had a blast this last year. New (awesome) apartment, started on my thesis, launched this blog, and just generally had a good time.</p>

<p>So how to follow it up? What can 2010 do to beat this?</p>

<p>Rather than coming up with a whole bunch of new year’s resolutions for myself, I thought it might be more interesting to reverse the process — and say what I’d like to see <em>others</em> do in the coming year.</p>

<p>Throughout the year, I’ve encountered a lot of software products that, for one reason or another, I’d like to see improve. Perhaps they simply suck currently, and desperately need to be fixed — or perhaps they’re already so good that I’m happy to use them, but I can think of further improvements that’d really make the “best in class”.</p>

<p>So for the next couple of days, I’ll post my thoughts on what I’d like to see from a few of these products during the coming year.</p>

<p>Hope you have a great new year’s eve! See you next year.</p>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2009/12/happy-new-year-hopes-for-2010/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adventures in Microoptimizations</title>
		<link>http://jalf.dk/blog/2009/12/adventures-in-microoptimizations/</link>
		<comments>http://jalf.dk/blog/2009/12/adventures-in-microoptimizations/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 07:10:49 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[cpu]]></category>
		<category><![CDATA[low-level]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=425</guid>
		<description><![CDATA[A friend recently asked me for “the simplest optimization problem I could think of”. This led to a fun discussion of low-level optimization and how the CPU executes your code. And so I decided to share it here.

Let’s make it clear though, that the following will have very little practical use. We’re not just into [...]]]></description>
			<content:encoded><![CDATA[<p>A friend recently asked me for “the simplest optimization problem I could think of”. This led to a fun discussion of low-level optimization and how the CPU executes your code. And so I decided to share it here.<span id="more-425"></span></p>

<p>Let’s make it clear though, that the following will have very little practical use. We’re not just into “it doesn’t make a measurable difference” territory, but also deep into “the compiler will do this for you”. So please, don’t try to apply these “optimizations” to your real-world code to save a clock cycle.</p>

<p>This is merely intended as a thought experiment, illustrating some of the factors that makes performance so difficult to predict. And now, with that disclaimer in place, let’s get on with it:</p>

<h1>The problem</h1>

<p>The “problem” I came up with was the evaluation of <code>x+x+x+x</code>. This was the simplest snippet of code I could think of for which optimization is possible. For the sake of this discussion, let us assume that <code>x</code> is an integer.</p>

<p>A naive compiler will evaluate this code as <code>((x+x)+x)+x</code>. In other words, it will evaluate one addition, feed that result to the second addition, and then finally feed the result of that to the third addition.</p>

<h1>Optimization #1</h1>

<p>The optimization I suggested was to evaluate it as <code>(x+x)+(x+x)</code> instead. And why is this faster?
A modern CPU is superscalar — that is, it is able to execute multiple instructions every clock cycle. Depending on the CPU model, it can probably execute three or four instructions belonging from the same thread every cycle.</p>

<p>So where the original version would take three times the duration of an <code>add</code> instruction to execute, my optimization can be done in two times the duration: Both the initial subexpressions can be evaluated <em>in parallel</em>. And so, after only the duration of <em>one</em> <code>add</code> instruction, we’ve got the result of two of the additions, and can perform the third and final one. So in this very simple case, we actually reduced the run time by 33%. Not bad, eh?</p>

<h1>Optimization #2</h1>

<p>My friend then asked if <code>x*4</code> would be an optimization as well. And now it gets a bit more interesting.</p>

<p>First, of course, <code>x*4</code> is just a single multiplication. Is that faster than three additions? Is it faster than two additions (which is the time it’d take for my “optimized” version to run)?</p>

<p>That depends on the speed of a multiplication instruction. On common CPU’s, a moment’s research tells us that <code>add</code> has a latency of 1 cycle, and <code>mul</code> has a latency of 3 cycles.<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup>, so the multiplication takes as long as the original unoptimized version.</p>

<h1>Evaluation</h1>

<p>So what does this mean? That at a glance, optimization #1 is faster than #2, certainly. #1 yields a result after two clock cycles, where #2 takes a whopping <em>three</em> cycles.</p>

<p>But there are other factors at play. Sometimes the multiplication version may be more efficient. The CPU has a limited number of execution units. It can also only decode a limited number of instructions at a time.</p>

<p>The version using addition requires three instructions to be decoded, and uses two execution units during the first cycle, and one unit in the second. All in all, we’re occupying three “execution-unit cycles”. The version using multiplication does take three cycles, but because modern CPU’s are pipelined, it only occupies the execution unit during the first cycle. In the second cycle, the execution unit is able to begin on a new instruction, while continuing to process the <code>mul</code> instruction. So this version only requires one “execution-unit cycle”. In other words, we’ll free up other execution units so they can execute other instructions. We’re also freeing the front-end from having to decode three instructions.</p>

<p>So we now know that:</p>

<ul>
<li>If we need the result as soon as possible, the optimized <code>add</code> version will be more efficient because it finishes sooner.</li>
<li>But if we need to execute a lot of other instructions as well, the <code>mul</code> version will be more efficient because it uses fewer hardware resources on the CPU</li>
</ul>

<p>What if we have a lot of instructions we want to execute <em>and</em> we need the result soon? Or if we only have these instructions to execute, and we don’t care about when we’ll get the result (perhaps the next operation is to add the result to that of an ongoing division, which is <em>very</em> slow, so it won’t matter if we take 2, 3 or 15 cycles to get ready)? Hard to say. Either one may be preferable.</p>

<p>Of course on x86 CPUs we also have to take the variable instruction length into account. How many bytes does a <code>mul</code> instruction take? What about three <code>add</code>s? That affects both how much data has to be read from memory and how much space will be taken up in CPU cache, and so that should be taken into account as well.</p>

<p>So what can we learn from this? Mainly that performance is nontrivial. Never assume that you can tell whether some code is “fast” or “slow”. And be especially careful with assumptions about how it can be improved. It is very possible that your “optimization” will actually run slower.</p>

<p>Whenever you optimize code, do as the <a href="http://blogs.msdn.com/ricom/archive/2003/12/02/40779.aspx">pros</a>: <a href="http://blogs.msdn.com/ricom/archive/2007/06/13/partly-sunny-chance-of-showers-bring-an-umbrella.aspx"><em>measure, measure and measure</em></a>. Measure the speed of the original code. Measure the result of the optimized code. Be careful with the many ways in which your measurement can be invalidated (by the compiler optimizing away the code you wanted to test, or by the CPU cache changing the result in your test case from what you’d expect in the real world by caching — or not caching — the data you’re operating on).</p>

<p>And when performing low-level optimizations, another vital piece of advice is to <em>understand the hardware</em>. Know which instructions are being executed, know the cost of instructions on the relevant hardware, and know what other tricks the hardware uses (Your CPU is probably superscalar and pipelined, and processes instructions out of order. It probably also has a cache of a certain size, with a specific cache line size, and a certain associativity. It has a fixed number of execution units, a known pipeline length and so on. And while we’re at it, the memory subsystem matters too. How long does it take to access RAM? How can the CPU reorder reads and writes? What is its policy for writes? When are they pushed from cache to RAM? If you want to optimize your code on the instruction level, you <em>need</em> to know your CPU. Even the simplest code is affected by dozens such factors, any of which might make a difference.</p>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>For the sake of this example, let us assume that simple multiplication and addition instructions are used. Some CPU’s may have more complex instructions that, for example, can perform the multiplication faster if the second operand is a power of two. And of course we could implement the multiplication as <code>x &lt;&lt; 2</code> too. <a href="#fnref:1" rev="footnote">↩</a></p>
</li>

</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2009/12/adventures-in-microoptimizations/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>OpenID 2.0 and HTTP redirects</title>
		<link>http://jalf.dk/blog/2009/12/openid-2-0-and-http-redirects/</link>
		<comments>http://jalf.dk/blog/2009/12/openid-2-0-and-http-redirects/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 23:48:36 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Meta]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[openid]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=417</guid>
		<description><![CDATA[Ever since I signed up on StackOverflow.com roughly a year ago, I’ve had an OpenID. On the whole, I think it’s a great concept, and I wish more sites would allow me to sign in with it.

However, a few things have been bothering me about it.

When I first signed up, I did a bit of [...]]]></description>
			<content:encoded><![CDATA[<p>Ever since I signed up on <a href="http://stackoverflow.com/">StackOverflow.com</a> roughly a year ago, I’ve had an OpenID. On the whole, I think it’s a great concept, and I wish more sites would allow me to sign in with it.</p>

<p>However, a few things have been bothering me about it.<span id="more-417"></span></p>

<p>When I first signed up, I did a bit of research, and found out that you can use your own domain as your OpenID. You simply enter the following in a <code>&lt;head&gt;</code> of a HTML page you control:</p>

<pre><code>&lt;link rel="openid.server" href="http://myopenidprovider" /&gt;
&lt;link rel="openid.delegate" href="http://myopenid-at-that-provider/" /&gt;
</code></pre>

<p>And the URI of that HTML page can now be used as your OpenID. It will forward authentication requests to the specified provider. This gave me a nice clean URI to use as my OpenID, and as a bonus, it meant that I could change my OpenID provider and keep my ID, just by editing this HTML.</p>

<p>Of course, I quickly found out there was a downside as well. When I created this blog, I placed it in <code>http://jalf.dk/blog</code>. I figured I could easily add a redirect from <code>http://jalf.dk</code> and so it wouldn’t matter in the long run.</p>

<p>When I tried adding this redirect, I realized that this of course would also redirect any OpenID requests. My OpenID provider would then see a login attempt from <code>http://jalf.dk/blog</code> instead, and all hell would break loose.</p>

<p>So I removed the redirect, and instead placed this message in <code>http://jalf.dk/</code> along with the OpenID <code>&lt;link&gt;</code> tags:</p>

<blockquote>
  <p>Please go <a href="http://jalf.dk/blog/">here</a> for my blog. Sorry for the lack of a proper redirect.</p>
</blockquote>

<p>Not very elegant, but it worked. OpenID requests were handled correctly, and readers of my blog could follow the link, or just bookmark <code>/blog</code> in the first place.</p>

<p>Today, a friend asked me why I didn’t have a redirect, and I explained the above problem. I didn’t think about it any further until half an hour ago, when I realized that Facebook can be tied to an OpenID account. As I said before, the more services I can log in to with my OpenID, the better, so I attempted to add my OpenID… And got a nasty error message telling me that my OpenID only supported version 1.1, and Facebook required 2.0.</p>

<p>Geez, I hadn’t even realized there were multiple versions.</p>

<p>So I went hunting for a solution. And it turned out to be pretty simple, and have the nice side effect of solving the redirection problem as well!</p>

<p>It turns out that the <code>&lt;link&gt;</code> tags embedded in HTML only work for OpenID 1.0 and 1.1. For 2.0, you have to provide a YADIS XML file.</p>

<p>Unfortunately, there seems to be very few examples online of what this file should <em>look like</em>.
I did find a nice example of using a YADIS file for OpenID 1.0 <a href="http://blog.paulisageek.com/2009/06/easy-openid-delegation-with-yadis.html">here</a>, which got me started. The Wikipedia article on <a href="http://en.wikipedia.org/wiki/Yadis">YADIS</a> held another example, but again only with OpenID 1.0. However, it also shows how to specify LID 2.0, so while I have no clue what LID is for, at least it gave a hint of how to support multiple versions.</p>

<p>Finally, diving into the <a href="http://openid.net/specs/openid-authentication-2_0.html">specification for OpenID 2.0</a>, I discovered the correct URI to specify as <code>&lt;Type&gt;</code> in the YADIS file: <code>http://specs.openid.net/auth/2.0</code>. Of course they just <em>had</em> to change the URI format between versions 1.1 and 2.0. Nothing is ever that easy.</p>

<p>But with this, the last piece fell into place. I created an <code>openid.xml</code> file looking like this:</p>

<pre><code>< ?xml version="1.0" encoding="UTF-8"?>
&lt;xrds:XRDS xmlns:xrds="xri://$xrds" xmlns="xri://$xrd*($v*2.0)"
xmlns:openid="http://openid.net/xmlns/1.0"&gt;
  &lt;XRD&gt;
    &lt;Service priority="50"&gt;
      &lt;Type&gt;http://specs.openid.net/auth/2.0/signon&lt;/Type&gt;
      &lt;URI&gt;http://myopenidprovider&lt;/URI&gt;
      &lt;openid:Delegate&gt;http://myopenid-at-that-provider/&lt;/openid:Delegate&gt;
    &lt;/Service&gt;
    &lt;Service priority="20"&gt;
      &lt;Type&gt;http://openid.net/signon/1.1&lt;/Type&gt;
      &lt;URI&gt;http://myopenidprovider&lt;/URI&gt;
      &lt;openid:Delegate&gt;http://myopenid-at-that-provider/&lt;/openid:Delegate&gt;
    &lt;/Service&gt;
    &lt;Service priority="10"&gt;
      &lt;Type&gt;http://openid.net/signon/1.0&lt;/Type&gt;
      &lt;URI&gt;http://myopenidprovider&lt;/URI&gt;
      &lt;openid:Delegate&gt;http://myopenid-at-that-provider/&lt;/openid:Delegate&gt;
    &lt;/Service&gt;
  &lt;/XRD&gt;
&lt;/xrds:XRDS&gt;
</code></pre>

<p>and using the PHP snippet from <a href="http://blog.paulisageek.com/2009/06/easy-openid-delegation-with-yadis.html">paulisageek</a>,</p>

<pre><code>< ?php
if (strpos($_SERVER['HTTP_ACCEPT'], "application/xrds+xml") !== FALSE) {
  header("Content-Type: application/xrds+xml");
  echo file_get_contents("openid.xml");
}
else {
  header("Location: http://jalf.dk/blog");
}
?>
</code></pre>

<p>I now have:</p>

<ul>
<li>The same nice, short, easy-to-remember OpenID URI I always had</li>
<li>My blog accessible form <code>http://jalf.dk</code></li>
<li>My Facebook account linked to my OpenID</li>
<li>Support for OpenID version 2.0</li>
</ul>

<p>All in all, I’m happy. And now that I’ve documented the process, perhaps the next person who runs into this problem may be a bit happier too.</p>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2009/12/openid-2-0-and-http-redirects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Houston, we have a (performance) problem</title>
		<link>http://jalf.dk/blog/2009/12/houston-we-have-a-performance-problem/</link>
		<comments>http://jalf.dk/blog/2009/12/houston-we-have-a-performance-problem/#comments</comments>
		<pubDate>Tue, 15 Dec 2009 13:49:43 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[stm]]></category>
		<category><![CDATA[thesis]]></category>
		<category><![CDATA[transactional-memory]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=403</guid>
		<description><![CDATA[Ouch. These last few days, I’ve been fixing a few lingering bugs in my STM system, and last night, I finally nailed them. Specifically, it is now possible to open variables within a transaction as read-only. An obvious optimization, right? At least that’s the idea. Less work is required by the STM system if we [...]]]></description>
			<content:encoded><![CDATA[<p>Ouch. These last few days, I’ve been fixing a few lingering bugs in my STM system, and last night, I finally nailed them. Specifically, it is now possible to open variables within a transaction as <em>read-only</em>. An obvious optimization, right? At least that’s the idea. Less work is required by the STM system if we can trust that the variable isn’t modified by this transaction.
<span id="more-403"></span></p>

<p>Well, my test case for this feature now takes <em>ages</em> to run. As I mentioned previously, a simple transaction modifying two integer variables under heavy contention can pull off almost two million transactions per second on my laptop.</p>

<p>My new test, in which each thread takes four variables and alternates between modifying two of them and reading the other two, runs perhaps ten thousand (!) times slower.</p>

<p>Of course I have several leads on how to fix this. The problem is largely all the performance-related “extras” I’ve been leaving out. For example, if a transaction fails to acquire a variable it needs, it simply aborts and immediately retries. In many cases, a  better approach would be to block the thread, waiting for that variable to actually become available.</p>

<p>There are several other cases where I have a similar problem: I have to choose between delaying the thread for a moment with <code>sleep()</code> before attempting to continue, blocking it until some condition is true, or aborting the transaction entirely and starting over from scratch. At the moment, I generally just pick the easiest solutions (typically abort, and <em>occasionally</em> call <code>sleep()</code> a few times before we resort to that. Again, implementing some actual meaningful policies here would make a big difference. And tweaking these policies should help still more.</p>

<p>Another problem is that currently, I do not enforce a consistent global order when acquiring objects during a commit. This means I risk livelocks, again causing excessive rollbacks when multiple threads are competing over access to the same variables.</p>

<p>So I’m still optimistic. It should be possible to get performance back on track. But man, it’s depressing watching performance plummet like this.</p>

<p><strong>Edit</strong><br />
And an update. After poking around a bit, it turned out that most of the time was being spent sleeping. When a transaction attempts to commit, if it can not acquire all the all the variables it needs, it retries a few times with a short delay (a couple of milliseconds) in between. If it doesn’t succeed after a few tries, it rolls back the entire transaction and starts over.</p>

<p>It turned out that these few, short <code>sleep()</code> calls brought CPU utilization down to something like 0.01%, and totally destroyed performance. Simply turning the <code>sleep()</code> call into a <em>no-op</em> brought me back to something more or less reasonable. I still need to improve on the above shortcomings, but now at least I can run my tests in less than an hour.</p>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2009/12/houston-we-have-a-performance-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using My STM Library</title>
		<link>http://jalf.dk/blog/2009/11/using-my-stm-library/</link>
		<comments>http://jalf.dk/blog/2009/11/using-my-stm-library/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 12:58:22 +0000</pubDate>
		<dc:creator>jalf</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[stm]]></category>
		<category><![CDATA[thesis]]></category>
		<category><![CDATA[transactional-memory]]></category>

		<guid isPermaLink="false">http://jalf.dk/blog/?p=362</guid>
		<description><![CDATA[As promised yesterday, I’d like to show off a few bits of my STM library. Of course it’s far from done, and is still missing several key features, but the core library is in pretty good shape. So as they say on the internets, “my STM library, let me show you it”

In the following, I’ll [...]]]></description>
			<content:encoded><![CDATA[<p>As promised yesterday, I’d like to show off a few bits of my STM library. Of course it’s far from done, and is still missing several key features, but the core library is in pretty good shape. So as they say on the internets, <em>“my STM library, let me show you it”</em><span id="more-362"></span></p>

<p>In the following, I’ll show a slightly modified version of one of my test cases. It shows how to call and use my library, from the user’s point of view. The test spawns a number of threads, which each wait on a barrier (because if one thread was allowed to run while another was being constructed, I’d get fewer concurrent transactions, and my test would be less likely to uncover race conditions), and then perform a fixed number of transactions. In each transaction, two transactional variables are opened for writing, one is decremented and the other incremented. If the sum of these variables is nonzero in any iteration, the thread registers a failure.</p>

<p>And if, after all the threads have terminated, the value of each of these variables is not what was expected, a failure is registered as well.</p>

<p>So from a testing point of view, there should be plenty of opportunity for things to go wrong. Just a single small race condition somewhere, and <em>one</em> of the many millions of reads would be inconsistent and the test would fail.</p>

<pre><code>#include &lt;stm.hpp&gt; // my STM library

#include &lt;boost/test/unit_test.hpp&gt; // Boost.Test is used to supply a unit-testing framework
#include &lt;boost/thread.hpp&gt; // Boost.Thread is used as a threading API

// define the number of threads to run, and the number of iterations for each
enum { thread_count = 8, iterations = 200000 }; 

// The following are transactional variables. The shared template ensures that the contained value
// can only be accessed as part of a transaction, and provides the necessary metadata
// for checking validity and consistency
// Two such integers are created, both initialized to zero
stm::shared&lt;int&gt; val1(0);
stm::shared&lt;int&gt; val2(0); 

BOOST_AUTO_TEST_SUITE( threads ) // define a test suite named "threads"

// this function defines the body of our transaction. 
// We're passed a transaction, which can be used to open any "shared" variables
bool tx_func(stm::transaction&amp; tx){
    // open both variables for writing
    int&amp; a = val1.open_rw(tx);
    int&amp; b = val2.open_rw(tx);

    // modify the variables freely
    --a;
    ++b;

    return a + b == 0; // Our transaction returns a bool. Other return types (or void) are also supported
}

// this class defines a thread. operator() is called as the thread's entry point
struct thread_functor{
    // In the constructor, the thread object is given a barrier it can synchronize on,
    // and a reference where it can write the its result (success/failure)
    thread_functor(boost::barrier&amp; bar, int&amp; res) : bar(bar), res(res) {}

    void operator()(){
        // when the thread is first created, we wait for the barrier
        // This ensures that no transactions are running until all threads have been constructed
        bar.wait(); 
        for (int i = 0; i &lt; iterations; ++i){
            // for each iteration, pass our transaction function to the "atomic" function, which executes it atomically.
            // To get a non-void return type, we have to specify the template parameter explicitly 
            // (this can be avoided in C++0x using the return_of template to deduce the return type implicitly)

            // depending on the return value of the transaction, write success or failure back
            res = (res != 0) &amp;&amp; stm::atomic&lt;bool&gt;(tx_func) ? 1 : 0;
        }
    }

    boost::barrier&amp; bar;
    int&amp; res;
};

// another transaction, to be executed after our helper threads terminate, 
// to verify that the right number of modifications have occurred
// note that here variables are opened for reading only
void verify(stm::transaction&amp; tx) {
    const int&amp; a = val1.open_r(tx);
    const int&amp; b = val2.open_r(tx);

    BOOST_CHECK_EQUAL(-a, thread_count * iterations);
    BOOST_CHECK_EQUAL(b, thread_count * iterations);
}

// finally, we get to our test case itself
BOOST_AUTO_TEST_CASE ( short_concurrent_transactions )
{
    boost::barrier bar(thread_count);
    boost::thread_group gr;
    int res[thread_count]; // array of results
    // set all the results to an initial true/1 value (since each iteration "and"'s it together with the current result
    std::fill(res, res+thread_count, 1); 

    for (int i = 0; i &lt; thread_count; ++i){
        gr.create_thread(thread_functor(bar, res[i])); // create the threads, passing the necessary parameters to each
    }

    gr.join_all(); // wait for all threads to terminate

    // verify that each thread return success
    for (int i = 0; i &lt; thread_count; ++i){
        BOOST_CHECK_EQUAL(res[i], 1);
    }

    // run a final transaction to access both variables and check their final values
    stm::atomic(verify);
}

BOOST_AUTO_TEST_SUITE_END()
</code></pre>

<p>In the above, I used a function object to represent threads, and a regular function to represent transactions. Of course in both cases, either would work — a function object would potentially be more efficient as it is easier for the compiler to inline, but I used a function for brevity.</p>

<p>In C++0x, of course, lambdas could also have been used in both cases.</p>

<p>One of my design goals has been to make basic usage as simple and intuitive as possible, and I think I’ve succeeded so far. Any C++ programmer who is familiar with the STL algorithms or the Boost libraries, should find my library’s interface very straightforward. Note especially that all the transaction “magic”, of verifying validity and retrying transactions as needed, is completely invisible to the user. You simply define a function expressing what your transaction should do, and pass it to the <code>atomic</code> function.</p>

<p>In its current version, this test is able to execute around 1,800,000 transactions per second on my Core Duo 2GHz laptop. (Of course, with transactions as small as these, opening only two variables each, performance is a lot better than it would be in real-world transactions.</p>

<p>So that’s it for now. Of course I’ve got a few more user-facing features in the pipeline<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup>, and a <em>lot</em> of backend changes, but the basic functionality is there, and I’m pretty happy with it so far.</p>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>It should probably be possible to specify that the transaction should <em>not</em> automatically retry if it fails to commit, and instead abort with an exception. It should also be possible to define nested transactions, and use operations such as <em>OrElse</em> and <em>Retry</em> primitives introduced in <a href="http://www.haskell.org/haskellwiki/Software_transactional_memory">Haskell STM</a> <a href="#fnref:1" rev="footnote">↩</a></p>
</li>

</ol>
</div>
]]></content:encoded>
			<wfw:commentRss>http://jalf.dk/blog/2009/11/using-my-stm-library/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
