<?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>Mlangc&#039;s Blog</title>
	<atom:link href="http://mlangc.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://mlangc.wordpress.com</link>
	<description>--- if you think you&#039;re smarter than the badblocks program, you almost certainly aren&#039;t.</description>
	<lastBuildDate>Thu, 25 Aug 2011 02:09:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='mlangc.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Mlangc&#039;s Blog</title>
		<link>http://mlangc.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://mlangc.wordpress.com/osd.xml" title="Mlangc&#039;s Blog" />
	<atom:link rel='hub' href='http://mlangc.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Series About Java Concurrency – Pt. 4</title>
		<link>http://mlangc.wordpress.com/2011/05/19/series-about-java-concurrency-pt-4/</link>
		<comments>http://mlangc.wordpress.com/2011/05/19/series-about-java-concurrency-pt-4/#comments</comments>
		<pubDate>Thu, 19 May 2011 21:59:30 +0000</pubDate>
		<dc:creator>mlangc</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Concurrency]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[puzzle]]></category>

		<guid isPermaLink="false">http://mlangc.wordpress.com/?p=547</guid>
		<description><![CDATA[This post is the solution of Series About Java Concurrency – Pt. 3, so be sure to read my last post before this one. So, what does the code from Series About Java Concurrency – Pt. 3 do? As it turns out, there isn&#8217;t a single answer: If you are using a recent Oracle VM [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mlangc.wordpress.com&amp;blog=10679887&amp;post=547&amp;subd=mlangc&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This post is the solution of <a href="http://mlangc.wordpress.com/2011/05/10/series-about-java-concurrency-pt-3/">Series About Java Concurrency – Pt. 3</a>, so be sure to read my last post before this one.</p>
<p>So, what does the code from <a href="http://mlangc.wordpress.com/2011/05/10/series-about-java-concurrency-pt-3/">Series About Java Concurrency – Pt. 3</a> do? As it turns out, there isn&#8217;t a single answer: If you are using a recent Oracle VM the behavior of the program seems to depend on whether your VM runs in <a href="http://download.oracle.com/javase/6/docs/technotes/guides/vm/server-class.html">server or client mode</a>. In client mode the program will most likely stop after roughly 2.5s as one would naively expect. In server mode however, the program just loops forever. The update to <i>stop</i> in line 24 never gets visible to the main thread. This isn&#8217;t a bug, but perfectly legal behavior according to the <a href="http://java.sun.com/docs/books/jls/third_edition/html/memory.html#17.4">Java Memory Model</a>, which allows optimizing</p>
<pre class="brush: java; first-line: 33;">
while(!stop)
</pre>
<p>into</p>
<pre class="brush: java; first-line: 33;">
while(true)
</pre>
<p>Synchronization is not just about avoiding data races; it is also needed to avoid reading stale data. This may sound weired, but allows the VM as well as the CPU to apply powerful optimizations (for example <a href="http://en.wikipedia.org/wiki/Out-of-order_execution">Out-of-order execution</a>) that would otherwise be impossible or very hard to implement. </p>
<p>One way to fix our shiny little program is by using the <i>synchronized</i> keyword as demonstrated below:</p>
<pre class="brush: java;">
package com.wordpress.mlangc.concurrent;

public class PleaseWait
{
    private static final long MILLIS_TO_WAIT = 2500;
    private static boolean stop = false;

    private static synchronized boolean isStop()
    {
        return stop;
    }

    private static synchronized void setStop(boolean stop)
    {
        PleaseWait.stop = stop;
    }

    public static void main(String[] args)
    {
        Thread timer = new Thread(new Runnable()
        {
            public void run()
            {
                try
                {
                    Thread.sleep(MILLIS_TO_WAIT);
                }
                catch(InterruptedException e)
                {
                    // We are already exiting the thread.
                }
                finally
                {
                    setStop(true);
                    System.out.println(&quot;Stop requested.&quot;);
                }
            }
        });

        timer.start();

        long start = System.currentTimeMillis();
        while(!isStop())
            ; // &lt;-- Do nothing.

        long stoppedAfter = System.currentTimeMillis() - start;
        System.out.printf(&quot;Stopped after %dms.\n&quot;, stoppedAfter);
    }
}
</pre>
<p>It is important to note that both the setter and the getter are synchronized using the same lock. Synchronizing only the setter method is not enough! </p>
<p>While the code above works reasonably well, we can still do better because we don&#8217;t need mutual exclusion, but just want to ensure that main thread sees what the timer thread does. This can be accomplished quite easily by declaring <i>stop</i> to be <i>volatile</i>. The <i>volatile</i> keyword makes sure that the main thread always sees to most recent value of <i>stop</i> without any additional uses of <i>synchronized</i>. Following this advice, we end up with something like this:</p>
<pre class="brush: java;">
package com.wordpress.mlangc.concurrent;

public class PleaseWait
{
    private static final long MILLIS_TO_WAIT = 2500;
    private static volatile boolean stop = false;

    public static void main(String[] args)
    {
        Thread timer = new Thread(new Runnable()
        {
            public void run()
            {
                try
                {
                    Thread.sleep(MILLIS_TO_WAIT);
                }
                catch(InterruptedException e)
                {
                    // We are already exiting the thread.
                }
                finally
                {
                    stop = true;
                    System.out.println(&quot;Stop requested.&quot;);
                }
            }
        });

        timer.start();

        long start = System.currentTimeMillis();
        while(!stop)
            ; // &lt;-- Do nothing.

        long stoppedAfter = System.currentTimeMillis() - start;
        System.out.printf(&quot;Stopped after %dms.\n&quot;, stoppedAfter);
    }
}
</pre>
<p>The only difference to the broken version from <a href="http://mlangc.wordpress.com/2011/05/10/series-about-java-concurrency-pt-3/">Series About Java Concurrency – Pt. 3</a> is the <i>volatile</i> keyword in line 6.</p>
<p>Last but not least I want to state clearly that this article is not about the proper way to implement task cancellation, which is a nontrivial topic of it&#8217;s own. If parts of this post are new to you, I strongly suggest that you grab yourself a copy of the excellent book <a href="http://jcip.net/">Java Concurrency in Practice</a> and read at least the first chapter called <i>Fundamentals</i> thoroughly. By doing so you are almost certainly saving yourself from nasty surprises or frustrating debugging session in the future.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mlangc.wordpress.com/547/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mlangc.wordpress.com/547/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mlangc.wordpress.com/547/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mlangc.wordpress.com/547/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mlangc.wordpress.com/547/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mlangc.wordpress.com/547/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mlangc.wordpress.com/547/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mlangc.wordpress.com/547/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mlangc.wordpress.com/547/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mlangc.wordpress.com/547/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mlangc.wordpress.com/547/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mlangc.wordpress.com/547/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mlangc.wordpress.com/547/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mlangc.wordpress.com/547/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mlangc.wordpress.com&amp;blog=10679887&amp;post=547&amp;subd=mlangc&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy"></div>]]></content:encoded>
			<wfw:commentRss>http://mlangc.wordpress.com/2011/05/19/series-about-java-concurrency-pt-4/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f50add19b65e82fe8dbabc438c402ed?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mlangc</media:title>
		</media:content>
	</item>
		<item>
		<title>Series About Java Concurrency – Pt. 3</title>
		<link>http://mlangc.wordpress.com/2011/05/10/series-about-java-concurrency-pt-3/</link>
		<comments>http://mlangc.wordpress.com/2011/05/10/series-about-java-concurrency-pt-3/#comments</comments>
		<pubDate>Tue, 10 May 2011 21:59:25 +0000</pubDate>
		<dc:creator>mlangc</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Concurrency]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[puzzle]]></category>

		<guid isPermaLink="false">http://mlangc.wordpress.com/?p=528</guid>
		<description><![CDATA[As it seems that even experienced Java programmers might have serious misconceptions regarding the Java memory model, I&#8217;ve decided to publish a concurrency related puzzle once again. Consider the following simple program and try to predict it&#8217;s output: The solution, together with explanations will be published in the near future.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mlangc.wordpress.com&amp;blog=10679887&amp;post=528&amp;subd=mlangc&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>As it seems that even experienced Java programmers might have serious misconceptions regarding the Java memory model, I&#8217;ve decided to publish a <a href="http://mlangc.wordpress.com/2009/11/27/series-about-java-concurrency-pt-1/">concurrency related puzzle</a> once again. Consider the following simple program and try to predict it&#8217;s output:</p>
<pre class="brush: java;">
package com.wordpress.mlangc.concurrent;

public class PleaseWait
{
    private static final long MILLIS_TO_WAIT = 2500;
    private static boolean stop = false;

    public static void main(String[] args)
    {
        Thread timer = new Thread(new Runnable()
        {
            public void run()
            {
                try
                {
                    Thread.sleep(MILLIS_TO_WAIT);
                }
                catch(InterruptedException e)
                {
                    // We are already exiting the thread.
                }
                finally
                {
                    stop = true;
                    System.out.println(&quot;Stop requested.&quot;);
                }
            }
        });

        timer.start();

        long start = System.currentTimeMillis();
        while(!stop)
            ; // &lt;-- Do nothing.

        long stoppedAfter = System.currentTimeMillis() - start;
        System.out.printf(&quot;Stopped after %dms.\n&quot;, stoppedAfter);
    }
}
</pre>
<p>The solution, together with explanations will be published in the near future.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mlangc.wordpress.com/528/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mlangc.wordpress.com/528/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mlangc.wordpress.com/528/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mlangc.wordpress.com/528/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mlangc.wordpress.com/528/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mlangc.wordpress.com/528/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mlangc.wordpress.com/528/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mlangc.wordpress.com/528/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mlangc.wordpress.com/528/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mlangc.wordpress.com/528/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mlangc.wordpress.com/528/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mlangc.wordpress.com/528/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mlangc.wordpress.com/528/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mlangc.wordpress.com/528/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mlangc.wordpress.com&amp;blog=10679887&amp;post=528&amp;subd=mlangc&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy"></div>]]></content:encoded>
			<wfw:commentRss>http://mlangc.wordpress.com/2011/05/10/series-about-java-concurrency-pt-3/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f50add19b65e82fe8dbabc438c402ed?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mlangc</media:title>
		</media:content>
	</item>
		<item>
		<title>When True Is Not True Anymore</title>
		<link>http://mlangc.wordpress.com/2010/07/18/when-true-is-not-true-anymore/</link>
		<comments>http://mlangc.wordpress.com/2010/07/18/when-true-is-not-true-anymore/#comments</comments>
		<pubDate>Sun, 18 Jul 2010 21:50:40 +0000</pubDate>
		<dc:creator>mlangc</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://mlangc.wordpress.com/?p=438</guid>
		<description><![CDATA[We all know that accessing uninitialized variables in C and C++ usually leads to some kind of undefined behavior one usually wants to avoid. What I didn&#8217;t know until recently is that uninitialized bool values might be especially malicious beasts. To see what they can do to you, take a look at the following program, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mlangc.wordpress.com&amp;blog=10679887&amp;post=438&amp;subd=mlangc&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>We all know that accessing uninitialized variables in C and C++ usually leads to some kind of undefined behavior one usually wants to avoid. What I didn&#8217;t know until recently is that uninitialized <i>bool</i> values might be especially malicious beasts. To see what they can do to you, take a look at the following program, and try to predict its output:</p>
<pre class="brush: cpp;">
#include &lt;string&gt;
#include &lt;iostream&gt;

using namespace std;

namespace {

    inline string stringify(const bool value)
    {
        return (value ? &quot;true&quot; : &quot;false&quot;);
    }

    struct Struct
    {
        long l;
        bool u;
    };
}

int main()
{
    Struct s;

    if(true != s.u)
        cout &lt;&lt; stringify(true) &lt;&lt; &quot; != &quot; &lt;&lt; stringify(s.u) &lt;&lt; endl;
}
</pre>
<p>Now type <i>g++ stringify.cc -o stringify</i> and run the generated executable. Here is what you might see on some platforms:</p>
<pre class="brush: plain; gutter: false;">
$ ./stringify
true != true
</pre>
<p>Yes, you got that right <b><i>true != true</i></b>! I get this behaviour with <i>g++-4.3</i> and <i>g++-4.4</i> on <a href="http://www.gentoo.org/">Gentoo</a> (<i>x86</i> and <i>x86_64</i>) as well as with <i>g++-4.1</i> and <i>g++-4.4</i> on <a href="http://www.ubuntu.com/">Ubuntu 9.10</a> (<i>x86_64</i>). Before attempting to explain what happened here, I want to summarize a few additional facts:</p>
<ul>
<li>
Several attempts to make this program shorter without ending up with something boring failed.
</li>
<li>
Turning on optimization causes <i>g++</i> optimize all <i>if</i> statements away (especially the implicit one in <i>stringify</i>) under the assumption that <i>s.u</i> is <i>false</i>, which again leads to a much more sane output.
</li>
<li>
I could not reproduce this with <i>icc-11.1</i>.
</li>
</ul>
<p>So, what happened? Is <i>g++</i> broken? Actually the answer is no. Accessing uninitialized memory leads to undefined behavior, and undefined means undefined. In fact the <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3092.pdf">C++0x Final Committee Draft</a> contains a footnote that explicitly mentions the oddity we have just seen:</p>
<blockquote><p>
 47) Using a bool value in ways described by this International Standard as “undefined,” such as by examining the value of an<br />
uninitialized automatic object, might cause it to behave as if it is neither true nor false.
</p></blockquote>
<p>This is not that surprising if one considers that at assembler level, a <i>bool</i> is not represented by a single bit, but at least by a <i>byte</i>. An uninitialized byte might have 256 different values, and not just two. One could of course consistently map 0 to <i>false</i> and everything else to <i>true</i>, but this is not what <i>g++</i> does. To see what I mean, take a look at the following assembler snippet, that <i>g++</i> generated for the <i>if</i> statement in line 24:</p>
<pre class="brush: plain;">
movzbl  -40(%rbp), %eax  # move s.u to eax.
xorl    $1, %eax         # xor eax with 1.
testb	%al, %al         # check if the low byte of eax is 0.
je      .L8              # jump to .L8 if so.
</pre>
<p>If the jump is taken, the body of the <i>if</i> statement in line 24 is skipped, otherwise it is executed. Now the <i>xorl</i> in line 2 switches the lowest bit in <i>eax</i>, leaving all other bits unchanged. Therefore <i>s.u</i> is considered to be equal to true if and only if it has the byte value 0&#215;01.</p>
<p>Now lets take a look at the assembler that represents the ternary operator in <i>stringify</i>:</p>
<pre class="brush: plain;">
cmpb $0, -36(%rbp)    # compare the argument with 0.
je   .L2              # jump to .L2 if the argument is 0.
movl $.LC0, %eax      # store &quot;true&quot; in eax.
jmp  .L3              # jump out.
.L2:
     movl $.LC1, %eax # store &quot;false&quot; in eax.
.L3:
</pre>
<p>Here <i>g++</i> maps 0 to <i>false</i> and every other value to true. This means that if the actual byte value of <i>s.u</i> is for example 0xFF (which for some reason is what <a href="http://cgdb.sourceforge.net/">cgdb</a> keeps telling me locally), the <i>if</i> in line 24 will be taken as if <i>s.u</i> was <i>false</i>, but <i>stringify</i> will behave as if <i>s.u</i> was true.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mlangc.wordpress.com/438/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mlangc.wordpress.com/438/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mlangc.wordpress.com/438/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mlangc.wordpress.com/438/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mlangc.wordpress.com/438/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mlangc.wordpress.com/438/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mlangc.wordpress.com/438/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mlangc.wordpress.com/438/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mlangc.wordpress.com/438/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mlangc.wordpress.com/438/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mlangc.wordpress.com/438/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mlangc.wordpress.com/438/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mlangc.wordpress.com/438/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mlangc.wordpress.com/438/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mlangc.wordpress.com&amp;blog=10679887&amp;post=438&amp;subd=mlangc&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy"></div>]]></content:encoded>
			<wfw:commentRss>http://mlangc.wordpress.com/2010/07/18/when-true-is-not-true-anymore/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f50add19b65e82fe8dbabc438c402ed?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mlangc</media:title>
		</media:content>
	</item>
		<item>
		<title>Be Careful When Converting Java Arrays to Lists</title>
		<link>http://mlangc.wordpress.com/2010/05/01/be-carefull-when-converting-java-arrays-to-lists/</link>
		<comments>http://mlangc.wordpress.com/2010/05/01/be-carefull-when-converting-java-arrays-to-lists/#comments</comments>
		<pubDate>Sat, 01 May 2010 16:03:52 +0000</pubDate>
		<dc:creator>mlangc</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[Collections]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://mlangc.wordpress.com/?p=360</guid>
		<description><![CDATA[Unfortunately not everything that should be trivial actually is. One example is converting Java arrays to lists. Of course, there is Arrays.toList, but using this method carelessly will almost certainly lead to nasty surprises. To see what I mean consider the following program and try to predict its output: As the Javadocs for Arrays.asList are [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mlangc.wordpress.com&amp;blog=10679887&amp;post=360&amp;subd=mlangc&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Unfortunately not everything that should be trivial actually is. One example is converting Java arrays to <a href="http://java.sun.com/javase/6/docs/api/java/util/List.html">lists</a>. Of course, there is <a href="http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#asList%28T...%29">Arrays.toList</a>, but using this method carelessly will almost certainly lead to nasty surprises. To see what I mean consider the following program and try to predict its output:</p>
<pre class="brush: java;">
package com.wordpress.mlangc.arrays;

import java.util.Arrays;

public class ArraysToList
{
    public static void main(final String[] args)
    {
        System.out.println(
                Arrays.asList(new String[] { &quot;a&quot;, &quot;b&quot; }));

        System.out.println(
                Arrays.asList(new Integer[] { 1, 2 }));

        System.out.println(
                Arrays.asList(new int[] { 1, 2 }));

        System.out.println(
                Arrays.asList(new String[] { &quot;a&quot;, &quot;b&quot; }, &quot;c&quot;));
    }
}
</pre>
<p>As the Javadocs for <i>Arrays.asList</i> are quite vague, I can&#8217;t blame you for having some difficulties coming to a conclusion, so here is the answer step by step:</p>
<ul>
<li>
Line 9 prints <i>&#8220;[a, b]&#8220;</i> to our shiny little console which is pretty much what one would expect from a sane API, so we are happy.
</li>
<li>
The same is true for line 12 which results in <i>&#8220;[1, 2]&#8220;</i>.
</li>
<li>
Line 15 however is different, not only because 15 is not 12, but also because an <i>int</i> is not an <i>Integer</i>, and therefore prints <i>&#8220;[[I@39172e08]&#8220;</i> to our console, that is not shiny anymore. Instead of a list containing two <i>Integer</i> objects, we got a list containing the array as its sole element.
</li>
<li>
After what we have seen above, it should not take you by surprise that line 18 results in another mess that looks like <i>&#8220;[[Ljava.lang.String;@20cf2c80, c]&#8220;</i>.
</li>
</ul>
<p>So, what happened? The first two print statements worked as expected, because the <a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#45449">Java Language Specification</a> states that calling a method with signature <i>foo(T&#8230; t)</i> like <i>foo(new T[] { bar, baz })</i> is semantically equivalent to <i>foo(bar, baz)</i>. In <i>Arrays.asList</i> <i>T</i> is a type parameter, so it has to be an <i>Object</i>, and this is not true for <i>int</i>, but for <i>int[]</i>. Thats why the statement in line 16 is equivalent to</p>
<pre class="brush: java; gutter: false;">
Arrays.asList(new Object[] { new int[] { 1, 2 } })
</pre>
<p>Last but not least, the statement in line 19 called for trouble from the very beginning. We told the compiler that we want a list containing an array of strings and a string, which is exactly what we got.</p>
<p>So far for the explanation, but there is something else we can learn from that: The real source of confusion is not that varargs feature is badly designed; I would rather say that the opposite is true. The problem is that <i>Arrays.asList</i> violates <a href="http://java.sun.com/docs/books/effective/">EffJava2</a> Item 42, which explains clearly, in fact giving <i>Arrays.asList</i> as a bad example, why you should be quite careful when designing APIs that use Java varargs. I won&#8217;t repeat the reasoning of the book here, as you really should read it yourself, but for the sake of completeness I have to point out that the problematic statements from above would have been rejected by the compiler back in the old days of Java 1.4, which was a good thing. We can still use <i>Arrays.asList</i> today, but doing so safely requires us to be aware of the subtillities we are facing. Here are a few rules for converting arrays to lists, that guarantee nothing unexpected happens:</p>
<ul>
<li>
If you convert to a list just to convert to a string, use <a href="http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#toString%28java.lang.Object[]%29">Arrays.toString</a> instead. It does what you want all the time and also works for arrays of primitives.
</li>
<li>
If you want to convert an array of primitives to a list of boxed primitives, take advantage of <a href="http://commons.apache.org/lang/">Apache Commons Lang</a>, which most likely is a dependency of your project already anyway, and use <a href="http://commons.apache.org/lang/api-release/org/apache/commons/lang/ArrayUtils.html#toObject%28int[]%29">ArrayUtils.toObject</a> like this:</p>
<pre class="brush: java; gutter: false;">
List&lt;Integer&gt; list = Arrays.asList(ArrayUtils.toObject(new int[] { 1, 2 }));
</pre>
<p>Note however that <a href="http://mlangc.wordpress.com/2010/01/09/what-you-dont-want-to-know-about-java-boxed-primitives/">lists of boxed primitives should not generally be preferred over arrays containing primitives</a>.
</li>
<li>
If you want to convert an array of object references, use <i>Arrays.asList</i> directly:</p>
<pre class="brush: java; gutter: false;">
 List&lt;String&gt; list =
           Arrays.asList(new String[] { &quot;a&quot;, &quot;b&quot; });
</pre>
<p>Don&#8217;t forget to make sure that the people you work with won&#8217;t emulate you carelessly.
</li>
</ul>
<p>Of course, you can also choose to just remember that <i>Arrays.asList</i> might behave unexpectedly and use plain <i>for</i> loops instead, but that clutters your code and comes with a performance penalty.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mlangc.wordpress.com/360/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mlangc.wordpress.com/360/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mlangc.wordpress.com/360/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mlangc.wordpress.com/360/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mlangc.wordpress.com/360/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mlangc.wordpress.com/360/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mlangc.wordpress.com/360/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mlangc.wordpress.com/360/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mlangc.wordpress.com/360/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mlangc.wordpress.com/360/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mlangc.wordpress.com/360/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mlangc.wordpress.com/360/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mlangc.wordpress.com/360/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mlangc.wordpress.com/360/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mlangc.wordpress.com&amp;blog=10679887&amp;post=360&amp;subd=mlangc&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy"></div>]]></content:encoded>
			<wfw:commentRss>http://mlangc.wordpress.com/2010/05/01/be-carefull-when-converting-java-arrays-to-lists/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f50add19b65e82fe8dbabc438c402ed?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mlangc</media:title>
		</media:content>
	</item>
		<item>
		<title>Using C++0x Variadic Templates to Pretty-Print Types</title>
		<link>http://mlangc.wordpress.com/2010/04/18/using-c0x-variadic-templates-to-pretty-print-types/</link>
		<comments>http://mlangc.wordpress.com/2010/04/18/using-c0x-variadic-templates-to-pretty-print-types/#comments</comments>
		<pubDate>Sun, 18 Apr 2010 19:44:10 +0000</pubDate>
		<dc:creator>mlangc</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[C++0x]]></category>
		<category><![CDATA[templates]]></category>

		<guid isPermaLink="false">http://mlangc.wordpress.com/?p=283</guid>
		<description><![CDATA[Recently I stumbled over an exercise in the very interesting book C++ Template Metaprogramming. The challenge of the exercise is to make something like the following compile and do what you expect: Note that we cannot use typeid, as typeid(T).name() is not required to print anything meaningful, and in practice doesn&#8217;t. As long as we [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mlangc.wordpress.com&amp;blog=10679887&amp;post=283&amp;subd=mlangc&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Recently I stumbled over an exercise in the very interesting book <a href="http://www.boostpro.com/mplbook/">C++ Template Metaprogramming</a>. The challenge of the exercise is to make something like the following compile and do what you expect:</p>
<pre class="brush: cpp; gutter: false;">
cout &lt;&lt; PrintType&lt;int const* const**&amp;&gt;() &lt;&lt; endl;
</pre>
<p>Note that we cannot use <i>typeid</i>, as <i>typeid(T).name()</i> is not required to print anything meaningful, and in practice doesn&#8217;t. As long as we don&#8217;t support function pointers, the problem from above can be solved rather easily using template specializations, although it requires quite some typing:</p>
<pre class="brush: cpp;">
#ifndef HH_PRINT_TYPE
#define HH_PRINT_TYPE

#include &lt;ostream&gt;
#include &lt;boost/tr1/type_traits.hpp&gt;
#include &lt;boost/lexical_cast.hpp&gt;

namespace mine {

    template&lt;class T_&gt; struct PrintType
    {
        // Never used directly.
    };

    /**
     * For plain types we don't know.
     *
     * Could be enhanced by using type_traits.
     */
    template&lt;class T_&gt;
    std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, PrintType&lt;T_&gt;)
    {
        os &lt;&lt; &quot;T&quot;;
        return os;
    };

    /**
     * For void.
     */
    template&lt;&gt;
    std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, PrintType&lt;void&gt;)
    {
        os &lt;&lt; &quot;void&quot;;
        return os;
    };

    /**
     * For int.
     */
    template&lt;&gt;
    std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, PrintType&lt;int&gt;)
    {
        os &lt;&lt; &quot;int&quot;;
        return os;
    };

    /**
     * For char.
     */
    template&lt;&gt;
    std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, PrintType&lt;char&gt;)
    {
        os &lt;&lt; &quot;char&quot;;
        return os;
    };

    /**
     * For const qualified types.
     */
    template&lt;class T_&gt;
    std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, PrintType&lt;T_ const&gt;)
    {
        os &lt;&lt; PrintType&lt;T_&gt;() &lt;&lt; &quot; const&quot;;
        return os;
    };

    /**
     * For volatile qualified types.
     */
    template&lt;class T_&gt;
    std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, PrintType&lt;T_ volatile&gt;)
    {
        os &lt;&lt; PrintType&lt;T_&gt;() &lt;&lt; &quot; volatile&quot;;
        return os;
    };

    /**
     * For const volatile qualified types.
     */
    template&lt;class T_&gt;
    std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, PrintType&lt;T_ const volatile&gt;)
    {
        os &lt;&lt; PrintType&lt;T_&gt;() &lt;&lt; &quot; const volatile&quot;;
        return os;
    };

    /**
     * For reference types.
     */
    template&lt;class T_&gt;
    std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, PrintType&lt;T_&amp;&gt;)
    {
        os &lt;&lt; PrintType&lt;T_&gt;() &lt;&lt; &quot;&amp;&quot;;
        return os;
    };

    /**
     * For pointers.
     */
    template&lt;class T_&gt;
    std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, PrintType&lt;T_*&gt;)
    {
        os &lt;&lt; PrintType&lt;T_&gt;() &lt;&lt; &quot;*&quot;;
        return os;
    }

    /**
     * For pointers to members.
     */
    template&lt;class ClassT_, class MemberT_&gt;
    std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, PrintType&lt;MemberT_ ClassT_::*&gt;)
    {
        os &lt;&lt; PrintType&lt;MemberT_&gt;() &lt;&lt; &quot; &quot; &lt;&lt; PrintType&lt;ClassT_&gt;() &lt;&lt; &quot;::*&quot;;
        return os;
    }

    /**
     * Helper function for array types.
     */
    template&lt;std::size_t SizeP_, class T_&gt;
    std::string printDimensions(PrintType&lt;T_[SizeP_]&gt;)
    {
        return
            &quot;[&quot; + boost::lexical_cast&lt;std::string&gt;(SizeP_) + &quot;]&quot;
            + printDimensions(PrintType&lt;T_&gt;());
    }

    /**
     * Ends the recursion started by printDimensions(PrintType&lt;T_[SizeP_]&gt;).
     */
    template&lt;class T_&gt;
    std::string printDimensions(PrintType&lt;T_&gt;)
    {
        return &quot;&quot;;
    }

    /**
     * For arrays.
     */
    template&lt;std::size_t SizeP_, class T_&gt;
    std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, PrintType&lt;T_[SizeP_]&gt;)
    {
        os &lt;&lt; PrintType&lt;typename std::tr1::remove_all_extents&lt;T_&gt;::type&gt;()
           &lt;&lt; printDimensions(PrintType&lt;T_[SizeP_]&gt;());

        return os;
    }

    /**
     * For const arrays.
     */
    template&lt;std::size_t SizeP_, class T_&gt;
    std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, PrintType&lt;const T_[SizeP_]&gt;)
    {
        os &lt;&lt; &quot;const &quot; &lt;&lt; PrintType&lt;T_[SizeP_]&gt;();
        return os;
    }

    /**
     * For volatile arrays.
     */
    template&lt;std::size_t SizeP_, class T_&gt;
    std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, PrintType&lt;volatile T_[SizeP_]&gt;)
    {
        os &lt;&lt; &quot;volatile &quot; &lt;&lt; PrintType&lt;T_[SizeP_]&gt;();
        return os;
    }

    /**
     * For const volatile arrays.
     */
    template&lt;std::size_t SizeP_, class T_&gt;
    std::ostream&amp; operator&lt;&lt;(
            std::ostream&amp; os,
            PrintType&lt;const volatile T_[SizeP_]&gt;)
    {
        os &lt;&lt; &quot;const volatile &quot; &lt;&lt; PrintType&lt;T_[SizeP_]&gt;();
        return os;
    }
}
#endif
</pre>
<p>Of course, we are still missing quite a few full specializations but adding them is trivial. In fact, the only part of the code above that is slightly tricky is the part between line 120 and 147, that supports pretty-printing  of potentially multi dimensional arrays. Also note the explicit overloads for <i>const volatile</i> at lines 80 and 172. Without them we would get ambiguity errors if trying to pretty print a type that is qualified as such.</p>
<p>While this is all very nice and impressive, the code above still fails us miserably if we do something like</p>
<pre class="brush: cpp; gutter: false;">
 cout &lt;&lt; mine::PrintType&lt;int(int, char, void*)&gt;() &lt;&lt; endl;
