<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Musing Dragoman</title>
	<atom:link href="https://mortoray.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>https://mortoray.wordpress.com</link>
	<description>Programming ideals, wizened script, befuddled systems</description>
	<lastBuildDate>Fri, 17 Feb 2012 05:04:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='mortoray.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>https://secure.gravatar.com/blavatar/0320f006b561b121759b081fe16899eb?s=96&#038;d=https%3A%2F%2Fs-ssl.wordpress.com%2Fi%2Fbuttonw-com.png</url>
		<title>Musing Dragoman</title>
		<link>https://mortoray.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="https://mortoray.wordpress.com/osd.xml" title="Musing Dragoman" />
	<atom:link rel='hub' href='https://mortoray.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Performance is irrelevant</title>
		<link>https://mortoray.wordpress.com/2012/02/09/performance-is-irrelevant/</link>
		<comments>https://mortoray.wordpress.com/2012/02/09/performance-is-irrelevant/#comments</comments>
		<pubDate>Thu, 09 Feb 2012 05:16:43 +0000</pubDate>
		<dc:creator>mortoray</dc:creator>
				<category><![CDATA[Efficiency]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[efficiency]]></category>
		<category><![CDATA[performance considerations]]></category>
		<category><![CDATA[software]]></category>
		<category><![CDATA[speed]]></category>
		<category><![CDATA[theoretical limits]]></category>

		<guid isPermaLink="false">http://mortoray.wordpress.com/?p=144</guid>
		<description><![CDATA[Asking questions about performance online universally invites scorn and accusation. A large number of programmers apparently feel that the efficiency &#8230;<p><a href="https://mortoray.wordpress.com/2012/02/09/performance-is-irrelevant/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mortoray.wordpress.com&amp;blog=11292855&amp;post=144&amp;subd=mortoray&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Asking questions about performance online universally invites scorn and accusation. A large number of programmers apparently feel that the efficiency of code is nowadays insignificant. So long as the functional requirements have been met the approach is golden. Any attempt to discuss improvements is met with stiff resistance. The most common of the mantras are &#8220;premature optimization&#8221; and &#8220;have you measured it&#8221;.</p>
<p>While at times such notions may not be misguided, this general attitude towards ignoring performance considerations is quite dangerous. Let&#8217;s look at a few reasons why performance is still quite relevant, especially early in the development process.</p>
<h3>Performance Issues</h3>
<h4>Some things don&#8217;t parallelize</h4>
<p>There are a great number of functional behaviours which simply can&#8217;t be done in parallel, or rather, gain nothing when done in parallel. At the high-level, most client-server requests tend to have one outermost request which works in serial to assemble the response for the client. Perhaps many of the sub-requests can be done in parallel, but this serial code still has to execute and can often become the bottleneck in the total response time.</p>
<p>At lower levels there are algorithms which can&#8217;t be efficiently handled on multiple cores. Often the overhead of splitting up the work is more than the cost of the algorithm itself. Or sometimes, just like the client requests, the algorithm has a serial nature that just can&#8217;t be avoided.</p>
<h4>Chips aren&#8217;t getting any faster</h4>
<p>In recent years the speed of individual cores has not really been increasing. While we are certainly not at the theoretical limits yet, the physical obstacles to increasing speed are significant. Essentially we&#8217;ve hit a speed limit in the commodity market and chips simply range from 2 to 4 Ghz. Industry has decided providing more cores is better than providing faster cores.</p>
<p>Combine this with the inability to parallelize certain behaviours and you can see a problem.</p>
<h4>Can&#8217;t fix it later</h4>
<p>Designing to be scalable, either to multiple cores, or to multiple computers, is something that has to be planned for fairly early in the process. While you don&#8217;t need to scale immediately, you at least need to chose an architecture and algorithms that can be easily adjusted later. Most people, including programmers, tend to think in a serial fashion, thus most code tends not to lend itself to concurrent processing. If you have failed to consider concurrency, and scaling, early in the process, you may find that path fairly difficult.</p>
<p>Even small decisions made early on can lead to significant performance loss when used systemically through the code. Perhaps a band internal flow, or poor use of global memory. Once a bad behaviour is ingrained at all points in the code it becomes extremely time consuming to change. Naturally programmers just follow the existing code; whatever is there at the start will be magnified throughout.</p>
<h4>Solutions are non-trivial</h4>
<p>This is a good counter to those who blindly argue you should profile your code to see what is more efficient. If any algorithm could be coded in multiple forms within a few hours then perhaps simply trying them out is a good idea. Most algorithms are however part of a larger system and trying to segregate and replace that component can often be difficult. Often the item to be improved can simply not be isolated and is more of systemic feature in the code. Given the amount of time that will be required to make a change, or code the first version, it seems entirely reasonable to try and think about the efficiency ahead of time.</p>
<p>This also entirely neglects that doing proper performance measurements is very hard. But that is a topic all on its own.</p>
<h4>Broken theory of scaling</h4>
<p>A system which can scale in theory is vastly different than one that can actually scale. A common failure is related to networking. A cluster of computers has to be connected by real switches and wires which have a fixed limit on the traffic they can effectively handle. Beyond that you need to start grouping and segregating. Designs often fail to account for this, requiring direct connectivity between all computers and/or forcing all traffic through a single machine.</p>
<h4>Inefficiency costs money</h4>
<p>Even if your system can scale simply by adding more computers, this isn&#8217;t necessary the best solution. Every additional machine has a real cost associated with it. Beyond an initial purchase and installation cost, there is a continual ongoing cost in maintenance and electricity, as well as the final disposal cost. These expenses are significant, and in a highly competitive service market, reductions of even 5 or 10% can make huge difference in the ability of the company to attract clients.</p>
<h2>Conclusion</h2>
<p>While spending too much time fine-tuning the details is often fruitless, backing too far away from performance concerns can be downright dangerous. A lot of major performance issues can be handled up-front with just a tiny bit of planning and forethought. Even if your non-functional requirements are light at the start, don&#8217;t be surprised by changes in demand, and in particular peak load problems. Never forget that hardware has physical limitations to scaling, and that every additional machine is an added cost.</p>
<p>This is not attempting to be an argument in favour of excessive optimization. It is more of a counter to the alarming trend I see to completely ignore performance issues. Don&#8217;t ignore that 5% loss of speed in your module, it&#8217;s going to magnify with the 5% loss in all the other code!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mortoray.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mortoray.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mortoray.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mortoray.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mortoray.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mortoray.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mortoray.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mortoray.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mortoray.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mortoray.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mortoray.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mortoray.wordpress.com/144/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mortoray.wordpress.com/144/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mortoray.wordpress.com/144/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mortoray.wordpress.com&amp;blog=11292855&amp;post=144&amp;subd=mortoray&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://mortoray.wordpress.com/2012/02/09/performance-is-irrelevant/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="https://secure.gravatar.com/avatar/e6ce612702a34f9bcebee2f34139c3f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mortoray</media:title>
		</media:content>
	</item>
		<item>
		<title>Never use the &#8220;continue&#8221; keyword!</title>
		<link>https://mortoray.wordpress.com/2012/01/22/never-use-the-continue-keyword/</link>
		<comments>https://mortoray.wordpress.com/2012/01/22/never-use-the-continue-keyword/#comments</comments>
		<pubDate>Sun, 22 Jan 2012 14:24:34 +0000</pubDate>
		<dc:creator>mortoray</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[coding standard]]></category>
		<category><![CDATA[cpp]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[spaghetti code]]></category>

		<guid isPermaLink="false">http://mortoray.wordpress.com/?p=141</guid>
		<description><![CDATA[I was looking through some questions on StackOverflow last week when I came across a curious answer. In it there &#8230;<p><a href="https://mortoray.wordpress.com/2012/01/22/never-use-the-continue-keyword/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mortoray.wordpress.com&amp;blog=11292855&amp;post=141&amp;subd=mortoray&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I was looking through some questions on StackOverflow last week when I came across a curious answer. In it there was a link to a coding standard that forbade the use of the &#8220;continue&#8221; keyword. There was even positive support for this answer. Needless to say, I was aghast. It&#8217;s like the arguments against &#8220;goto&#8221; have run amok and started targeting other language keywords.</p>
<p>Naturally I can&#8217;t just condemn such principles without showing why it is a problem. Looking through my own code it is certainly a commonly used keyword. While alternatives do exist, &#8220;continue&#8221; is an essential aspect of flow control which helps to reduce code and improve clarity. Instead of trying to prove its worth, I&#8217;ll instead go the other direction and show the consequences of not using &#8220;continue&#8221;.</p>
<h3>Reductio ad Falsam</h3>
<p>Avoiding the minor arguments, let&#8217;s jump right to a logical hypothesis: forbidding &#8220;continue&#8221; also requires forbidding any non-tail &#8220;return&#8221; statement. That is, if you believe using &#8220;continue&#8221; is wrong, logically our argument must also forbid the use of using a &#8220;return&#8221; statement anywhere other than as the last statement of a function. They both prematurely interrupt the flow of execution for a particular block of code.</p>
<p>We can show this using some simple code examples. First let&#8217;s start with a very typical loop using the the &#8220;continue&#8221; keyword.</p>
<p><pre class="brush: cpp;">
for( int i : range )
{
if( some_cond( i ) )
continue;

...
}
</pre></p>
<p>Now let&#8217;s use a function instead of the inline loop body. I have chosen this approach since as it is a common recommendation.</p>
<p><pre class="brush: cpp;">
void process( int i, ... )
{
if( some_cond( i ) )
return;

...
}

for( int i : range )
process( i, ... );
</pre></p>
<p>By doing this you have not modified the meaning of the code at all. An optimizing compiler could very well compile both code bits exactly the same. You have however increased the size of the code, and moved the loop body logic away from the loop &#8212; some people may find this lowers readability. Take this a step further in a language that supports closures (I&#8217;ll attempt to use a C++11 lambda):</p>
<p>Ignoring some syntactic bloat, this looks like the original code sample, albeit without a &#8220;continue&#8221; statement. By using a closure we can meet the requirement yet still produce the same code.</p>
<p><pre class="brush: cpp;">
for( int i : range )
[](int i) {
if( some_cond( i ) )
return;

...
} (i);
</pre></p>
<p>These code samples are to illustrate that using a non-tail &#8220;return&#8221; can be identical in meaning to using a &#8220;continue&#8221; statement. This would imply that whatever reason one would have to forbid the use of &#8220;continue&#8221; would also apply to this use of the &#8220;return&#8221; keyword. My intuition is that if I took this line of reasoning far enough the omission of &#8220;continue&#8221; might actually require a pure functional programming approach. Simply forbidding the use of a non-tail &#8220;return&#8221; is likely enough to show the absurdity of the original requirement.</p>
<h3>Practical Example</h3>
<p>By way of example let me show how not allowing &#8220;continue&#8221; will produce what I consider to be bad code. Looking through my recent project I found a sample that looks somewhat like the below (it scans a list of logical objects looking for those which can be processed).</p>
<p><pre class="brush: cpp;">
for( object_id_t id : proc_list )
{
object * obj = find_object( id );
if( !obj )
continue;

if( obj-&gt;is_active() )
continue;

time_t elapsed = now() - obj-&gt;begin;
if( elapsed &lt; timeout )
continue;

...
}
</pre></p>
<p>If we forbid &#8220;continue&#8221; (and non-tail &#8220;return&#8221;) this code would have to be convereted using embedded &#8220;if&#8221; blocks.</p>
<p><pre class="brush: cpp;">
for( object_id_t id : proc_list )
{
object * obj = find_object( id );
if( obj )
{
if( !obj-&gt;is_active() )
{
time_t elapsed = now() - obj-&gt;begin;
if( elapsed &gt;= timeout )
{
...
}
}
}
}
</pre></p>
<p>High levels of block depth are difficult to read. It is difficult to visually identify where the block ends and what the chain of conditions is. Converting this to a series of functions would result in a lot of nearly trivial functions. A large number of trivial forwarding functions are also hard to read as it is difficult to follow the code. I find the first code example, using &#8220;continue&#8221;, to be a very clear way to write the code.</p>
<h3>Conclusion</h3>
<p>Often requirements get taken out of context, and this might very well be one of those situations. The StackOverflow answer picked one aspect out of a very large document. Out of context, and without rationale I definitely see the requirement (of not using &#8220;continue&#8221;) to be more harmful than beneficial. When I looked through the requirements, most of them actually have a &#8220;Rationale&#8221; entry. This is very good practice; you should always document why you have certain restrictions. Strangely however the requirement on &#8220;continue&#8221; was lacking such a rationale, so we have no idea why it is not allowed &#8212; and in particular why they still allow non-tail &#8220;return&#8221; statements.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mortoray.wordpress.com/141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mortoray.wordpress.com/141/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mortoray.wordpress.com/141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mortoray.wordpress.com/141/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mortoray.wordpress.com/141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mortoray.wordpress.com/141/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mortoray.wordpress.com/141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mortoray.wordpress.com/141/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mortoray.wordpress.com/141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mortoray.wordpress.com/141/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mortoray.wordpress.com/141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mortoray.wordpress.com/141/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mortoray.wordpress.com/141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mortoray.wordpress.com/141/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mortoray.wordpress.com&amp;blog=11292855&amp;post=141&amp;subd=mortoray&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://mortoray.wordpress.com/2012/01/22/never-use-the-continue-keyword/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="https://secure.gravatar.com/avatar/e6ce612702a34f9bcebee2f34139c3f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mortoray</media:title>
		</media:content>
	</item>
		<item>
		<title>What is reference counting?</title>
		<link>https://mortoray.wordpress.com/2012/01/08/what-is-reference-counting/</link>
		<comments>https://mortoray.wordpress.com/2012/01/08/what-is-reference-counting/#comments</comments>
		<pubDate>Sun, 08 Jan 2012 14:52:56 +0000</pubDate>
		<dc:creator>mortoray</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[high level language]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[memory management]]></category>
		<category><![CDATA[reference counting]]></category>
		<category><![CDATA[scope rules]]></category>
		<category><![CDATA[variable scoping]]></category>

		<guid isPermaLink="false">http://mortoray.wordpress.com/?p=136</guid>
		<description><![CDATA[Objects are created, live for a while, and then destroyed. While creation is fairly clear, the when and how of &#8230;<p><a href="https://mortoray.wordpress.com/2012/01/08/what-is-reference-counting/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mortoray.wordpress.com&amp;blog=11292855&amp;post=136&amp;subd=mortoray&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Objects are created, live for a while, and then destroyed. While creation is fairly clear, the when and how of destruction is fairly language dependent. In languages like C you&#8217;re basically on your own, whereas in a very high level language like Python you don&#8217;t even think about destruction. But whether it is manually done, or controlled by the language, there needs to be some way to track what needs to be destroyed. Reference counting is one popular technique.</p>
<p>First we&#8217;ll cover the basics of object lifetime &#8212; you may nonetheless wish to read &#8220;<a title="What’s an object? What’s a variable?" href="/2012/01/08/whats-an-object-whats-a-variable/">What&#8217;s an Object</a>&#8221; as quick referesher on objects. Then we&#8217;ll move on to the basics of refernce counting.</p>
<h3>Scope Lifetime</h3>
<p>The most common lifetime for an object is scope-based: the object exists for the duration of a function or bracketed block of code. Even when using references the name of the reference itself tends to be a scope-based variable.</p>
<p><pre class="brush: cpp;">
	int function( int a )
	{
		int b = a + 1;
		a_struct c;

		if( b &gt; 2 )
		{
			int d = b;
			a_struct e;
		}
	}
</pre></p>
<p>The above C example shows us basic scope based lifetime. The variable &#8220;b&#8221; has function scope. When the function is entered an integer is allocated to be referenced by the name &#8220;b&#8221;. When the function exits this object will automatically be destroyed. The same happens for &#8220;c&#8221;, but here more space will be allocated as it is a structure and not a primitive. The &#8220;if&#8221; statement introduces a new scope, and in C the life of &#8220;d&#8221; and &#8220;e&#8221; will only be until the end of that &#8220;if&#8221; block.</p>
<p>It is important to note that virtually all languages have some kind of variable scoping. Even in languages where memory management is automatic, the reference variables still have a scope of some kind. While you as the programmer may not be aware you are working with references, the language does. The basic scope rules play an important role in memory management of all languages.</p>
<h3>Pointer Lifetime</h3>
<p>For a pointer it is essential to see the difference between the reference (the pointer variable itself) and the refered to object. The actual pointers follow the same scope lifetime rules, in that the pointer object itself disappears once the scope returns, but the refered to object persists.</p>
<p><pre class="brush: cpp;">
	int* function( )
	{
		int * a = new int( 5 );
		return a;
	}

	void caller()
	{
		int * b = function();
		delete b;
	}
</pre></p>
<p>In this basic C++ example the function creates a new integer object. Inside the function there is a pointer called &#8220;a&#8221; which refers to the new object. The return statement of &#8220;function&#8221; creates a copy of the pointer, which wil be assigned to the &#8220;b&#8221; pointer in the caller function. &#8220;b&#8221; in turn uses the pointer to delete the underlying object.</p>
<p>This is completely manual memory management. You are responsible for tracking and deleting the object.</p>
<h3>Reference Counting</h3>
<p>It is often difficult to manually track the life of an object. This is particular true when the object is used widely through the program. It would be helpful to have a somewhat automatic lifetime management. Once everything is done using the object it should be deleted. Reference counting is one such technique.</p>
<p>This method is simply keeping an extra counter along with each object that is created. The counter is the number of references that exist to the object, in the C/C++ case this would how many pointers refer to this object. Anytime a pointer is copied we increment the count, and anytime a pointer goes out of scope, or is reset, we decrement the count. When the count hits zero the object is deleted since nothing more is using it.</p>
<h4>Invasive Counting</h4>
<p>There are two basic approaches to reference counting, either invasive or non-invasive. Let&#8217;s first look at invasive as the examples are slightly clearer. In this approach the objects themselves are aware of the reference counting mechanism. Users of the pointers explicitly increment and decrement the count.</p>
<p><pre class="brush: cpp;">
	MyType * a = get_object();
	//do something with a
	a-&gt;decrement();
</pre></p>
<p>The above is a very typical use of an object with invasive reference counting. You call some function which returns an object. Once you are done with the object you are to release it. Here we call &#8220;decrement&#8221;; if the count reaches zero the object will be deleted. The called function might look ilke this:</p>
<p><pre class="brush: cpp;">
	MyType * current_object;
	
	MyType * get_object()
	{
		current_object-&gt;increment();
		return current_object;
	}
</pre></p>
<p>The function provides access to some global resource, or more likely a member variable. It calls &#8220;increment&#8221; prior to return to indicate there is a new reference to the object now. This makes the object safe to use by the caller since the count must be greater than zero after being returned.</p>
<p>To implement this approach all these objects simply need to derive from some common base class. A much simplified form of this class is shown below &#8212; many essential details are omitted to demonstrate strictly the core reference counting mechanism.</p>
<p><pre class="brush: cpp;">
	class ReferenceCount
	{
		int count;

		ReferenceCount()
		{
			count = 1; //start at 1 as creation implies at least once reference is being made
		}

		void increment()
		{
			count++;
		}

		void decrement()
		{
			count--;
			if( count == 0 )
				delete this;
		}
	};

	//any reference counted object simply derives from the above type
	class MyType : public ReferenceCount { ... }
</pre></p>
<p>Microsoft&#8217;s COM system uses this approach. The base classes is called &#8220;IUnknown&#8221; and has the two functions &#8220;AddRef&#8221; and &#8220;Release&#8221;.</p>
<h4>Non-Invasive Counting</h4>
<p>Non-invasive reference counting does not require the objects to derive from any common base type. It is called non-invasive since there is nothing added to the objects for the reference counting, it is handled external to the object itself. This allows reference counting to be added after the fact. The fundamentals remain essentially the same.</p>
<p><pre class="brush: cpp;">
	Counter&lt;MyType&gt; current_object;
	
	Counter&lt;MyType&gt; get_object()
	{
		current_object.increment();
		return current_object;
	}

	Counter&lt;MyType&gt; a = get_object();
	//use a.getObject() to obtain the object itself
	doSomething( a.getObject() );
	a.decrement();
</pre></p>
<p>This is the same logic as the non-invasive case but simply with a wrapper class. Here MyType isn&#8217;t aware that it is being reference counted. One approach is not necessarily better than the other. Each has its own set of advantages and disadvantages.</p>
<h3>Still Not Automatic</h3>
<p>Neither of the above methods manage to avoid the feeling of being manual. While they certainly help track the use of an object, the programmer is still left calling increment and decrement on their own. How automated this is, or can be, depends highly on the language.</p>
<p>PHP does this completely automatically. All variables are implicitly reference counted. When you assign to a new name in PHP it increments the count, if you call &#8220;unset&#8221; the count is decremented. The counting is completely hidden from the PHP programmer.</p>
<p>C of course does nothing for you: due to the language itself automatic counting is simply not possible. C++ on the other hand provides a library of smart pointers which can do it automatically.</p>
<h4>Example: C++ shared_ptr</h4>
<p>C++ offers a shared_ptr wrapper which is a non-invasive reference counter. It simplifies reference counting by doing all the increments and decrements automatically based on assignments and scoping rules.</p>
<p><pre class="brush: cpp;">
	shared_ptr&lt;int&gt; function()
	{
		shared_ptr&lt;int&gt; a( new int(5) ); //count is now 1
		return a; //count is now 2, one for &quot;a&quot; and one for return temporary (*)
	} //count drops to 1 as &quot;a&quot; goes out of scope

	void caller()
	{
		shared_ptr&lt;int&gt; b( function() ); //count is now 2, one for tempoary and one for b
		//now count is 1 since the temporary is gone
		shared_ptr&lt;int&gt; c = b; //up to 2 since we have another pointer copy
	} //now count is 0 since &quot;b&quot; and &quot;c&quot; go out of scope, thus the object will be deleted
</pre></p>
<p>In the example the current count is shown at each step. The key to note here is that any copy of the &#8220;shared_ptr&#8221; will increment the count on the object. Once that count hits zero the object will be deleted. In the context of C++ the &#8220;shared_ptr&#8221; is not a special language feature, but rather part of the standard library. There is nothing magical about it, it is merely a structure similar to below (where T is the object type, usually a template parameter in C++).</p>
<p><pre class="brush: cpp;">
	struct shared_ptr
	{
		T * obj;
		int * count;
	};
</pre></p>
<p>This reference counted pointer is thus no more than a reference to the underlying object and a reference to the count itself. In addtion it has many operators and functions overloaded to enable the automatic counting mechanics.</p>
<p><em>(*Some of these steps are merely logical, since an optimizer can actually avoid certain copy operations, like a return statement and assignment from temporary.)</em></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mortoray.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mortoray.wordpress.com/136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mortoray.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mortoray.wordpress.com/136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mortoray.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mortoray.wordpress.com/136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mortoray.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mortoray.wordpress.com/136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mortoray.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mortoray.wordpress.com/136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mortoray.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mortoray.wordpress.com/136/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mortoray.wordpress.com/136/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mortoray.wordpress.com/136/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mortoray.wordpress.com&amp;blog=11292855&amp;post=136&amp;subd=mortoray&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://mortoray.wordpress.com/2012/01/08/what-is-reference-counting/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="https://secure.gravatar.com/avatar/e6ce612702a34f9bcebee2f34139c3f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mortoray</media:title>
		</media:content>
	</item>
		<item>
		<title>What&#8217;s an object? What&#8217;s a variable?</title>
		<link>https://mortoray.wordpress.com/2012/01/08/whats-an-object-whats-a-variable/</link>
		<comments>https://mortoray.wordpress.com/2012/01/08/whats-an-object-whats-a-variable/#comments</comments>
		<pubDate>Sun, 08 Jan 2012 14:42:49 +0000</pubDate>
		<dc:creator>mortoray</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[object oriented language]]></category>
		<category><![CDATA[object programming]]></category>

		<guid isPermaLink="false">http://mortoray.wordpress.com/?p=130</guid>
		<description><![CDATA[A variable is is the most fundamental concept in programming. You can&#8217;t do anything without variables. Yet most languages let &#8230;<p><a href="https://mortoray.wordpress.com/2012/01/08/whats-an-object-whats-a-variable/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mortoray.wordpress.com&amp;blog=11292855&amp;post=130&amp;subd=mortoray&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A variable is is the most fundamental concept in programming. You can&#8217;t do anything without variables. Yet most languages let you gloss over what these actually are. Simplicity often hides the truth. For a large degree of programming this is actually okay, and indeed quite helpful. But when it comes to understanding memory, and managing resources, one has to better understand what a variable actually is.</p>
<p>This article assumes you know the basics and tries to expose the lesser known aspects of working with variables. It breaks apart a variable into more specific terms like object, name, and reference. It is by no means a definitive guide, but intends only to expose some underlying concepts often obscured by many languages.</p>
<h3>What is an object?</h3>
<p>Programming is a way to manipulate data and control hardware. Data tends to be exposed in discrete blocks and hardware via distinct channels. At the most abstract level we can consider both memory blocks and hardware resources under the same umbrella term: object. In an object-oriented language this could be an instance of class, whereas in a lower level language it may be a small structure, or just a single value. For system resources this may be a file, or network stream.</p>
<p>All objects share two things in common: creation and destruction. In order to use an object it has to first be created. This may be a simple declaration, an instantion statement like new, or a system function. Once created the objects are used until they are no longer desired. At this point they are destroyed. Here the variable may smiply go out of scope, the instance explicitly deleted, or a system function called to release the object.</p>
<p><pre class="brush: cpp;">
//allocate/free memory in C
char * data = malloc(123);
free(data);

//open/close socket in Flash
Socket sock = new Socket(...);
sock.close();
 </pre></p>
<p><em>The terms creation and destruction are often used in this context, perhaps coming from the higher level OOP languages. In some cases we actually have more of an acquire/release pattern. The pattern of use is nonetheless identical.</em></p>
<h3> Names and References</h3>
<p>All of those objects we&#8217;ve created are not very useful unless we have a way to access them. Every language has a lot of variation in this area, and unfortunately also uses different and overlapping terminology. Syntax aside, there are a few primary ways which we access an object.</p>
<h4>By Name</h4>
<p>When you declare a variable your compiler creates the backing object for you and lets you access it via the name you&#8217;ve given it. This case is so prevalent that often we don&#8217;t even consider there to be a distinction; we look at the variable name as though it were the object itself. This name can only ever refer to one object.</p>
<p><pre class="brush: cpp;">
//create an integer object, refer to it with the name &quot;a&quot;
int a;
//assign 5 to the integer object referred to be &quot;a&quot;
a = 5;
</pre></p>
<p>In the above code you have an integer object created somewhere in memory. In this case you really don&#8217;t care where this object is created. You do however need a way to access this data, thus you have also given it the name &#8220;a&#8221;. It&#8217;s tempting to consider &#8220;a&#8221; to be synonomous with the object itself, but that manner of thinking will cause problems later. You must consider that you have an integer object and the name &#8220;a&#8221; by which you access it as two distinct entities.</p>
<h4>By Reference</h4>
<p>The second most common way to access an object is by using some kind of reference, other than a name, to this object. We must be careful in how we use terms since every language does something slightly differently here. It is easiest to start with C since it has a very straight-forward definition of references, which is simply the address of the object in memory.</p>
<p><pre class="brush: cpp;">
//C memory references are called pointers
int * b = get_integer();
*b = 5;
</pre></p>
<p>The above code seems very similar to our example with &#8220;a&#8221;, but has some important differences. The type of &#8220;b&#8221; is an &#8220;integer pointer&#8221; rather than an integer itself. What we are saying is that &#8220;b&#8221; is actually the address of some integer. The &#8220;get_integer&#8221; function gives us this address &#8212; we don&#8217;t care where it is. The second part is the assignment. Here we are not actually assigning to &#8220;b&#8221;, but rather to the object pointed to by &#8220;b&#8221;.</p>
<p>What becomes confusing at this point is that you still have the name &#8220;b&#8221;, which is a distinct object. &#8220;b&#8221; is a name for the object which is of type &#8220;integer pointer&#8221;. You can, like with &#8220;a&#8221;, assign new values to &#8220;b&#8221; itself, and thus refer to a new object. &#8220;b&#8221; has its own memory location. Inside this memory is the address of another memory location.</p>
<h3>Examples</h3>
<h4>Java References</h4>
<p>Java references are very similar to C pointers. All class type variables are implicitly references and thus no class instances have any direct names. They are addressed by reference only. For example, here we will compare roughly equivalent code in Java and C++.</p>
<p><pre class="brush: cpp;">
//Java
MyClass c = new MyClass();
c.setValue( 123 );&lt;/h4&gt;
//C++
MyClass * c = new MyClass();
c-&gt;setValue( 123 );
</pre></p>
<p>In both classes a new instance of MyClass is being created. &#8220;c&#8221; is not the object itself, but rather a way to refer to the object. The new instance is actually anonymous, it has no name.</p>
<h4>C++ References</h4>
<p>On top of C pointers C++ added a reference construct. The standard tries to distinguish a reference from a pointer, but in the end, a reference becomes nothing more than a normal pointer with a slightly different syntax and restricted copy semantics. Here is a common, and misleading example, about what a reference is.</p>
<p>
int a;
int &amp; b = a;
</p>
<p>The classic text at this point is to say that &#8220;b&#8221; is an alias for &#8220;a&#8221;. Alias sounds lot like it is referring to the &#8220;name&#8221; of the variable. This is wrong, C++ references are not established at the name level, but strictly at the reference/memory address level. Consider if our source object is not a named variable.</p>
<p><pre class="brush: cpp;">
int * a = get_integer();
int &amp; b = *a;
</pre></p>
<p>&#8220;b&#8221; now refers to the object pointed to by &#8220;a&#8221;. There is no way at this point at the code to know what &#8220;name&#8221;, if any , &#8220;b&#8221; is bound to. It is no longer related to &#8220;a&#8221; at all; you can freely assign to &#8220;a&#8221; and what &#8220;b&#8221; refers to will not change.</p>
<h4>File System</h4>
<p>Names also exist at the operating system level and though feel slightly different are quite related. For example a file has a particular name on the drive, and this name can be used to locate that file. In this sense we have a named object just as the integer above, though most languages require a special construct to access the file.</p>
<p>First, a basic low-level C example.</p>
<p><pre class="brush: cpp;">
//open a file for reading
int out = open( &quot;output.txt&quot;, O_WRONLY | O_CREAT );

//write a string to the file
char const * text = &quot;my text&quot;;
write( out, text, strlen(text) );
</pre></p>
<p>We provide a name to the &#8220;open&#8221; command and obtain a reference to the resulting file. Note here that the reference is stored in a plain old &#8220;int&#8221; type. It should be clear that our file is not actually an integer, simply that the file can be referred to via this integer value. That is, the &#8220;name&#8221; of the file is still &#8220;output.txt&#8221;, and the name &#8220;out&#8221; is an integer which stores a reference to the file. Such references are often referred to as &#8220;handles&#8221;.</p>
<p><em>Many OS commands can get confusing as to what actually happens. In the &#8220;open&#8221; example here we&#8217;ve actually created two things: the file and the OS file reference. &#8220;out&#8221; is not a direct reference to the file, but rather a reference to an internal OS structure which in turn knows how to work with the file. So we actually have a reference to a reference. This is clear when you call the &#8220;close&#8221; command, the file isn&#8217;t deleted; just the OS structure is freed, but the file on the disk remains intact.</em></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mortoray.wordpress.com/130/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mortoray.wordpress.com/130/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mortoray.wordpress.com/130/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mortoray.wordpress.com/130/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mortoray.wordpress.com/130/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mortoray.wordpress.com/130/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mortoray.wordpress.com/130/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mortoray.wordpress.com/130/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mortoray.wordpress.com/130/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mortoray.wordpress.com/130/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mortoray.wordpress.com/130/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mortoray.wordpress.com/130/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mortoray.wordpress.com/130/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mortoray.wordpress.com/130/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mortoray.wordpress.com&amp;blog=11292855&amp;post=130&amp;subd=mortoray&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://mortoray.wordpress.com/2012/01/08/whats-an-object-whats-a-variable/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="https://secure.gravatar.com/avatar/e6ce612702a34f9bcebee2f34139c3f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mortoray</media:title>
		</media:content>
	</item>
		<item>
		<title>How does a mutex work? What does it cost?</title>
		<link>https://mortoray.wordpress.com/2011/12/16/how-does-a-mutex-work-what-does-it-cost/</link>
		<comments>https://mortoray.wordpress.com/2011/12/16/how-does-a-mutex-work-what-does-it-cost/#comments</comments>
		<pubDate>Fri, 16 Dec 2011 06:20:56 +0000</pubDate>
		<dc:creator>mortoray</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[concurrent programming]]></category>
		<category><![CDATA[contention]]></category>
		<category><![CDATA[cpu memory]]></category>
		<category><![CDATA[critical data]]></category>
		<category><![CDATA[efficiency]]></category>
		<category><![CDATA[fundamental operations]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[lock]]></category>
		<category><![CDATA[mutex]]></category>

		<guid isPermaLink="false">http://mortoray.wordpress.com/?p=123</guid>
		<description><![CDATA[Concurrent programming requires synchronisation. We can&#8217;t have more than one thread accessing data at the same time otherwise we end &#8230;<p><a href="https://mortoray.wordpress.com/2011/12/16/how-does-a-mutex-work-what-does-it-cost/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mortoray.wordpress.com&amp;blog=11292855&amp;post=123&amp;subd=mortoray&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div id="main-page-body">
<div id="page-body">
<div>
<p>Concurrent programming requires synchronisation. We can&#8217;t have more than one thread accessing data at the same time otherwise we end up with a <em>data race</em>. The most common solution is to wrap the critical data access in a mutex. Mutexes are, of course, not free. How the mutex is used has a significant impact in the cost of the code we are writing. When used correctly we&#8217;ll barely notice the overhead. When used incorrectly it can cause a program to run worse in threaded mode than it would have single threaded!</p>
<p><em>Also view <a title="CPU Memory – Why do I need a mutex?" href="/2010/11/18/cpu-memory-why-do-i-need-a-mutex/">CPU Memory &#8211; Why do I need a mutex?<br />
</a></em></p>
<h3>What is a mutex?</h3>
<p>A mutex, in its most fundamental form, is just an integer in memory. This memory can have a few different values depending on the that state of the mutex. Though usually when we speak of mutexes we also talk of the locks which use the mutex. The integer in memory is not very interesting, but the operations around it are.</p>
<p>There are two fundamental operations which a mutex must provide to be useful:</p>
<ul>
<li>lock</li>
<li>unlock</li>
</ul>
<p><em>unlock</em> is a simple case since it&#8217;s usually just one function. Unlocking a mutex makes it available for another process to lock. <em>lock</em> on the other hand usually has several variants. In most cases we&#8217;d like to wait until we can lock the mutex, so the most common lock operation does exactly this. Other users may wish to only wait for a given period of time, and yet some other users may not want to wait at all. Thus <em>lock</em> has a few variants, all of which have the goal to lock the mutex.</p>
<p>There can be only one lock on a mutex at any given time. If another thread wishes to lock the same mutex it must wait for the first to unlock it. This is the primary goal of the mutex. Attempting to lock an already locked mutex is called <em>contention</em>. In a well planned program contention should be quite low; you should be designing your code so that most attempts to lock the mutex will not block.</p>
<p>There are two reasons why you want to avoid contention. The first is simply that any thread waiting on a mutex is obviously not doing anything else &#8212; possibly resulting in unused CPU cycles. The second reason is more interesting for high performance code. Locking a currently unlocked mutex is extremely cheap compared to the contention case. We have to look at how the mutex works to understand why.</p>
<h3>How does it work?</h3>
<p>As mentioned before, the data of a mutex is simply an integer in memory. It&#8217;s value starts as 0, meaning that it is unlocked. If you wish to lock the mutex you can simply check if it is zero and then assign one. The mutex is now locked and you are the owner of it.</p>
<p>The trick is that the test and set operation has to be atomic. If two threads happen to read 0 at the exact same time, then both would write 1 and think they own the mutex. Without CPU support there is no way to implement a mutex in user space: this operation must be atomic with respect to the other threads. Fortunately CPUs has a function called &#8220;compare-and-set&#8221; or &#8220;test-and-set&#8221; which does exactly this. This function takes the address of the integer, and two integer values: a compare and set value. If the compare value matches the current value of the integer then it is replaced with the new value. In C style code this might like look this:</p>
<p><pre class="brush: cpp;">
int compare_set( int * to_compare, int compare, int set );

int mutex_value;
int result = compare_set( &amp;mutex_value, 0, 1 );
if( !result ) { /* we got the lock */ }
</pre></p>
<p>The caller determines what happens by the return value. It is the value at the pointer provided prior to the swap. If this value is equal to the test value the caller knows the set was successful. If the value is different then the caller knows the value has not changed. When the piece of code is done with the mutex it can simply set the value back to 0. This makes up the very basic part of our mutex.</p>
<blockquote><p>Atomic increment/decrement functions could also be used and is the recommended way if using the Linux futex.</p></blockquote>
<h3>What about waiting?</h3>
<p>Now comes the tricky part. Well, only in a way is it tricky, in another way it is simple. The above test-and-set mechanism provides no support for a thread to wait on the value (aside from a CPU intensive spin-lock). The CPU doesn&#8217;t really understand high-level threads and processes, so it isn&#8217;t in a position to implement waiting. The OS must provide the waiting functionality.</p>
<p>In order for the CPU to wait correctly a caller is going to need to go through a system call. It is the only thing that can synchronise the various threads and provide the waiting functionality. So if we have to wait on a mutex, or release a waiting mutex, we have no choice but to call the OS. Most OSs have built in mutex primitives. In some cases they provide full fledged mutexes. So if a system call does provide a full mutex why would we bother with any sort of test-and-set in user space? The answer is that system calls have quite a bit of overhead and should be avoided when possible.</p>
<p>Various operating systems diverge greatly at this point, and will likely change as time goes on. Under linux there is a system call <em>futex</em> which provides mutex like semantics. It is specifically designed so that non-contention cases can be completely resolved in user space. Contention cases are then delegated to the operating system to handle in a safe, albeit far costlier manner. The waiting is then handled as part of the OS process scheduler.</p>
<blockquote><p><em>futex</em> is actually quite flexible and various locking mechanisms could be build in addition to a mutex, such as a semaphore, a barrier, a read-write mutex, or any kind of signalling.</p></blockquote>
<h3>The Costs</h3>
<p>There are a few points of interest when it comes to the cost of a mutex. The first, and very vital point, is waiting time. Your threads should spend only a fraction of their time waiting on mutexes. If they are waiting too often then you are losing concurrency. In a worst case scenario many threads always trying to lock the same mutex may result in performance worse than a single thread serving all requests. This really isn&#8217;t a cost of the mutex itself, but a serious concern with concurrent programming.</p>
<p>The overhead costs of a mutex relate to the test-and-set operation and the system call that implements a mutex. The test-and-set is likely very low cost; being essential to concurrent processing the CPUs have good reason to make it efficient. We&#8217;ve kind of omitted another important instruction however: the fence. This is used in all high-level mutexes and may have a higher cost than the test-and-set operation. More costlier than even that however is the system call. Not only do you suffer the context switch overhead of the system call, the kernel now spends some time in its scheduling code.</p>
</div>
</div>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mortoray.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mortoray.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mortoray.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mortoray.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mortoray.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mortoray.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mortoray.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mortoray.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mortoray.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mortoray.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mortoray.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mortoray.wordpress.com/123/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mortoray.wordpress.com/123/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mortoray.wordpress.com/123/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mortoray.wordpress.com&amp;blog=11292855&amp;post=123&amp;subd=mortoray&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://mortoray.wordpress.com/2011/12/16/how-does-a-mutex-work-what-does-it-cost/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="https://secure.gravatar.com/avatar/e6ce612702a34f9bcebee2f34139c3f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mortoray</media:title>
		</media:content>
	</item>
		<item>
		<title>Dangerous and confusing casting</title>
		<link>https://mortoray.wordpress.com/2011/11/20/dangerous-and-confusing-casting/</link>
		<comments>https://mortoray.wordpress.com/2011/11/20/dangerous-and-confusing-casting/#comments</comments>
		<pubDate>Sun, 20 Nov 2011 08:51:07 +0000</pubDate>
		<dc:creator>mortoray</dc:creator>
				<category><![CDATA[Defective C++]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[casting]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[pointers]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[typing]]></category>

		<guid isPermaLink="false">http://mortoray.wordpress.com/?p=118</guid>
		<description><![CDATA[Casting in C++ is a confusing jumble of unclear and dangerous operations. It mixes unrelated concepts. It introduces ambiguities and &#8230;<p><a href="https://mortoray.wordpress.com/2011/11/20/dangerous-and-confusing-casting/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mortoray.wordpress.com&amp;blog=11292855&amp;post=118&amp;subd=mortoray&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Casting in C++ is a confusing jumble of unclear and dangerous operations. It mixes unrelated concepts. It introduces ambiguities and redundancies. It&#8217;s an essential but flawed aspect of the language.</p>
<p><span id="more-118"></span></p>
<p style="text-align:right;"><em>(This article is part of the series on <a title="Defective C++" href="/defective-cpp/">Defective C++</a>)</em></p>
<h3>A Broken Any Cast</h3>
<p>C++ inherited the casting syntax from C. For compatibility reasons, one presumes, the C-style cast is equivalent to either a const_cast, static_cast, reinterpret_cast or combination thereof. Having const_cast and reinterpret_cast in the list make the language dangerous. Like C, the cast notation can convert from any arbitrary pointer type to any other arbitrary pointer type whether they are in any way compatible or not.</p>
<p>This isn&#8217;t an argument against the various forms of casting. Each have their own specific purpose and are valuable in their own way. static_cast and dynamic_cast are by far the most commonly used and fulfil most intended meanings of cast. const_cast is frequently used, and though needed, does introduce a potential for invalid code. reinterpret_cast is by far the least used, and the most likely to do the wrong thing. So the failure is that the simplest cast notation can actually invoke the least used and most dangerous casting operation.</p>
<p>Part of the problem is the mixing of casting and conversion. If you wish to convert one type to another type, whether a fundamental or object, you can use a static_cast. This is very bad, since logically a conversion is not the same as a cast: a cast results in a different view on the original object, and a conversion results in a new temporary object. Equating these two meanings just muddles the meaning of both of them. Given that the functional notation also exists for type conversion it is not clear why static_cast needs to do the same thing.</p>
<h3>Solution</h3>
<p>A saner option would be to do a static_cast or dynamic_cast. Perhaps the sanest option would be to do a dynamic_cast and throw an exception if it could not be converted. Usually when you cast it is because you expect to have the desired type and less frequently because you are checking the type. Thus an exception makes sense here. A distinct operation for type checking is a good option instead of an attempted cast.</p>
<p>For situations where performance is important, static_cast would still be available; dynamic cast is a relatively slow operation. The same rules would apply: you can only static_cast between types part of the same class hierarchy.</p>
<p>Conversion would be done strictly via a conversion syntax and would be unrelated from casting. How conversion is done also has issues, but we can get to those another time.</p>
<p>reinterpret_cast is a difficult beast to approach. It has several very distinct purposes. Ideally a language would have another way to approach each of its uses without having to introduce this very dangerous operator. Exactly how is cause for another article.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mortoray.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mortoray.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mortoray.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mortoray.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mortoray.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mortoray.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mortoray.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mortoray.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mortoray.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mortoray.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mortoray.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mortoray.wordpress.com/118/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mortoray.wordpress.com/118/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mortoray.wordpress.com/118/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mortoray.wordpress.com&amp;blog=11292855&amp;post=118&amp;subd=mortoray&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://mortoray.wordpress.com/2011/11/20/dangerous-and-confusing-casting/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="https://secure.gravatar.com/avatar/e6ce612702a34f9bcebee2f34139c3f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mortoray</media:title>
		</media:content>
	</item>
		<item>
		<title>Requirements for a Concurrent (Threaded) Library / API</title>
		<link>https://mortoray.wordpress.com/2011/11/13/requirements-for-a-concurrent-threaded-library-api/</link>
		<comments>https://mortoray.wordpress.com/2011/11/13/requirements-for-a-concurrent-threaded-library-api/#comments</comments>
		<pubDate>Sun, 13 Nov 2011 15:08:04 +0000</pubDate>
		<dc:creator>mortoray</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[thread]]></category>

		<guid isPermaLink="false">http://mortoray.wordpress.com/?p=114</guid>
		<description><![CDATA[Multithreaded programming in a perfect environment can be frustrating. It becomes infuriating when dealing with a plethora of libraries each &#8230;<p><a href="https://mortoray.wordpress.com/2011/11/13/requirements-for-a-concurrent-threaded-library-api/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mortoray.wordpress.com&amp;blog=11292855&amp;post=114&amp;subd=mortoray&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Multithreaded programming in a perfect environment can be frustrating. It becomes infuriating when dealing with a plethora of libraries each with their own notion of thread-safety. As concurrency has evolved so have the libraries, some more than others. Through a lot of trial, and lots of theory, we&#8217;ve arrived at a set of basic expectations of concurrent APIs.</p>
<p>Here we present those basic expectations. These rules should be followed by all libraries and deviations properly documented. They represent a sane basis for doing concurrent programming. Any library that does not uphold these requirements, or fails to note deviations, is simply not concurrency friendly or simply not thread-safe.</p>
<h3>Data Model</h3>
<p>In this discussion we are going to use the term object to refer to any data created by the API. This may actually be a high level language object (a C-style struct, or a Java class) or merely an opaque handle which refers to data. There are other combined representations as well. Regardless of how it is referenced we assume the library exposes discrete data objects.</p>
<p>In this light the library must describe the relationship between its objects. There will be both independent and dependent objects. The requirements listed here focus primarily on dealing with independent objects; logically distinct data structures are the primary focus of concurrency. Note additionally that in some contexts dependents objects may be treated as independent, this facilitates things such as containers and object pools.</p>
<blockquote><p>Example: A has table is a data object which contains several other data objects. At the level of the hash table itself, such as inserting and removing elements, all objects in the hash table are dependent. However, when working directly on the contained objects, they can be treated as independent of each other.</p></blockquote>
<p><em>(These rules apply to both threading and multiple processes equally. They will however be described in terms of threads as that is how most people consider such concurrency.)</em></p>
<h3>Thread Freedom</h3>
<p>The library can be used from multiple threads. This is the absolute basic requirement of a threading friendly library. Such a library could yet hardly be called concurrent. The API must allow data operations on objects in different threads at the same time. This is what would be considered the actual minimum to expect from a thread friendly library. It may now be called concurrent as you can actually do data processing in parallel.</p>
<p>An API with only those two requirements would however be troublesome to use. On top of these we need that individual objects are usable from multiple threads. That is, if you create an object in one thread you must be able to use it in another thread as well. Without this ability you, as the user of the library, will be overly stressed trying to use the various objects.</p>
<blockquote><p>Example: You are using a financial library and wish to do some bond calculations. You create a bond object in thread A and store it in a global table. Later you are in thread B and wish to use that bond object. This should be allowed.</p></blockquote>
<p>As an aside, certain libraries offer ways to move objects between threads. While this is better than nothing, it is still problematic, especially if you can only push to another thread rather than pulling to your current thread. Some operating systems however have this basic limtiation when it comes to certain resources. Our definition extends to these OS modules as well: they aren&#8217;t concurrent, perhaps not even thread friendly.</p>
<h3>Dependent Object Read-Write Safety</h3>
<p>The standard rules for data access to dependent objects are multiple read safety and exclusive write safety. This equally applies to parallel access to an individaul object (perhaps the most common case of access).</p>
<h4>Multiple Read</h4>
<p>The functions which do not modify an object (read-only functions) are expected to be completely thread-safe. You can call any number of different read-only functions on dependent objects from any number of threads at the same time.</p>
<blockquote><p>Example: You have construced a string. Any number of threads should be able to read this string at the same time.</p></blockquote>
<h4>Exclusive Write</h4>
<p>Functions which can modify an object require exclusive access to that object&#8217;s dependent group. The caller is expected to perform sufficient locking to ensure that none of its other threads are either reading or writing to the same object at the same time.</p>
<blockquote><p> Example: An image is actually a collection of related data. You have the bits of the image itself, a color table, author information, print attributes, etc. All this sub-data is child data of the image itself. Therefore it is reasonable that exclusive access (for write) is required on the whole set of data (the image object) when modifying any of this related data.</p></blockquote>
<h3>Independent Object Read-Write Safety</h3>
<p>Independent objects must be usable concurrently from multiple threads without restriction or concern for data races. This applies to both objects of distinct types and those of the same type.</p>
<p>Unrelated objects can be modified at the same time. For this reason it is vital the API describe it&#8217;s ownership rules and indicate which objects are related to each other. Some APIs may define a parent-child relationship and require that modifications to any children require an exclusive lock on the parent (thereby excluding all reading and writing from the parent and all of its children).</p>
<blockquote><p>Example: Given a graphics processing library you create image A. The basic requirements state that any thread may read any properties of image A at the same time. If you wish to modify A however you are responsible for providing exclusive access. Now, if you create a second image B, it is assumed independent from image A. In this light you should be able to modify A in one thread and B in another thread at the same time.</p></blockquote>
<blockquote><p>Example: Containers usually have a special relationship with their children. The container itself is a single object, thus it requires exclusive access for modifications, including inserting and removing data. However, it is normally expected that so long as the container is not being modified, each member in the container is independent of the others.</p></blockquote>
<h3>Limited Thread Data and Cleanup</h3>
<p>Though thread freedom allows objects to be used from multiple threads there is no prohibition on thread local data. Thread local structures can often be used to improve performance by reducing contention on shared data. A library is free to do so, on one condition however: when the thread exits the thread local data must be cleaned up. Simply put, as threads come and go in your program you should expect that the library will not be leaking resources. The space complexity of the library should be related to how many threads are using it now, and how many objects you have.</p>
<p>Related here is how much data, or how many resources, the thread local structures retain. A user of a library should be safe in expecting that only a sensible amount of memory is being used and that no scarce resources are cached per thread. A library which does not uphold this ideal will essentially violate the thread freedom requirement.</p>
<blockquote><p>Example: You have an audio library for playing and mixing sounds. Some sound cards have on-card memory which can be used for sample playback. This memory is usually quite limited. If each thread in which the libary was used allocated some of this memory for its private use, you&#8217;d find it quickly exhausted &#8212; and indeed probably sitting idle in threads which aren&#8217;t currently using the sound library.</p></blockquote>
<h3>Per-Thread Feedback</h3>
<p>Different languages have different ways of reporting error values, in some cases these cannot be returned directly as a result of calling a function. For example, a lot of POSIX functions return a handle-like object, where a 0, or -1, indicates an error has occurred. To actually determine what error has occurred another function, or a global variable (like errno) needs to be consulted.</p>
<p>Such mechanisms must be thread-safe. One thread must be able to call the primary function and then consult the return value without having another thread overwrite that value. In practical terms that means this return state must be thread local.</p>
<blockquote><p>Example: You call the fopen function to open a file. If file cannot be opened a null value is returned and errno set to indicate what happened. Now, between this call and reading errno another thread also calls a function which sets errno. It is expected that this second call does not disrupt the value of errno in the first thread. That is, each thread has its own unique copy of errno.</p></blockquote>
<h3>External State Defines Requirements</h3>
<p>All data objects have an externally visible state tied to some internal state. The user of a library is oblivious to this internal state: they work only with the external view. This gives libraries a lot of implementation flexibility. Since the user does not see this internal model, the concurrency requirements must be described and met according to the externally visible state.</p>
<blockquote><p>Example: If you have string A and then create a copy of it to produce string B. You should now be able to assume that these are two independent objects with respect to the API. Thus you should be able to modify both objects at the same time from different threads. While this may seem obvious, it has very significant implications for libraries which wish to use copy-on-write semantics.</p></blockquote>
<h4>Visibility</h4>
<p>So far we&#8217;ve not talked much about low-level data visibility. It is probably too deep for this article, but we must mention it briefly. The standard language rules on visibility apply to all the requirements here. The programmer is expected to create a proper series of happens-before operations and ensure visibility of his objects between threads. This is mentioned in this section since the user will only be ensuring visibility on the external view of the objects. It is vital for the libraries internal state to at least match, or exceed, the user expected visibility.</p>
<h3>Low Contention</h3>
<p>The rules about thread freedom and data safety are really only useful if the processing can actually be done concurrently. That is, writing to two independent objects can actually make progress on two distinct processors at the same time. It doesn&#8217;t help anybody if the first write simply locks the entire library until complete, at which point the second write proceeds.</p>
<p>This is perhaps not a fundamental requirement, but a reasonable expectation of a quality library. The other requirements should not create a lot of resource contention between threads. That is, each call to the library should be able to make progress without waiting on other calls to the library.</p>
<p>Certain APIs will require shared data however, and thus some contention will exist. So what exactly low contention means is a quality issue. On the ideal side we have libraries with zero contention, these tend to be the stateless libraries which actually don&#8217;t have shared data. On the really bad side we have libraries which lock on every call and are essentially single-threaded.</p>
<h3>Extensions and Exceptions</h3>
<p>These requirements are provided as a basic set that a sane library should be providing. If you observe that many libraries don&#8217;t meet these requirements you&#8217;d be correct. You should also note that many libraries are very difficult to use in a threaded program, some libraries are close to impossible to use correctly, and others end up needing a dedicated and exclusive thread.</p>
<p>A basic set of requirements is still useful when dealing with such libraries. It gives us the basic expectations from which we can note deviations. Rather than just saying the library is not thread-safe we can now indicate specific guarantees which are not met by the library, or which specific limitations are in place.</p>
<p>Other libraries will extend the requirements. The most common extension is creating an active self-locking object. Such objects can be modified by any number of threads at the same time as the library itself will do all the necessary locking. In some contexts this is actually the preferred behaviour, whereas in others it is a significant performance impediment. Again here however, describing a deviation from the expected requirements helps users of the library understand.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mortoray.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mortoray.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mortoray.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mortoray.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mortoray.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mortoray.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mortoray.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mortoray.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mortoray.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mortoray.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mortoray.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mortoray.wordpress.com/114/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mortoray.wordpress.com/114/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mortoray.wordpress.com/114/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mortoray.wordpress.com&amp;blog=11292855&amp;post=114&amp;subd=mortoray&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://mortoray.wordpress.com/2011/11/13/requirements-for-a-concurrent-threaded-library-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="https://secure.gravatar.com/avatar/e6ce612702a34f9bcebee2f34139c3f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mortoray</media:title>
		</media:content>
	</item>
		<item>
		<title>Mismatched Allocation and Delete Nonsense</title>
		<link>https://mortoray.wordpress.com/2011/10/30/mismatched_allocation_and_delete_nonsense/</link>
		<comments>https://mortoray.wordpress.com/2011/10/30/mismatched_allocation_and_delete_nonsense/#comments</comments>
		<pubDate>Sun, 30 Oct 2011 08:17:15 +0000</pubDate>
		<dc:creator>mortoray</dc:creator>
				<category><![CDATA[Defective C++]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://mortoray.wordpress.com/?p=105</guid>
		<description><![CDATA[Efficient yet confused. Powerful but unsafe. So is the nature of C++ object allocation and instantiation. (This article is part &#8230;<p><a href="https://mortoray.wordpress.com/2011/10/30/mismatched_allocation_and_delete_nonsense/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mortoray.wordpress.com&amp;blog=11292855&amp;post=105&amp;subd=mortoray&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Efficient yet confused. Powerful but unsafe. So is the nature of C++ object allocation and instantiation.<br />
<span id="more-105"></span></p>
<p style="text-align:right;"><em>(This article is part of the series on <a title="Defective C++" href="/defective-cpp/">Defective C++</a>)</em></p>
<h3>Arrays</h3>
<p>The first warning of a memory problem in C++ is the need to match array allocation with special deallocation syntax.</p>
<p><pre class="brush: cpp;">
int * a = new int[10];
delete a; //wrong
delete[] a; //right
</pre></p>
<p>Any call to `new[]` must be matched with a call to `delete[]`. Since the array in C++ is kind of a dimunitive type, becoming a pointer at any instant, it is not possible for the compiler to enforce this requirement, in which case it&#8217;d simply be an inconvenience. Instead you&#8217;ll just end up getting undefined run-time behaviour, hopefully your program will crash, but likely you&#8217;ll just get unusual memory corruption.</p>
<p>Beyond just a syntax issue, this problem is exemplified by templates: you simply can&#8217;t easily write a wrapper that works with both plain pointers and arrays. Look at the <em>shared_ptr</em> wrapper class and you&#8217;ll notice it can&#8217;t be readily used with an array. This is unfortunate since the pattern of a shared pointer doesn&#8217;t change whether the underlying pointer is a single object or an array.</p>
<h3>Placement New</h3>
<p>An important feature of memory management is the ability to instantiate objects at an already allocated location in memory. This is done with the placement new syntax. Deleting this object however requires an entirely different syntax.</p>
<p><pre class="brush: cpp;">
T * a = new (block)T;
a-&gt;~T();
</pre></p>
<p>Use a custom allocator instead and you&#8217;ll also be forced to write  an explicit call to <em>operator delete</em>. While the basic <em>new</em> and <em>delete</em> offer a sane symmetry, the placement syntax is extremely asymmetric and confuses exactly what <em>new</em> and <em>delete</em> are supposed to be doing.</p>
<h3>Virtual Destructor</h3>
<p>Where the array syntax may be considered annoying, and the placement syntax merely confused, it is hard to deny the ability to delete <strong>part of an object</strong> is critically flawed. Whenever you delete an object through a pointer to one of its bases, you may either properly delete the whole object, or just delete part of it depending on how it was declared.</p>
<p><pre class="brush: cpp;">
struct A { ~A() { } };
struct B : public A { ~B() { } };

B * b = new B;
A * a = b;
delete a; //half-delete
</pre></p>
<p>Some might still be shocked to learn the above does not call <em>B::~B</em> but only calls <em>A::~A</em>. Unless the base-class destructor is marked <em>virtual</em> the <em>delete</em> operator will only delete the immediately known type. Here that is an <em>A</em> since it is deleting an <em>A*</em>.</p>
<h3>Solutions</h3>
<p>The history of these problems stems from C compatibility. In particular, a C-struct uses no more memory than its actual member contents (plus padding for ailgnment). In C, memory allocation and object instantiation are two distinct operations which have to be manually performed. C++  merges these two operations, but does so with some critical flaws..</p>
<p>There is no arguing that at times you&#8217;ll need fine control of memory allocation and instantiation. Such options should be provided, but the default should be a sane system where <em>delete</em> just does the right thing. That is, <em>delete A</em> will properly delete an array, single object, or derived object. If the object was allocated via a special allocator it should also properly call the deallocator, or otherwise properly deallocate all via that single <em>delete A</em> syntax.</p>
<p>Doing so may require meta-information to be stored along with any allocated object. Regardless of how an object is created the resulting object must contain enough information to say how it should be deleted. This concept isn&#8217;t entirely new, just look at any <em>smart_ptr</em> and you&#8217;ll see a <em>deleter</em> function which does exactly this.</p>
<p>Curiously, if you also believe a language should be garbage collected you also implicitly support this feature. The collector will have to know exactly how to destroy any object. Short of a full scanning collector, safe deletion also simplifies the task of any object pool or any variant type for that matter.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mortoray.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mortoray.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mortoray.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mortoray.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mortoray.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mortoray.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mortoray.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mortoray.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mortoray.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mortoray.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mortoray.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mortoray.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mortoray.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mortoray.wordpress.com/105/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mortoray.wordpress.com&amp;blog=11292855&amp;post=105&amp;subd=mortoray&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://mortoray.wordpress.com/2011/10/30/mismatched_allocation_and_delete_nonsense/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="https://secure.gravatar.com/avatar/e6ce612702a34f9bcebee2f34139c3f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mortoray</media:title>
		</media:content>
	</item>
		<item>
		<title>Implicit Type Promotion and Conversion</title>
		<link>https://mortoray.wordpress.com/2011/10/27/implicit-type-promotion-and-conversion/</link>
		<comments>https://mortoray.wordpress.com/2011/10/27/implicit-type-promotion-and-conversion/#comments</comments>
		<pubDate>Thu, 27 Oct 2011 09:50:00 +0000</pubDate>
		<dc:creator>mortoray</dc:creator>
				<category><![CDATA[Defective C++]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[semantics]]></category>
		<category><![CDATA[static typing]]></category>
		<category><![CDATA[type theory]]></category>

		<guid isPermaLink="false">http://mortoray.wordpress.com/?p=83</guid>
		<description><![CDATA[C introduced it and C++ mastered it. The hellish world of implicit conversion and type promotion. A system which silently &#8230;<p><a href="https://mortoray.wordpress.com/2011/10/27/implicit-type-promotion-and-conversion/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mortoray.wordpress.com&amp;blog=11292855&amp;post=83&amp;subd=mortoray&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>C introduced it and C++ mastered it. The hellish world of implicit conversion and type promotion. A system which silently modifies, truncates, rounds, and otherwise mangles our variables.<span id="more-83"></span></p>
<p style="text-align:right;"><em>(This article is part of the series on <a title="Defective C++" href="http://mortoray.wordpress.com/defective-cpp/">Defective C++</a>)</em></p>
<p>C++ has inherited from C a series of implicit type conversions. On top of this it has added a class-based conversion mechanism. Perhaps this is quite convenient if you don&#8217;t care too much about types. Perhaps the concept is even sound, but something is lacking. As implemented they are very counter to the notion of a type-safe language.</p>
<p>For example, integer promotion has the vexing property that adding to short integers results in an normal integer. As does any numeric operation on a char. When trying to work with small values, or use bit-masks, this results in distracting casting to avoid compiler warnings. Warnings which you should not turn off if you care anything at all about static typing.</p>
<p><pre class="brush: cpp;">
uint8_t flags = 0;
uint8_t const flag_a = 0x12;

//this is the best syntax:
flags |= flag_a;
//but instead you have to do this to avoid warnings
flags = uint8_t( flags | flag_a );
</pre></p>
<p>The craziest conversion involves the bool type. Any pointer implicitly converts to bool, as does a bool convert to any integer type. Explicit conversion to bool inside a conditional is a great convenience, but there is no reason to convert to/from bool by simply calling a function or assigning.</p>
<p><pre class="brush: cpp;">
void function( int value, bool flag ) { }

int main()
{
  int a = 123;
  bool flag = true;

  //oops, a common mistake
  function( flag, a );
}
</pre></p>
<p>That the above compiles without warning (even with -Wall -Wconversion in gcc) is distressing!</p>
<p>A case of promotion gone wrong appears when you start using the extended integer types. Look at the following code and decide which function is called.</p>
<p><pre class="brush: cpp;">
#include &lt;stdint.h&gt;

void func( int64_t a ) { }
void func( double a ) { }

int main()
{
int a = 10;
func( a );
}
</pre></p>
<p>Neither are called. This introduces an ambiguity since the int can be promoted to both an int64_t and a double. While promotion to double is a nice convenience, it should be clear that promotion to an integer is to be preferred.</p>
<h3>Solutions</h3>
<p>Type safety can only really be guaranteed if values cannot be lost by default. Therefore any conversion which truncates a value should never be done implicitly. That is, no casting to shorter or less precise types.</p>
<p>Class constructors should be marked as explicit by default, since the vast majority of single parameter constructors are not intended to be used as converters. I might even be willing to accept an argument that class conversion is never done implicitly and must always be explicitly noted. I might also accept an argument that says no implicit class conversion should be allowed at all.</p>
<p>Bool should be a distinct type, non-integral. In a way it should be treated a condition result, thus allowing all the familiar conversions and avoiding the broken ones.</p>
<p>Explicit casts to whatever you want should of course always be allowed. However it would be good to have clear functions which do this and indicate what will happen. Do you wish to truncate, round, assign to max on overflow, wrap-around, etc?</p>
<p>While all the implicit type conversion may seem to make programming easier, ultimately it just hides defects. These defects are often hard to find as well since they are not of a type that generate warnings or can be identified by run-time analysis tools. The rules for a statically typed language should be strict.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mortoray.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mortoray.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mortoray.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mortoray.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mortoray.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mortoray.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mortoray.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mortoray.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mortoray.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mortoray.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mortoray.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mortoray.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mortoray.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mortoray.wordpress.com/83/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mortoray.wordpress.com&amp;blog=11292855&amp;post=83&amp;subd=mortoray&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://mortoray.wordpress.com/2011/10/27/implicit-type-promotion-and-conversion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="https://secure.gravatar.com/avatar/e6ce612702a34f9bcebee2f34139c3f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mortoray</media:title>
		</media:content>
	</item>
		<item>
		<title>The Ideal Language has &#8220;goto&#8221;</title>
		<link>https://mortoray.wordpress.com/2011/10/23/the-ideal-language-has-goto/</link>
		<comments>https://mortoray.wordpress.com/2011/10/23/the-ideal-language-has-goto/#comments</comments>
		<pubDate>Sun, 23 Oct 2011 04:57:25 +0000</pubDate>
		<dc:creator>mortoray</dc:creator>
				<category><![CDATA[Ideal Language]]></category>
		<category><![CDATA[execution path]]></category>
		<category><![CDATA[language]]></category>
		<category><![CDATA[longjmp]]></category>
		<category><![CDATA[obfuscated code]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[spaghetti code]]></category>

		<guid isPermaLink="false">http://mortoray.wordpress.com/?p=74</guid>
		<description><![CDATA[&#8220;goto&#8221;: the demonized programming contsruct. This little expression allows you to jump to somewhere else in the code while skipping &#8230;<p><a href="https://mortoray.wordpress.com/2011/10/23/the-ideal-language-has-goto/">Continue reading &#187;</a></p><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mortoray.wordpress.com&amp;blog=11292855&amp;post=74&amp;subd=mortoray&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>&#8220;goto&#8221;: the demonized programming contsruct. This little expression allows you to jump to somewhere else in the code while skipping the expressions in between. Opponents say it leads to spaghetti code and has no business in modern programming. Most new languages buy into this argument and don&#8217;t include a goto expression, yet fail to include a solution for the places where goto is still useful.<span id="more-74"></span><br />
There are several situations where goto produces the cleanest possible code. Trying to cram the same login into a series of if statements, or loops, in fact can lead to more obfuscated code. Within state machines, including parsers, it often has a role. For highly optimised code it provides a good solution for producing lean code. All in all there are enough valid reasons for goto that not including it in a language is a mistake.</p>
<h2>Alternatives</h2>
<p>Wherever there is an alternative to goto it should be used. For example, breaking out of multiple levels of nesting can be done with a &#8220;continue&#8221; keyword which accepts a label for a loop construct. Common exit code from a function can be handled via a &#8220;finally&#8221; block. Often a goto can be avoided simply by using a closure or local function with a return statement.</p>
<h2>Restrictions</h2>
<p>Nobody will claim that unlimited goto is ever required. We aren&#8217;t trying to recreate setjmp/longjmp, which are truly hard to resolve in a modern language. To preserve stack integrity goto can only ever allow one to go up in the stack. Preserving caller context may also require that goto can only be used within a function.  These limitations are reasonable, and most uses of goto live within these.</p>
<h2>Implications</h2>
<p>Obviously if the execution path is going to be jumping about very clear rules about variable life-time are necessary. This is the clearest reason why goto can only ever go up the stack. Going down the stack means entering scopes of variables which haven&#8217;t yet been initialized. So using a goto will unwind the stack up to the level of the destination. In terms of resources this essentially requires the RIAA pattern for resource management: we can&#8217;t allow the goto to leak resources.</p>
<p>In the destination scope it also follows that the construction of any variables is not skipped. (C++ has this rule.) That is, in the scope where the label exists, all variables after that label must have been constructed and initialized prior to the goto statement.</p>
<h2>Questions</h2>
<p>Jumping outside of the current function probably doesn&#8217;t make sense. The most signficant reason is that there is no guarantee that the calling function has the destination label defined. We need to make some allowances however. If the language makes use of anonymous functions, closures and lambdas it may make sense to allow goto in those scopes. If those components are specified elsewhere however, for convenience, or reuse, would goto be allowed?</p>
<h3>Examples</h3>
<p>Here is a random collection of places where <em>goto </em>may be a suitable option.</p>
<h4>Breaking from inner loops</h4>
<p>This a common example of where goto is helpful. You have an inner loop and wish to break out of the outer loop. For example, say you wish to find an element in a matrix.</p>
<p><pre class="brush: cpp;">
	location_t where;
	for( int i=0; i &lt; num_rows; ++i )
	{
		for( int j=0; j &lt; num_cols; ++j )
		{
			if( matrix(i,j).is_what_we_want() )
			{
				where.set(i,j);
				goto found;
			}
		}
	}
	throw error( &quot;not-found&quot; );

	found:
	//do something with it
</pre></p>
<p>There are obviously other ways to doing this to avoid the &#8220;goto&#8221;, but this code is perfectly clear and easy to follow. It does not make sense to code this another way if the only reason is to avoid using &#8220;goto&#8221;.</p>
<h4>Redoing a block of code</h4>
<p>You have a bit of code which you might need to execute multiple times within the same scope. A &#8220;goto&#8221; can sometimes be the clearest option to express this logic.</p>
<p><pre class="brush: cpp;">
	redo:
		...
		if( must_redo_expr )
			goto redo;
		...
		if( another_redo_expr )
			goto redo;
</pre></p>
<p>The alternate, and popular approach, is to use a while loop.</p>
<p><pre class="brush: cpp;">
	while( true )
	{
		...
		if( must_redo_expr )
			continue;
		...
		if( another_redo_expr )
			continue;

		break;
	};
</pre></p>
<p>The loop approach slightly obscures the logic of the code. It also has a severe limitation that if you have an inner loop you simply can&#8217;t use &#8220;continue&#8221; to get to the outer loop. Furthermore, it introduces a new scope which can be a problem for certain variables. Now, you can virtually always use a loop instead, but why would you do that if it requires a lot of code juggling and ends up making the code less readable?</p>
<h4>Error handling</h4>
<p>Error handling within a function can more complicated than simply returning &#8220;false&#8221; or throwing a simple exception. Said error handling may depend on numerous local variables.</p>
<p><pre class="brush: cpp;">
	int function( int a, int b, int c )
	{
		int d = 0;
		...
		if( detect_problem )
			goto broken;
		...
		if( other_problem )
			goto broken;
		...
		return d;

	broken:
		report( a, d );
		resolve( c );
		return -1;
	}
</pre></p>
<p>Repeating the error handling code multiple times would be bad (code duplication is always bad). You might be able to create an external functon, but then you must take care to pass all the variables correctly in each case.</p>
<p>This type of error handling is kind of like a miniature exception handling. You can basically create local &#8220;try-catch&#8221; conditions without the overhead (both runtime and syntactic) of using exceptions. It keeps your primary logic easier to read by pushing error handling off somewhere else.</p>
<h4>State machine</h4>
<p>Sometimes the logic of an algorithm is just not amenable to representation as a tree. This quite often appears when doing parsing or encoding.</p>
<p><pre class="brush: cpp;">
	void handle_stream()
	{
	idle_state:
		...
		if( detect_high )
			goto high_state;
		if( detect_low )
			goto low_state;
		if( detect_end )
			goto end_state;
		goto idle_state;

	high_state:
		...
		if( continue_high )
			goto high_state;
		goto low_state;

	low_state:
		...
		goto idle_state;

	end_state:
		...
		return;
	}
</pre></p>
<p>Though state machines are pretty common we don&#8217;t see this type of code very much. Most of such code tends to be created by code generators, such as a parser generator or a protocol generator. In cases where performance is not an issue you&#8217;ll instead see a higher level functional, or object based approach &#8212; using a &#8220;switch&#8221; statement or function pointers you can avoid using &#8220;goto&#8221;, though the logic is the same.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mortoray.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mortoray.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mortoray.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mortoray.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mortoray.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mortoray.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mortoray.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mortoray.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mortoray.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mortoray.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mortoray.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mortoray.wordpress.com/74/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mortoray.wordpress.com/74/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mortoray.wordpress.com/74/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mortoray.wordpress.com&amp;blog=11292855&amp;post=74&amp;subd=mortoray&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>https://mortoray.wordpress.com/2011/10/23/the-ideal-language-has-goto/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="https://secure.gravatar.com/avatar/e6ce612702a34f9bcebee2f34139c3f1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mortoray</media:title>
		</media:content>
	</item>
	</channel>
</rss>