</pre>
<p>or even</p>
<pre class="brush: cpp; gutter: false;">
 cout &lt;&lt; mine::PrintType&lt;int(SomeClass::*)(int, char, void*, ...)&gt;() &lt;&lt; endl;
</pre>
<p>as we didn&#8217;t yet add support for function pointers. We can of course fix this by adding more specializations, but as functions can take a nearly arbitrary number of arguments (the C++0x <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2010/n3092.pdf">Final Committee Draft</a> suggests implementations to support at least 256), doing this in C++03 would be a rather unpleasant experience. Using C++0x variadic templates on the other hand side, supporting function types is not a big deal (as soon as one understands how variadic templates work <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> ):</p>
<pre class="brush: cpp;">
#ifdef __GXX_EXPERIMENTAL_CXX0X__ //&lt;-- set by gcc for '-std=c++0x'.
    /**
     * Helper struct for printing parameter lists.
     *
     * @tparam StartsP_
     *  true iff we are at the start of the list (needed to omit
     *  commas in the output).
     * @tparam ArgsT_
     *  the actual arguments.
     */
    template&lt;bool StartsP_, class... ArgsT_&gt; struct PrintParameters
    {
        // Never used directly.
    };

    /**
     * For parameter lists.
     */
    template&lt;bool StartsP_, class ArgT, class... ArgsT_&gt;
    std::ostream&amp; operator&lt;&lt;(
            std::ostream&amp; os,
            PrintParameters&lt;StartsP_, ArgT, ArgsT_...&gt;)
    {
        os &lt;&lt; (StartsP_ ? &quot;&quot; : &quot;, &quot;)
           &lt;&lt; PrintType&lt;ArgT&gt;()
           &lt;&lt; PrintParameters&lt;false, ArgsT_...&gt;();

        return os;
    }

    /**
     * Terminates the recursion.
     */
    template&lt;bool StartsP_&gt;
    std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, PrintParameters&lt;StartsP_&gt;)
    {
        return os;
    }

    /**
     * For function pointers.
     */
    template&lt;class RetT_, class... ArgsT_&gt;
    std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, PrintType&lt;RetT_(ArgsT_...)&gt;)
    {
        os &lt;&lt; PrintType&lt;RetT_&gt;()
           &lt;&lt; &quot;(&quot; &lt;&lt; PrintParameters&lt;true, ArgsT_...&gt;() &lt;&lt; &quot;)&quot;;

        return os;
    }

    /**
     * For member functions.
     */
    template&lt;class ClassT_, class RetT_, class... ArgsT_&gt;
    std::ostream&amp; operator&lt;&lt;(
            std::ostream&amp; os,
            PrintType&lt;RetT_(ClassT_::*)(ArgsT_...)&gt;)
    {
        os &lt;&lt; PrintType&lt;RetT_&gt;()
           &lt;&lt; &quot;(&quot; &lt;&lt; PrintType&lt;ClassT_&gt;()
           &lt;&lt; &quot;::*)(&quot; &lt;&lt; PrintParameters&lt;true, ArgsT_...&gt;() &lt;&lt; &quot;)&quot;;

        return os;
    }

    /**
     * For parameter lists with varargs.
     */
    template&lt;bool StartsP_, class... ArgsT_&gt; struct PrintParamtersWithVarArgs
    {
        // Never used directly.
    };

    /**
     * For function pointers with varargs.
     */
    template&lt;class RetT_, class... ArgsT_&gt;
    std::ostream&amp; operator&lt;&lt;(
            std::ostream&amp; os,
            PrintType&lt;RetT_(ArgsT_... ...)&gt;)
    {
        os &lt;&lt; PrintType&lt;RetT_&gt;()
           &lt;&lt; &quot;(&quot; &lt;&lt; PrintParameters&lt;true, ArgsT_...&gt;() &lt;&lt; &quot;...)&quot;;

        return os;
    }

    /**
     * For member functions with varargs.
     */
    template&lt;class RetT_, class ClassT_, class... ArgsT_&gt;
    std::ostream&amp; operator&lt;&lt;(
            std::ostream&amp; os,
            PrintType&lt;RetT_(ClassT_::*)(ArgsT_... ...)&gt;)
    {
        os &lt;&lt; PrintType&lt;RetT_&gt;()
           &lt;&lt; &quot;(&quot; &lt;&lt; PrintType&lt;ClassT_&gt;()
           &lt;&lt; &quot;::*)(&quot; &lt;&lt; PrintParameters&lt;true, ArgsT_...&gt;() &lt;&lt; &quot;...)&quot;;

        return os;
    }

    /**
     * For rvalue references (for completeness).
     */
    template&lt;class T_&gt;
    std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, PrintType&lt;T_&amp;&amp;&gt;)
    {
        os &lt;&lt; PrintType&lt;T_&gt;() &lt;&lt; &quot;&amp;&amp;&quot;;
        return os;
    };
#endif
</pre>
<p>The main workhorse in the code above is the operator at line 19, that calls itself recursively in line 26 until it eventually degenerates into the operator defined at line 34.  Also note the double ellipsis in lines 81 and 95: The first one unpacks the template parameter list, the second one is for old style varargs.</p>
<p>Last but not least: Feel free to come up with suggestions or comments.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mlangc.wordpress.com/283/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mlangc.wordpress.com/283/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mlangc.wordpress.com/283/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mlangc.wordpress.com/283/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mlangc.wordpress.com/283/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mlangc.wordpress.com/283/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mlangc.wordpress.com/283/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mlangc.wordpress.com/283/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mlangc.wordpress.com/283/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mlangc.wordpress.com/283/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mlangc.wordpress.com/283/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mlangc.wordpress.com/283/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mlangc.wordpress.com/283/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mlangc.wordpress.com/283/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mlangc.wordpress.com&amp;blog=10679887&amp;post=283&amp;subd=mlangc&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy"></div>]]></content:encoded>
			<wfw:commentRss>http://mlangc.wordpress.com/2010/04/18/using-c0x-variadic-templates-to-pretty-print-types/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f50add19b65e82fe8dbabc438c402ed?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mlangc</media:title>
		</media:content>
	</item>
		<item>
		<title>Saying Goodbye to boost::regex.</title>
		<link>http://mlangc.wordpress.com/2010/04/05/saying-goodbye-to-boostregex/</link>
		<comments>http://mlangc.wordpress.com/2010/04/05/saying-goodbye-to-boostregex/#comments</comments>
		<pubDate>Mon, 05 Apr 2010 21:41:22 +0000</pubDate>
		<dc:creator>mlangc</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[boost]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Regular Expressions]]></category>
		<category><![CDATA[UTF-8]]></category>

		<guid isPermaLink="false">http://mlangc.wordpress.com/?p=254</guid>
		<description><![CDATA[Yesterday I decided to finally say goodbye to boost::regex (a.k.a tr1::regex) and use PCRE instead in Untropy. The reason for this is, that boost::regex doesn&#8217;t support UTF-8 properly. Yes, there is boost::u32regex together with a few related types and functions, but as the name says, they work with UTF-32, not UTF-8. This wouldn&#8217;t be a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mlangc.wordpress.com&amp;blog=10679887&amp;post=254&amp;subd=mlangc&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Yesterday I decided to finally say goodbye to <a href="http://www.boost.org/doc/libs/1_42_0/libs/regex/doc/html/index.html">boost::regex</a> (a.k.a tr1::regex) and use <a href="http://www.pcre.org/">PCRE</a> instead in <a href="http://sourceforge.net/projects/untropy/">Untropy</a>. The reason for this is, that boost::regex doesn&#8217;t support UTF-8 properly. Yes, there is <a href="http://www.boost.org/doc/libs/1_42_0/libs/regex/doc/html/boost_regex/ref/non_std_strings/icu/unicode_types.html">boost::u32regex</a> together with a few related types and functions, but as the name says, they work with UTF-32, not UTF-8. This wouldn&#8217;t be a real problem if <a href="http://source.icu-project.org/apiref/icu4c/classUnicodeString.html">UnicodeString</a> was a proper C++ string type, like <a href="http://library.gnome.org/devel/glibmm/unstable/classGlib_1_1ustring.html">Glib::ustring</a> for example. Unfortunately it isn&#8217;t, as it misses the usual typedefs and iterators; it doesn&#8217;t even define ostream operators and seems to have problems with <i>-D_GLIBCXX_DEBUG</i>. In practice this meant that working with boost::u32regex introduced quite a few explicit string conversions, that didn&#8217;t make the API very convenient at all. And no, I&#8217;m not going to use <i>UnicodeString</i> as my default string type, because this would make almost every other string related API harder to use. As I strongly prefer Perl regular expressions over other flavours, using <i>PCRE</i> was the only reasonable choice. After all, this library is widely used, feature rich, and deals with UTF-8 properly. It also has two C++ wrappers, <a href="http://man.he.net/man3/pcrecpp">pcrecpp</a> and <a href="http://www.daemon.de/PCRE">PCRE++</a>. Unfortunately they are both unusable:</p>
<ul>
<li><i>pcrecpp</i> exposes one of the most crippled C++ APIs I&#8217;ve ever seen and is poorly documented, although it is shipped with <i>PCRE</i> and has been provided by Google.</li>
<li><i>PCRE++</i> looks much much saner, but fails to properly separate compiled patterns from match results, although the underlying C API does exactly that.</li>
</ul>
<p>Being accustomed to the very well designed <a href="http://java.sun.com/javase/6/docs/api/java/util/regex/package-summary.html">java.util.regex API</a>, I was left with no other choice than to write a C++ wrapper myself. Luckily the <i>PCRE</i> C API isn&#8217;t that bad at all and well documented (although you should really spend at least one hour reading the man page carefully while experimenting with small examples, as the API is definitely neither self explanatory nor fool proof). Finally it took me about 8 hours to design, implement and test this <a href="http://untropy.svn.sourceforge.net/viewvc/untropy/trunk/src/regex.hh?view=markup&amp;pathrev=34">C++ API</a>, that is roughly modelled after <i>java.util.regex</i>, and migrate existing code to it. I&#8217;m seriously considering putting an extended and overworked version of this API in a standalone library as soon as I have time.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mlangc.wordpress.com/254/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mlangc.wordpress.com/254/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mlangc.wordpress.com/254/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mlangc.wordpress.com/254/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mlangc.wordpress.com/254/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mlangc.wordpress.com/254/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mlangc.wordpress.com/254/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mlangc.wordpress.com/254/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mlangc.wordpress.com/254/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mlangc.wordpress.com/254/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mlangc.wordpress.com/254/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mlangc.wordpress.com/254/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mlangc.wordpress.com/254/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mlangc.wordpress.com/254/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mlangc.wordpress.com&amp;blog=10679887&amp;post=254&amp;subd=mlangc&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy"></div>]]></content:encoded>
			<wfw:commentRss>http://mlangc.wordpress.com/2010/04/05/saying-goodbye-to-boostregex/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f50add19b65e82fe8dbabc438c402ed?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mlangc</media:title>
		</media:content>
	</item>
		<item>
		<title>Propagating exceptions between different threads in C++0x</title>
		<link>http://mlangc.wordpress.com/2010/03/29/propagating-exceptions-between-different-threads-in-c-0x/</link>
		<comments>http://mlangc.wordpress.com/2010/03/29/propagating-exceptions-between-different-threads-in-c-0x/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 12:22:45 +0000</pubDate>
		<dc:creator>mlangc</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[C++0x]]></category>
		<category><![CDATA[Concurrency]]></category>

		<guid isPermaLink="false">http://mlangc.wordpress.com/?p=226</guid>
		<description><![CDATA[Yesterday I finally decided to make use of all available C++0x features that are supported by gcc-4.4 in Untropy (a very specialized tool for a limited audience that is not of interest here). Whats interesting about this, is that what finally made the important difference for me is not one of the prominent features like [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mlangc.wordpress.com&amp;blog=10679887&amp;post=226&amp;subd=mlangc&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Yesterday I finally decided to make use of all available C++0x features that are <a href="http://gcc.gnu.org/gcc-4.4/cxx0x_status.html">supported by gcc-4.4</a> in <a href="http://sourceforge.net/projects/untropy/">Untropy</a> (a very specialized tool for a limited audience that is not of interest here). Whats interesting about this, is that what finally made the important difference for me is not one of the prominent features like <a href="http://msdn.microsoft.com/en-us/library/dd293668%28VS.100%29.aspx">Rvalue References</a>, <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1984.pdf">Auto Typed Variables</a> or <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2672.htm">Initializer Lists</a>. Instead it&#8217;s some seemingly unspectacular type called <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2179.html">std::exception_ptr</a> and a few related functions. This rather minimal, but very useful API has been designed to allow transporting exceptions between threads. Here is how this can be done with <a href="http://www.boost.org/doc/libs/1_42_0/doc/html/thread.html">boost::thread</a> (includes and namespaces are omitted for clarity &#8211; you can grab the full source <a href="http://untropy.svn.sourceforge.net/viewvc/untropy/trunk/src/callable.hh?view=markup&amp;pathrev=17">here</a>):</p>
<pre class="brush: cpp;">
   /**
     * A callable implementation that allows exception propagation between
     * threads for boost::tread.
     */
    class Callable
    {
        public:

        /**
         * Default copy constructor.
         *
         * @attention
         *  Should not be invoked from different threads with the same objects.
         */
        Callable(const Callable&amp;) = default;

        /**
         * Default assignment operator.
         *
         * @attention
         *  Should not be invoked from diffenrent threads with the same objects.
         */
        Callable&amp; operator=(const Callable&amp;) = default;

        /**
         * To be called by boost::thread.
         */
        void operator()() const;

        /**
         * Rethrows pending exceptions.
         */
        void rethrowIfPending() const;

        /**
         * Returns true iff an exception is pending.
         */
        bool isExceptionPending() const;

        virtual ~Callable() = default;

        protected:

        Callable();

        /**
         * Does the actual work.
         *
         * @attention
         *  Exceptions thrown from this method will be catched in operator()(),
         *  and can be rethrown by rethrowIfPending() const.
         */
        virtual void doWork() const = 0;

        private:

        struct ExceptionContainer
        {
            std::exception_ptr exceptionPtr;
        };

        std::tr1::shared_ptr&lt;boost::recursive_mutex&gt; m_exceptionMutex;
        std::tr1::shared_ptr&lt;ExceptionContainer&gt; m_exception;
    };
</pre>
<p>Note that the only reason we explicitly declared a copy constructor and an assignment operator is for the comments above them. Luckily the C++0x feature <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2346.htm">Defaulted and Deleted Functions</a> allows us to omit some boilerplate code here. But lets move to the more interesting, and less documented private part of the declaration above: Because <i>boost::thread</i> invokes a copy of our <i>Callable</i>, we cannot store pending exceptions in the class directly if we want them to be visible from outside. To solve this problem, we actually store them in a <i>tr1::shared_ptr</i> to a helper struct. Last but not least, we use a <i>boost::recursive_mutex</i> to guard the stored exception. Here is the implementation (the full source is <a href="http://untropy.svn.sourceforge.net/viewvc/untropy/trunk/src/callable.cc?view=markup&amp;pathrev=17">here</a>):</p>
<pre class="brush: cpp;">
    void Callable::operator()() const
    {
       try
       {
           doWork();
       }
       catch(...)
       {
           boost::recursive_mutex::scoped_lock lock(*m_exceptionMutex);
           m_exception-&gt;exceptionPtr = current_exception();
       }
    }

    void Callable::rethrowIfPending() const
    {
        boost::recursive_mutex::scoped_lock lock(*m_exceptionMutex);
        if(isExceptionPending())
            rethrow_exception(m_exception-&gt;exceptionPtr);
    }

    bool Callable::isExceptionPending() const
    {
        boost::recursive_mutex::scoped_lock lock(*m_exceptionMutex);
        return (exception_ptr() != m_exception-&gt;exceptionPtr);
    }

    Callable::Callable()
        : m_exceptionMutex(new boost::recursive_mutex()),
          m_exception(new ExceptionContainer)
    {

    }
</pre>
<p>Using the C++0x features <i>std::current_exception()</i> in line 10 and <i>std::rethrow_exception</i> in line 18, thats all that needs to be done. The careful reader might notice, that <i>Callable</i> isn&#8217;t entirely thread save itself: Coping or assigning <i>Callable</i> instances that share internal state from within multiple threads will most likely result in undefined behavior, because <i>tr1::shared_ptr</i> is not thread save. In theory, C++0x contains APIs for accessing <i>shared_ptr</i> instances atomically from within multiple threads, but they are not available with gcc-4.4. Of course, I could have implemented a thread save reference counter in the <i>Callable</i> class directly, but that doesn&#8217;t seem worth the effort, as manipulating identical <i>Callable</i> instances from multiple threads is an error anyway. Last but not least: <a href="http://untropy.svn.sourceforge.net/viewvc/untropy/trunk/test/t_callable.cc?view=markup&amp;pathrev=21">Here</a> is a unit test for the code above, that also illustrates how it is meant to be used.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mlangc.wordpress.com/226/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mlangc.wordpress.com/226/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mlangc.wordpress.com/226/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mlangc.wordpress.com/226/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mlangc.wordpress.com/226/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mlangc.wordpress.com/226/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mlangc.wordpress.com/226/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mlangc.wordpress.com/226/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mlangc.wordpress.com/226/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mlangc.wordpress.com/226/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mlangc.wordpress.com/226/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mlangc.wordpress.com/226/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mlangc.wordpress.com/226/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mlangc.wordpress.com/226/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mlangc.wordpress.com&amp;blog=10679887&amp;post=226&amp;subd=mlangc&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy"></div>]]></content:encoded>
			<wfw:commentRss>http://mlangc.wordpress.com/2010/03/29/propagating-exceptions-between-different-threads-in-c-0x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f50add19b65e82fe8dbabc438c402ed?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mlangc</media:title>
		</media:content>
	</item>
		<item>
		<title>What you don&#8217;t want to know about Java boxed primitives</title>
		<link>http://mlangc.wordpress.com/2010/01/09/what-you-dont-want-to-know-about-java-boxed-primitives/</link>
		<comments>http://mlangc.wordpress.com/2010/01/09/what-you-dont-want-to-know-about-java-boxed-primitives/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 15:36:02 +0000</pubDate>
		<dc:creator>mlangc</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[boxed primitives]]></category>
		<category><![CDATA[Collections]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://mlangc.wordpress.com/?p=197</guid>
		<description><![CDATA[In my last post I used a simple calculation to argument that a List containing 1024 Integer objects will take at least 8kb on a 32bit system, and at least 12kb on a 64bit system. I then also argued that additional storage for 1024 Java objects is needed (some of them might be cached, but [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mlangc.wordpress.com&amp;blog=10679887&amp;post=197&amp;subd=mlangc&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://mlangc.wordpress.com/2010/01/07/what-you-dont-want-to-know-about-java-collections/">last post</a> I used a simple calculation to argument that a <i>List</i> containing 1024 <i>Integer</i> objects will take at least 8kb on a 32bit system, and at least 12kb on a 64bit system. I then also argued that additional storage for 1024 Java objects is needed (some of them might be cached, but let&#8217;s talk about that later). Unfortunately however, I did not investigate about how much that would be until Ortwin Glück, who runs the <a href="http://www.odi.ch/prog/design/newbies.php">Java Anti-Patterns</a> page, wrote me an email with some very interesting numbers. So I started doing some research: </p>
<p>
<b>How can you reliably obtain the size of a random Java object?</b> Well, there are basically two answers: You can use a hack that is outlined <a href="http://www.javaworld.com/javaworld/javatips/jw-javatip130.html">here</a>, or you can use <a href="http://java.sun.com/javase/6/docs/api/java/lang/instrument/Instrumentation.html#getObjectSize%28java.lang.Object%29">Instrumentation.getObjectSize(Object obj)</a>. Still, obtaining an implementation of <a href="http://java.sun.com/javase/6/docs/api/java/lang/instrument/Instrumentation.html">Instrumentation</a> and transversing object graphs is far more work than I want to invest. Luckily there is <a href="http://sizeof.sourceforge.net/">java.sizeOf</a> which does exactly that for us. <i>java.sizeOf</i> basically is a Java agent and a library to access the functionality of it, packaged into a single class called <i>SizeOf</i>. To see what it can do, I wrote this small program:</p>
<pre class="brush: java;">
package com.wordpress.mlangc.sizeof;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import net.sourceforge.sizeof.SizeOf;

public class TestSizeOf
{
    private static final String WTF = &quot;:-O&quot;;
    private static final int SIZE = 1024;

    public static void main(final String[] args)
    {
        SizeOf.skipStaticField(true);

        printSize(new Object());
        printSize(Integer.valueOf(0));
        printSize(new int[SIZE]);

        List&lt;Integer&gt; intList = new ArrayList&lt;Integer&gt;(SIZE);
        for(int i = 0; i != SIZE; ++i)
            intList.add(i);
        printSize(intList);
    }

    private static void printSize(final Object obj)
    {
        System.out.println(&quot;sizeof(&quot; + describeObject(obj) + &quot;): &quot;
                + SizeOf.humanReadable(SizeOf.deepSizeOf((obj))));
    }

    private static String describeObject(final Object obj)
    {
        Class&lt;?&gt; clazz = obj.getClass();
        StringBuilder sb = new StringBuilder(clazz.getCanonicalName());
        if(clazz.isArray())
        {
            if(!sb.toString().endsWith(&quot;[]&quot;))
                throw new AssertionError(WTF);
            sb.insert(sb.length() - 1, Array.getLength(obj));
        }
        else if(obj instanceof Collection&lt;?&gt;)
        {
            Collection&lt;?&gt; collection = (Collection&lt;?&gt;) obj;
            sb.append(&quot;&lt;&quot;)
              .append(extractGenericTypeFromCollectionHack(collection))
              .append(&quot;&gt;&quot;)
              .append(&quot;[&quot;)
              .append(collection.size())
              .append(&quot;]&quot;);
        }
        return sb.toString();
    }

    private static String extractGenericTypeFromCollectionHack(final Collection&lt;?&gt; collection)
    {
        //It is impossible to do this reliably because of type erasure;
        //for our purposes however, this simple hack,
        //that just inspects the first element, should do nicely.
        if(collection.isEmpty())
            return &quot;?&quot;;
        return collection.iterator().next().getClass().getCanonicalName();
    }
}
</pre>
<p>Then I executed the code above like this (the <i>-javaagent</i> option is important):</p>
<pre class="brush: plain; gutter: false;">
$ java -javaagent:${LIB_PATH}/SizeOf.jar com.wordpress.mlangc.sizeof.TestSizeOf
JAVAGENT: call premain instrumentation for class SizeOf
sizeof(java.lang.Object): 16.0b
sizeof(java.lang.Integer): 24.0b
sizeof(int[1024]): 4.0234375Kb
sizeof(java.util.ArrayList&lt;java.lang.Integer&gt;[1024]): 32.0625Kb
</pre>
<p>So, yes a plain and mostly useless Java <i>Object</i> consumes 16bytes on my system (x86_64 with sun-jdk-1.6.0_17) &#8211; thats already 4 <i>int</i> values! An <i>Integer</i> object swallows 24bytes (I would have guessed 20 = 16 + 4 &#8211; maybe this has something to do with alignment?), which is 6 <i>int</i> values. An array of 1024 <i>int</i> values needs 4kb as it should, but an <i>ArrayList&lt;Integer&gt;</i> swallows 32kb = 24kb + 8kb and therefore consumes <b>unbelievable 8 times more memory than the array</b>.
</p>
<p>
<b>So what do I suggest considering the disturbing facts outlined above?</b></p>
<ul>
<li>
<b>Prefer primitive types to boxed primitives</b> as suggested by <a href="http://java.sun.com/docs/books/effective/">EffJava2</a> Item 49. Not only are they more efficient, the fact that they cannot be <i>null</i> makes them more attractive for other reasons too (albeit not always).
</li>
<li>
If you use wrapped primitives, <b>prefer autoboxing or the <i>Foo.valueOf(foo f)</i> factory methods</b> to direct constructors calls, as this is (citing the <a href="http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#valueOf%28int%29">Javadocs</a>) &#8220;likely to yield significantly better space and time performance by caching frequently requested values&#8221;. This is especially important for <i>Boolean</i>, where autoboxing or <i>valueOf(&#8230;)</i> never creates a new object.
</li>
<li>
Be aware of the fact, that <b><i>Collections</i> containing boxed primitives are huge memory wasters compared to arrays</b>, even when caching is considered, but keep in mind, that <b>premature optimization is the root of all evil</b>.
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mlangc.wordpress.com/197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mlangc.wordpress.com/197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mlangc.wordpress.com/197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mlangc.wordpress.com/197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mlangc.wordpress.com/197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mlangc.wordpress.com/197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mlangc.wordpress.com/197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mlangc.wordpress.com/197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mlangc.wordpress.com/197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mlangc.wordpress.com/197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mlangc.wordpress.com/197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mlangc.wordpress.com/197/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mlangc.wordpress.com/197/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mlangc.wordpress.com/197/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mlangc.wordpress.com&amp;blog=10679887&amp;post=197&amp;subd=mlangc&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy"></div>]]></content:encoded>
			<wfw:commentRss>http://mlangc.wordpress.com/2010/01/09/what-you-dont-want-to-know-about-java-boxed-primitives/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f50add19b65e82fe8dbabc438c402ed?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mlangc</media:title>
		</media:content>
	</item>
		<item>
		<title>What you don&#8217;t want to know about Java Collections</title>
		<link>http://mlangc.wordpress.com/2010/01/07/what-you-dont-want-to-know-about-java-collections/</link>
		<comments>http://mlangc.wordpress.com/2010/01/07/what-you-dont-want-to-know-about-java-collections/#comments</comments>
		<pubDate>Thu, 07 Jan 2010 21:11:53 +0000</pubDate>
		<dc:creator>mlangc</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Collections]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://mlangc.wordpress.com/?p=166</guid>
		<description><![CDATA[In one of my previous posts I wrote (I deleted &#8220;very&#8221; later, after the post was already published, as it seemed to strong): Prefer lists to arrays unless you have a very good reason not to. This was mostly in reaction to Lists are overrated, which has since been edited. Before anything else, l want [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mlangc.wordpress.com&amp;blog=10679887&amp;post=166&amp;subd=mlangc&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://mlangc.wordpress.com/2009/12/08/java-anti-patterns-revisited/">one of my previous posts</a> I wrote (I deleted &#8220;very&#8221; later, after the post was already published, as it seemed to strong):</p>
<blockquote><p>
Prefer lists to arrays unless you have a <del>very</del> good reason not to.
</p></blockquote>
<p>This was mostly in reaction to <a href="http://www.odi.ch/prog/design/newbies.php#29">Lists are overrated</a>, which has since been edited. Before anything else, l want to state clearly that I still stick to that statement. Unfortunately, due deficiencies in the Java language design and standard library APIs, some of these &#8220;good reasons&#8221; are not obvious:</p>
<p>
<b>Lists containing lots of boxed primitives are huge memory wasters:</b> This isn&#8217;t really surprising, if you just do the math. Imagine you have 1024 integers. If you store them in an int array, you should be able to get away with about 4kb of memory. But storing them in a list of boxed primitives will take at least 8kb on a 32bit system, and at least 12kb on a 64bit system, because we now also need an additional object reference for every int value we store. Last but not least, we also need an implementation dependent amount of storage for 1024 Java objects, that make matters even worse. Note that this reasoning isn&#8217;t limited to <i>Collections</i>, but to boxed primitives in general (see also Item 49 &#8220;Prefer primitive types to boxed primitives&#8221; in <a href="http://java.sun.com/docs/books/effective/">EffJava2</a>).
</p>
<p>
<b>Collections.sort(&#8230;) favors idiots over experts:</b> Let me cite the <a href="http://java.sun.com/javase/6/docs/api/java/util/Collections.html#sort%28java.util.List%29">relevant Javadocs</a>:</p>
<blockquote><p>
This implementation dumps the specified list into an array, sorts the array, and iterates over the list resetting each element from the corresponding position in the array. This avoids the n2 log(n) performance that would result from attempting to sort a linked list in place.
</p></blockquote>
<p>So, yes, the implementors of the Java standard library favor programmers who are clueless enough to sort huge linked lists, over programmers that did their homework. In fact, this simple Qicksort implementation, that sorts the given <i>List</i> in place, outperforms Collections.sort(&#8230;) by about 20% for <i>ArrayList&lt;Integer&gt;</i> and doesn&#8217;t needlessly allocate possibly huge amounts of memory:</p>
<pre class="brush: java;">
static &lt;T extends Comparable&lt;? super T&gt;&gt; void qsort(final List&lt;T&gt; list)
{
    qsort(list, 0, list.size());
}

private static &lt;T extends Comparable&lt;? super T&gt;&gt; void qsort(final List&lt;T&gt; list, final int start, final int end)
{
    // Handle simple cases:
    if(end - start &lt; 2)
        return;

    if(end - start == 2)
    {
        if(list.get(start).compareTo(list.get(end - 1)) &gt; 0)
            swap(list, start, end - 1);
        return;
   }

   // Select a new pivot:
   int i1 = start;
   int i2 =  start + (end - start)/2;
   int i3 = end - 1;
   T p1 = list.get(i1);
   T p2 = list.get(i2);
   T p3 = list.get(i3);

   int pivotInd = -1;
   if(p1.compareTo(p2) == -p1.compareTo(p3))
       pivotInd = i1;
   else if(p2.compareTo(p1) == -p2.compareTo(p3))
       pivotInd = i2;
   else
       pivotInd = i3;
   T pivot = list.get(pivotInd);

   // Partition the list using the selected pivot:
   swap(list, pivotInd, end - 1);
   int storeInd = start;
   for(int i = start; i != end - 1; ++i)
   {
       if(pivot.compareTo(list.get(i)) &gt; 0)
           swap(list, i, storeInd++);
    }
    swap(list, storeInd, end - 1);

    // Recurse:
    qsort(list, start, storeInd);
    qsort(list, storeInd + 1, end);
}

private static &lt;T&gt; void swap(final List&lt;T&gt; list, final int i1, final int i2)
{
    T tmp = list.get(i1);
    list.set(i1, list.get(i2));
    list.set(i2, tmp);
}
</pre>
<p>If you don&#8217;t believe me, try it yourself &#8211; but please don&#8217;t use this code in production without testing it thoroughly.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mlangc.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mlangc.wordpress.com/166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mlangc.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mlangc.wordpress.com/166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mlangc.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mlangc.wordpress.com/166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mlangc.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mlangc.wordpress.com/166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mlangc.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mlangc.wordpress.com/166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mlangc.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mlangc.wordpress.com/166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mlangc.wordpress.com/166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mlangc.wordpress.com/166/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mlangc.wordpress.com&amp;blog=10679887&amp;post=166&amp;subd=mlangc&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy"></div>]]></content:encoded>
			<wfw:commentRss>http://mlangc.wordpress.com/2010/01/07/what-you-dont-want-to-know-about-java-collections/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f50add19b65e82fe8dbabc438c402ed?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mlangc</media:title>
		</media:content>
	</item>
		<item>
		<title>Anti-Patterns once again</title>
		<link>http://mlangc.wordpress.com/2009/12/09/anti-patterns-once-again/</link>
		<comments>http://mlangc.wordpress.com/2009/12/09/anti-patterns-once-again/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 20:55:50 +0000</pubDate>
		<dc:creator>mlangc</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Anti-Patterns]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://mlangc.wordpress.com/?p=158</guid>
		<description><![CDATA[I just stumbled over another list of interesting Java Anti-Patterns. Luckily I agree with all of them, so unlike the last time, I can just link them here.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mlangc.wordpress.com&amp;blog=10679887&amp;post=158&amp;subd=mlangc&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I just stumbled over another list of interesting Java Anti-Patterns. Luckily I agree with all of them, so unlike <a href="http://mlangc.wordpress.com/2009/12/08/java-anti-patterns-revisited/">the last time</a>, I can just link them <a href="http://javaantipatterns.wordpress.com/">here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/mlangc.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/mlangc.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/mlangc.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/mlangc.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/mlangc.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/mlangc.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/mlangc.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/mlangc.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/mlangc.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/mlangc.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/mlangc.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/mlangc.wordpress.com/158/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/mlangc.wordpress.com/158/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/mlangc.wordpress.com/158/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=mlangc.wordpress.com&amp;blog=10679887&amp;post=158&amp;subd=mlangc&amp;ref=&amp;feed=1" width="1" height="1" /><div class="sharedaddy"></div>]]></content:encoded>
			<wfw:commentRss>http://mlangc.wordpress.com/2009/12/09/anti-patterns-once-again/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/9f50add19b65e82fe8dbabc438c402ed?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mlangc</media:title>
		</media:content>
	</item>
	</channel>
</rss>
