<?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>Rethrick Construction</title>
	<atom:link href="http://rethrick.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://rethrick.wordpress.com</link>
	<description>stuff</description>
	<lastBuildDate>Sat, 27 Sep 2008 02:27:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='rethrick.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Rethrick Construction</title>
		<link>http://rethrick.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://rethrick.wordpress.com/osd.xml" title="Rethrick Construction" />
	<atom:link rel='hub' href='http://rethrick.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Most web frameworks are broken</title>
		<link>http://rethrick.wordpress.com/2008/07/13/most-web-frameworks-are-broken/</link>
		<comments>http://rethrick.wordpress.com/2008/07/13/most-web-frameworks-are-broken/#comments</comments>
		<pubDate>Sun, 13 Jul 2008 09:23:04 +0000</pubDate>
		<dc:creator>rethrick</dc:creator>
				<category><![CDATA[1]]></category>
		<category><![CDATA[guice]]></category>
		<category><![CDATA[type system]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[type theory]]></category>
		<category><![CDATA[warp-widgets]]></category>

		<guid isPermaLink="false">http://rethrick.wordpress.com/?p=11</guid>
		<description><![CDATA[We&#8217;ve all been there: fired up the next set of changes in a webapp and navigated through a complex flow, entering fake data for the n-th time only to find a massive exception on the page we&#8217;ve been expectantly working on, due to a simple typo: No such property: ${person.chidl} Apart from the enormous frustration, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rethrick.wordpress.com&amp;blog=3241505&amp;post=11&amp;subd=rethrick&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve all been there: fired up the next set of changes in a webapp and navigated through a complex flow, entering fake data for the n-th time only to find a massive exception on the page we&#8217;ve been expectantly working on, due to a simple typo:</p>
<pre>No such property: ${person.<strong>chidl</strong>}</pre>
<p>Apart from the enormous frustration, the problem with this approach is that it is very fragile. If you fix the one error, then go through the entire process again there are no guarantees you won&#8217;t end up with another further down the page:</p>
<pre>No such property: ${person.<strong>siblign</strong>}</pre>
<p>This happens all the time, and most programmers seem to accept it as an awful fact of life with web frameworks today. You might say, that&#8217;s OK&#8211;I&#8217;ll trade this any day for the madness that is JSP and scriptlets. Except, there&#8217;s a whole new madness seething underneath.</p>
<h2>Broken encapsulation</h2>
<p>Consider this expression:</p>
<pre>Hello, ${person.firstName} ${person.lastName}.</pre>
<p>And now consider the class behind it:</p>
<pre>public abstract class Person {
   public abstract String getName();
}</pre>
<p>What&#8217;s going on?! The property should be <strong>${person.name}</strong> yet it works with these two non-existent properties. What happened was: someone inadvertently exposed properties of a subtype (first + last names) that were never meant to be known. Tightly coupling presentation code to the internals of your app.</p>
<p>This is allowed by most web frameworks because they use a dynamically typed expression language that invokes property getters via reflection. This problem also gets worse.</p>
<h2>Expression depression</h2>
<p>Imagine how horrible this could get. Some libraries allow method calls and computations:</p>
<pre>Total is ${cost + tax.computeGst(cost, 10)}</pre>
<p>Does method <em>computeGst()</em> exist? Does it take two integers? Does it return the same type as <strong>cost</strong> (double), so they can be multiplied? &lt;Insert_web_framework_here&gt; does not care. As far as it&#8217;s concerned, you can <a title="Breakfast machine" href="http://www.youtube.com/watch?v=7-Ps_mOvzrw" target="_blank">follow the Rube Goldberg</a> and find out when things blow up.</p>
<h2>Weak typing</h2>
<p>Can it possibly get worse? Indeed. Let&#8217;s dissect the GST example further:</p>
<pre>Total is ${cost + tax.computeGst(cost, 10)}</pre>
<p>If <strong>cost</strong> is a String that holds <strong>&#8220;24.50&#8243;</strong>, and the <em>computeGst()</em> method returned a double, it is very possible that your web framework will still compute the result! This is because many of them perform implicit type conversion:</p>
<pre>"3" + 2 =&gt; 5</pre>
<p>If you are not a Visual Basic programmer, this should make you shudder.</p>
<h2>Warp-widgets</h2>
<p><a title="Mike Brock, MVEL author" href="http://twitter.com/brockm" target="_blank">Mike Brock</a> (from <a title="MVEL" href="http://mvel.codehaus.org" target="_blank">MVEL</a>) has been helping me add static typing to <a title="warp-widgets" href="http://code.google.com/p/warp-core" target="_blank">warp-widgets</a>. The latest build (on my laptop) now statically type checks every expression in the template and reports errors in a manner very similar to javac:</p>
<pre>....warp.widgets.<strong>TemplateCompileException</strong>: Could not compile template for..

1) unknown or unresolvable property: chidl

 21:      &lt;ul&gt;
 22:          &lt;li&gt;${person.<strong><span style="color:#ff0000;">chidl</span></strong>}&lt;/li&gt;
                           ^ </pre>
<p>Note that every expression on a page is statically type-checked and verified even if errors are found earlier. Thus eliminating all four of the problems I described earlier.</p>
<p>A big shout out to Mike for his responsiveness in adding obscure features that I request.</p>
<p>Another shout out to &#8220;Crazy&#8221; <a title="Bob Lee" href="http://crazybob.org" target="_blank">Bob Lee</a> for pushing me to do this in the first place.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rethrick.wordpress.com/11/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rethrick.wordpress.com/11/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rethrick.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rethrick.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rethrick.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rethrick.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rethrick.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rethrick.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rethrick.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rethrick.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rethrick.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rethrick.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rethrick.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rethrick.wordpress.com/11/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rethrick.wordpress.com/11/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rethrick.wordpress.com/11/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rethrick.wordpress.com&amp;blog=3241505&amp;post=11&amp;subd=rethrick&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rethrick.wordpress.com/2008/07/13/most-web-frameworks-are-broken/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a620dbcc8718d730f9955d7932beee6b?s=96&#38;d=identicon" medium="image">
			<media:title type="html">rethrick</media:title>
		</media:content>
	</item>
		<item>
		<title>Static typing for warp-widgets</title>
		<link>http://rethrick.wordpress.com/2008/06/08/static-typing-for-warp-widgets/</link>
		<comments>http://rethrick.wordpress.com/2008/06/08/static-typing-for-warp-widgets/#comments</comments>
		<pubDate>Sun, 08 Jun 2008 07:41:20 +0000</pubDate>
		<dc:creator>rethrick</dc:creator>
				<category><![CDATA[1]]></category>
		<category><![CDATA[guice]]></category>
		<category><![CDATA[type system]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[type theory]]></category>

		<guid isPermaLink="false">http://rethrick.wordpress.com/?p=8</guid>
		<description><![CDATA[I have been thinking about this for awhile, currently warp-widgets templates are duck typed. I want to make them statically typed (as crazybob encouraged me to). There are two parts to this. Part #1 I had intended the widget annotations to be java annotations, which can be used to configure user-widgets dynamically&#8230;in the template: @My(name="Jeff") &#60;div&#62;... [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rethrick.wordpress.com&amp;blog=3241505&amp;post=8&amp;subd=rethrick&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have been thinking about this for awhile, currently <a title="A ridiculously simple, restful statically typed web framework" href="http://code.google.com/p/warp-core" target="_blank">warp-widgets</a> templates are duck typed. I want to make them statically typed (as <a title="Crazy Bob Lee" href="http://crazybob.org" target="_blank">crazybob</a> encouraged me to). There are two parts to this.</p>
<h3>Part #1</h3>
<div>I had intended the widget annotations to be java annotations, which can be used to configure user-widgets dynamically&#8230;in the template:</div>
<div></div>
<pre>@My(name="Jeff")
&lt;div&gt;... &lt;/div&gt;</pre>
<div>&#8230;and in the class:</div>
<div></div>
<pre>@EmbedAs(My.class) 
public class MyWidget {

    @Inject
    public MyWidget(My my) {
         assert "Jeff".equals(my.name());    //true
    }
}</pre>
<div>This part is fairly simple. </div>
<h3>Part #2</h3>
<div>The second part is making expressions statically typed, which is a bit harder. Right now we use <a title="MVEL" href="http://mvel.codehaus.org">MVEL</a> as the expression evaluator, so:</div>
<pre>${message}</pre>
<div>&#8230;reads a property from getMessage(). But this is <a title="Duck Typing" href="http://en.wikipedia.org/wiki/Duck_typing">duck typed</a>, meaning the class owning getMessage() can be anything. And because MVEL is <a title="Dynamic Typing" href="http://en.wikipedia.org/wiki/Type_system">dynamically typed</a>, we cannot tell if the expression is wrong (i.e. does property &#8220;message&#8221; exist?) until it dies gloriously at runtime.</div>
<div>What are our options so that we can gain static typing guarantees? I can think of:</div>
<div>
<ol>
<li>writing a specialized statically typed expression language for warp-widgets (would be nice to avoid this!)</li>
<li>check that the properties/paths exist on classes at template compile time (static duck typing)</li>
<li>another, harder, option is to perform <em>expression egress type narrowing</em> at template compile time. Which will give us &#8216;effectively static&#8217; typing (but under the hood we allow MVEL to evaluate the expressions, dynamically)</li>
</ol>
</div>
<div>I can&#8217;t seem to find an easily embedded expression language for Java. The ones that do have some static typing (MVEL), use <a title="Type Inference (static typing without type annotations)" href="http://en.wikipedia.org/wiki/Type_inference">type annotations</a> which are clunky and easy to subvert. </div>
<div>Any thoughts or ideas? Throw them at me: <strong>dhanji at gmail com</strong> or <strong>twitter.com/dhanji</strong></div>
<p> </p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rethrick.wordpress.com/8/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rethrick.wordpress.com/8/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rethrick.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rethrick.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rethrick.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rethrick.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rethrick.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rethrick.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rethrick.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rethrick.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rethrick.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rethrick.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rethrick.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rethrick.wordpress.com/8/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rethrick.wordpress.com/8/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rethrick.wordpress.com/8/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rethrick.wordpress.com&amp;blog=3241505&amp;post=8&amp;subd=rethrick&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rethrick.wordpress.com/2008/06/08/static-typing-for-warp-widgets/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a620dbcc8718d730f9955d7932beee6b?s=96&#38;d=identicon" medium="image">
			<media:title type="html">rethrick</media:title>
		</media:content>
	</item>
		<item>
		<title>Clearing up misconceptions about Generics</title>
		<link>http://rethrick.wordpress.com/2008/04/30/generics/</link>
		<comments>http://rethrick.wordpress.com/2008/04/30/generics/#comments</comments>
		<pubDate>Wed, 30 Apr 2008 14:53:32 +0000</pubDate>
		<dc:creator>rethrick</dc:creator>
				<category><![CDATA[1]]></category>
		<category><![CDATA[closures]]></category>
		<category><![CDATA[guice]]></category>
		<category><![CDATA[type system]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[type theory]]></category>

		<guid isPermaLink="false">http://rethrick.wordpress.com/?p=7</guid>
		<description><![CDATA[Generics are at once the most welcome, easy to understand and most perplexing feature added to Java since&#8230; well ever! I couldn&#8217;t imagine life without them, but like anything in type theory they are at times confoundingly opaque. Here are some all-too-common misconceptions about Java Generics and Generic types in general: Set&#60;Child&#62; is a subtype [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rethrick.wordpress.com&amp;blog=3241505&amp;post=7&amp;subd=rethrick&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Generics are at once the most welcome, easy to understand and most perplexing feature added to Java since&#8230; well ever! I couldn&#8217;t imagine life without them, but like anything in type theory they are at times confoundingly opaque. Here are some all-too-common misconceptions about Java Generics and Generic types in general:</p>
<ul>
<li><span style="line-height:26px;">Set&lt;Child&gt; is a subtype of Set&lt;Parent&gt;</span></li>
</ul>
<p>This is an unfortunate misapprehension brought on by the rather strange fact that Child[] is a subtype of Parent[].</p>
<ul>
<li><span style="line-height:26px;">Type erasure means there is no real difference between Set&lt;T&gt;, Set or Set&lt;E&gt;</span></li>
</ul>
<p>Type erasure means there is no way to <em><strong>enforce</strong></em> the difference between them at runtime.</p>
<ul>
<li><span style="line-height:26px;">Set&lt;?&gt; is equal to Set&lt;?&gt;</span></li>
</ul>
<p>This one came up in the <a title="Google Guice" href="http://code.google.com/p/google-guice" target="_blank">Guice</a> list recently: why can&#8217;t we declare a variable as Set&lt;?&gt; and then bind it to some implementation that Set&lt;?&gt; accepts?</p>
<p>The answer is that Set&lt;?&gt; is a <em>wildcard</em> type. In other words, it is a <em><strong>query</strong></em> on all types that match Set&lt;?&gt;. Like Set&lt;Object&gt;, Set&lt;Socket&gt;, Set&lt;BuckRogers&gt;, and everything else besides. </p>
<p>Java cannot close over the scope of all possible bindings to any give Set&lt;?&gt;. In other words, because Set&lt;?&gt; can be <strong><em>any</em></strong> Set type, Java cannot assume that it is <strong><em>any </em></strong><strong><em>one</em></strong> Set type. Therefore you cannot bind Set&lt;?&gt; to any particular Set.</p>
<ul>
<li><span style="line-height:26px;">The same goes for Set&lt;? extends Thing&gt;</span></li>
</ul>
<p>This is a special kind of wildcard known as a <strong><em>bounded wildcard</em></strong>. In essence, a wildcard is a closure over the global scope of the program. If such a closure were possible, we could bind wildcards correctly. But this is not doable without the source code to everything (aka global compilation =)</p>
<ul>
<li><span style="line-height:26px;">Type reification would have let us do cool things like <strong>new T();</strong></span></li>
</ul>
<p>No, it <a title="reification is not magic" href="http://blogs.sun.com/abuckley/en_US/entry/new_t_is_not_possible" target="_blank">would not</a>.</p>
<ul>
<li><span style="line-height:26px;">Guice can @Inject List&lt;T&gt; if I bind all possible List&lt;<span style="line-height:26px;">T&gt;&#8217;s used in my program</span></span></li>
</ul>
<p>Type erasure means that Guice has no idea what the witness of T was, when bound. </p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rethrick.wordpress.com/7/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rethrick.wordpress.com/7/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rethrick.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rethrick.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rethrick.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rethrick.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rethrick.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rethrick.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rethrick.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rethrick.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rethrick.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rethrick.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rethrick.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rethrick.wordpress.com/7/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rethrick.wordpress.com/7/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rethrick.wordpress.com/7/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rethrick.wordpress.com&amp;blog=3241505&amp;post=7&amp;subd=rethrick&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rethrick.wordpress.com/2008/04/30/generics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a620dbcc8718d730f9955d7932beee6b?s=96&#38;d=identicon" medium="image">
			<media:title type="html">rethrick</media:title>
		</media:content>
	</item>
		<item>
		<title>A lock-free, wait-free, counting table</title>
		<link>http://rethrick.wordpress.com/2008/04/06/counting-table/</link>
		<comments>http://rethrick.wordpress.com/2008/04/06/counting-table/#comments</comments>
		<pubDate>Sun, 06 Apr 2008 06:48:35 +0000</pubDate>
		<dc:creator>rethrick</dc:creator>
				<category><![CDATA[1]]></category>
		<category><![CDATA[concurrent]]></category>
		<category><![CDATA[counting]]></category>
		<category><![CDATA[data structure]]></category>
		<category><![CDATA[hashtable]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[lock-free]]></category>
		<category><![CDATA[threading]]></category>
		<category><![CDATA[wait-free]]></category>

		<guid isPermaLink="false">http://rethrick.wordpress.com/?p=5</guid>
		<description><![CDATA[When consulting with a colleague, recently, I ran into an interesting use case: in order to monitor live performance, we needed to count the number of method invocations on a service (read: EJB/remoted call). Actually, it was a number of such services. The obvious answer was to place an interceptor around each method call, and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rethrick.wordpress.com&amp;blog=3241505&amp;post=5&amp;subd=rethrick&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>When consulting with a colleague, recently, I ran into an interesting use case: in order to monitor live performance, we needed to count the number of method invocations on a service (read: EJB/remoted call). Actually, it was a number of such services. The obvious answer was to place an interceptor around each method call, and increment a global counter. </p>
<p>However, here&#8217;s the kicker: we don&#8217;t know how many services there are, and the system is <em>highly</em> concurrent. What would you use to store service/call count mappings?</p>
<ul>
<li><strong>j.u.HashMap</strong> &#8211; Can&#8217;t, it&#8217;s not thread-safe</li>
<li><strong>Hashtable/Collections.synchronizedMap()</strong> &#8211; Nope, there&#8217;s a single lock&#8211;all invocations will queue up behind one another and kill performance</li>
<li><strong>j.u.c.ConcurrentHashMap</strong> &#8211; Even with a mighty (1:1) lock granularity it&#8217;s no good because a single EJB can be invoked concurrently. If there&#8217;s a popular EJB you end up with the one-giant-lock problem all over again</li>
</ul>
<p>So that&#8217;s exhausted all obvious solutions. What we needed was a table where:</p>
<ol>
<li>a key could be inserted atomically, and </li>
<li>its counter incremented atomically </li>
</ol>
<p>&#8230;but these two operations are not atomic (together). Here&#8217;s roughly what we came up with (credit should mostly go to my colleagues):</p>
<pre>public class CountingTable&lt;K&gt; {
    private final AtomicReferenceArray&lt;K&gt; keys = new AtomicReferenceArray(CAPACITY);
    private final AtomicIntegerArray counters = new AtomicIntegerArray(CAPACITY);

    public void count(K key) {
        int index = key.hashCode() &amp; (keys.length() - 1);

        while(key != keys.get(index) &amp;&amp; !keys.compareAndSet(index, null, key) 
                                     &amp;&amp; key != keys.get(index))
            if (index &lt; keys.length() -1)
                index++;
            else index = 0;  //wrap

        counters.incrementAndGet(index);
    }
}</pre>
<p>What we end up with is a <em>constant-time, fixed-size, lock-free, wait-free, open-addressing </em>hashtable<em> </em>with<em> linear reprobing</em>. =)</p>
<p>How does it work?</p>
<ul>
<li>First, we hash into the array somewhere (sparse-array trick); I use <strong>&amp;</strong> as it&#8217;s much faster than <strong>%</strong></li>
<li>Next, we test if the key is already there (if it is, drop out, increment and done)</li>
<li>If not, we try to insert the key using CAS against null (if slot is empty, insert key, drop out, increment and done)</li>
<li>If not, we recheck to ensure an interleaving thread didn&#8217;t insert our key (if it did, drop out, inc &amp; done)</li>
<li>If not, we move to the next slot in the array (<em>linear reprobing</em>); rinse, lather and repeat.</li>
</ul>
<p>Does it really work?</p>
<ul>
<li><strong>constant-time complexity</strong> &#8211; by hashing into an array, I guarantee <strong>O(1)</strong> puts in the average case</li>
<li><strong>lock-free</strong> &#8211; there is absolutely no mutex over the table, a stripe or even a single key!</li>
<li><strong>wait-free</strong> &#8211; during a single put, multiple threads can make make progress (even to the same key)</li>
<li><strong>linear-reprobing</strong> &#8211; by <a href="http://en.wikipedia.org/wiki/Linear_probing">scanning forward</a> into the array on collisions, we maintain constant time-complexity (degrades to linear in the worst case, i.e. if all keys collide)</li>
<li><strong>open-addressing</strong> &#8211; by storing keys and values in the array, we can vastly improve CPU cache performance over a traditional <a href="http://en.wikipedia.org/wiki/Hash_table#Open_addressing_versus_chaining" target="_blank">chaining hashtable</a> like j.u.HashMap and j.u.c.ConcurrentHashMap in colliding cases</li>
</ul>
<p>Send me your thoughts, I suspect this can be improved a lot. (And if you want to see another post on <em>reading </em>from this table =)</p>
<p>With due props to <a href="http://blogs.azulsystems.com/cliff/">Cliff Click</a>&#8216;s <a href="http://www.google.com/search?hl=en&amp;client=safari&amp;rls=en-us&amp;q=cliff+click+lock+free&amp;btnG=Search">ideas on hashtables</a>.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rethrick.wordpress.com/5/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rethrick.wordpress.com/5/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rethrick.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rethrick.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rethrick.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rethrick.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rethrick.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rethrick.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rethrick.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rethrick.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rethrick.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rethrick.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rethrick.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rethrick.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rethrick.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rethrick.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rethrick.wordpress.com&amp;blog=3241505&amp;post=5&amp;subd=rethrick&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rethrick.wordpress.com/2008/04/06/counting-table/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a620dbcc8718d730f9955d7932beee6b?s=96&#38;d=identicon" medium="image">
			<media:title type="html">rethrick</media:title>
		</media:content>
	</item>
		<item>
		<title>Thoughts on CICE (closures?)</title>
		<link>http://rethrick.wordpress.com/2008/03/22/hello-world/</link>
		<comments>http://rethrick.wordpress.com/2008/03/22/hello-world/#comments</comments>
		<pubDate>Sat, 22 Mar 2008 11:22:13 +0000</pubDate>
		<dc:creator>rethrick</dc:creator>
				<category><![CDATA[1]]></category>
		<category><![CDATA[closures]]></category>
		<category><![CDATA[guice]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[lambda]]></category>
		<category><![CDATA[scheme]]></category>
		<category><![CDATA[type system]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[CICE is a proposal by Doug Lea, Josh Bloch and &#8216;Crazy&#8217; Bob Lee. It is being touted as a &#8220;closures&#8221; proposal for Java 7 alongside Neal Gafter&#8217;s BGGA (for instance). Here are some thoughts I had after reading their document: So basically, CICE is syntactic sugar for anonymous classes. In Guice parlance, the following: bind(Service.class).toProvider(new Provider&#60;Service&#62;() { public Service get() [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rethrick.wordpress.com&amp;blog=3241505&amp;post=1&amp;subd=rethrick&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-family:Verdana;font-size:14px;line-height:normal;" class="Apple-style-span">
<div style="background-image:initial;background-repeat:initial;background-attachment:initial;background-color:#ffffff;">
<div style="padding-left:0;" class="wrapper">
<div class="content-item ">
<div>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;">CICE is a proposal by Doug Lea, Josh Bloch and &#8216;Crazy&#8217; Bob Lee. It is being touted as a &#8220;closures&#8221; proposal for Java 7 alongside <a href="http://www.javac.info/">Neal Gafter&#8217;s BGGA</a> (for instance). Here are some thoughts I had after reading <a href="http://docs.google.com/View?docid=k73_1ggr36h">their document</a>:</p>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;">So basically, CICE is syntactic sugar for anonymous classes. In <a href="http://code.google.com/p/google-guice">Guice</a> parlance, the following:</p>
<pre style="font-size:1em;margin:0;padding:0;">bind(Service.class).toProvider(new Provider&lt;Service&gt;() {</pre>
<pre style="font-size:1em;margin:0;padding:0;">    public Service get() {</pre>
<pre style="font-size:1em;margin:0;padding:0;">        return new ServiceImpl(); </pre>
<pre style="font-size:1em;margin:0;padding:0;">    } </pre>
<pre style="font-size:1em;margin:0;padding:0;">}); </pre>
<pre style="font-size:1em;margin:0;padding:0;"> </pre>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;">&#8230;becomes: </p>
<pre style="font-size:1em;margin:0;padding:0;"> </pre>
<pre style="font-size:1em;margin:0;padding:0;">bind(Service.class).toProvider(Provider&lt;Service&gt;()</pre>
<pre style="font-size:1em;margin:0;padding:0;">                   { return new ServiceImpl; }); </pre>
<pre style="font-size:1em;margin:0;padding:0;"> </pre>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;"><a href="http://www.schemers.org/">Scheme</a> is one of my favorite languages. Scheme basically <a href="http://gafter.blogspot.com/2007/01/definition-of-closures.html">invented closures</a>, so given this&#8211;what are my reactions to CICE?</p>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;">First, this is what I call a closure: A block of statements which retains its lexical context indefinitely. A closure is also a first-class citizen, in that it can be stored in a variable and passed around like an object .</p>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;"><b>I</b><b>n CICE, you don&#8217;t implictly <i>close </i>over the wrapping lexical context</b></p>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;">Uh, sure but what does that mean? It means local variables are not part of the closure&#8217;s immediate scope. Even though the <i>appear </i>to be:</p>
<pre style="font-size:1em;margin:0;padding:0;">public Runnable get() {</pre>
<pre style="font-size:1em;margin:0;padding:0;">    int x = 1;</pre>
<pre style="font-size:1em;margin:0;padding:0;">    Runnable r = Runnable() { System.out.println(x); };</pre>
<pre style="font-size:1em;margin:0;padding:0;">    x = 2;</pre>
<pre style="font-size:1em;margin:0;padding:0;">    return r; </pre>
<pre style="font-size:1em;margin:0;padding:0;">} </pre>
<pre style="font-size:1em;margin:0;padding:0;"> </pre>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;">&#8230; won&#8217;t work. r only sees x = 1. Local variables are actually <i>copied </i>to the block. By hiding the final keyword it fools you into believing this is not the case. The fix for this is to declare x as public:</p>
<pre style="font-size:1em;margin:0;padding:0;">public Runnable get() {</pre>
<pre style="font-size:1em;margin:0;padding:0;">    <b>public </b>int x = 1; <b>//huh?</b></pre>
<pre style="font-size:1em;margin:0;padding:0;"> </pre>
<pre style="font-size:1em;margin:0;padding:0;"><b></b>    Runnable r = Runnable()</pre>
<pre style="font-size:1em;margin:0;padding:0;">             { System.out.println(x); };</pre>
<pre style="font-size:1em;margin:0;padding:0;">    x = 2;</pre>
<pre style="font-size:1em;margin:0;padding:0;">    return r;</pre>
<pre style="font-size:1em;margin:0;padding:0;">} </pre>
<pre style="font-size:1em;margin:0;padding:0;"> </pre>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;">I thought closures are supposed to prevent such malarkey, not induce it =P</p>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;">Note: The BGGA proposal also <a href="http://tronicek.blogspot.com/2008/02/version-2008-02-22.html">added</a> something like this not long ago, but it was an optional<i>documenting </i>annotation <span style="font-family:'courier new', monospace;">@Shared</span>, rather than a language construct (i.e. <span style="font-family:'courier new', monospace;">public </span>keyword).</p>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;"> <b>Statement blocks are not anonymous</b></p>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;">Blocks in almost every language thus far are anonymous (Ruby, Groovy, Python, etc.). A block in CICE is just a contraction of an instance creation step, so you still need to specify its type. The following (anonymous method) is not possible:</p>
<pre style="font-size:1em;margin:0;padding:0;">list.each(<b>{ it -&gt; doStuffTo(it); }</b>);</pre>
<pre style="font-size:1em;margin:0;padding:0;"> </pre>
<pre style="font-size:1em;margin:0;padding:0;"> </pre>
<pre style="font-size:1em;margin:0;padding:0;"> </pre>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;"><b>Statement blocks are <i>too </i>type-restrictive </b></p>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;">By the same token, statement blocks are restricted to pre-existing types (i.e. single-method interfaces being subclassed). One powerful feature of closures in Scheme (and other dynamic languages) is they are type-neutral at the <i>point of definition</i>. This may seem cribby to you but:</p>
<pre style="font-size:1em;margin:0;padding:0;">(define add (lambda (<b>x y</b>) (+ x y)))</pre>
<pre style="font-size:1em;margin:0;padding:0;"> </pre>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;">&#8230;can add two integers, longs, or floats. The function in <i>add</i> is type-neutral. This is *not* to be confused with duck-typing (Scheme is <i>not </i>duck-typed). There is a subtle point here that all functions in Scheme are type-neutral until they execute, but this is not what I am getting at (ignore that Scheme is dynamically typed for the moment). I concede that it is harder to type-check inferred-type methods in a statically-typed language like Java, but CICE makes this <i>impossible </i>without pre-defined generic types:</p>
<pre style="font-size:1em;margin:0;padding:0;">public interface Adder&lt;N&gt; {</pre>
<pre style="font-size:1em;margin:0;padding:0;">    N add(N x, N y); </pre>
<pre style="font-size:1em;margin:0;padding:0;">}</pre>
<pre style="font-size:1em;margin:0;padding:0;"> </pre>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;">I must declare this type simply in order to use it anonymously. This feels backwards to me. By contrast, this is a <a href="http://www.haskell.org/">Haskell</a> function:</p>
<p style="font-family:'courier new', monospace;font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;">add x y = x + y</p>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;">Does <span style="font-family:'courier new', monospace;">add()</span> take Ints? Longs? Floats? We don&#8217;t know until it is used. And we don&#8217;t care. Haskell functions are quantified over all types. In other words, they are <i>type-polymorphic</i>. Haskell is statically-typed, so this is not an apples-to-oranges comparison either (if you were wondering that it was with Scheme and CICE). The CICE proposal claims this is something that could be improved upon.</p>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;"><b>More on type-neutrality</b></p>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;">OK, so you sort of grok that a closure is different from a CICE in that you don&#8217;t subclass a pre-defined type. But what&#8217;s so bad about that? One-method types are easy to create (and most useful ones already exist&#8211;example <span style="font-family:'courier new', monospace;">Callable&lt;T&gt;</span> and <span style="font-family:'courier new', monospace;">Future&lt;T&gt;</span>). Without resorting to duck-typing what else could I possibly think type-neutrality provides? &#8220;Real&#8221; closures possess function types. This essentially boils down to a tuple consisting of [R, A...An] where R is a return type and A&#8230;An are argument types. </p>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;">So what? Well&#8211;this actually means closures are quantifiable over all types matching the tuple (ANY matching function) and not simply a <i>named</i> type. Consider:</p>
<pre style="font-size:1em;margin:0;padding:0;">public java.lang.Runnable get() {</pre>
<pre style="font-size:1em;margin:0;padding:0;">    return <b>my.domain.Runnable</b>()</pre>
<pre style="font-size:1em;margin:0;padding:0;">        { System.out.println("Hi"); };  <b>//error</b></pre>
<pre style="font-size:1em;margin:0;padding:0;">}</pre>
<pre style="font-size:1em;margin:0;padding:0;"> </pre>
<pre style="font-size:1em;margin:0;padding:0;"></pre>
<pre style="font-size:1em;margin:0;padding:0;"></pre>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;">As opposed to a real closure:</p>
<pre style="font-size:1em;margin:0;padding:0;">package some.other.domain;</pre>
<pre style="font-size:1em;margin:0;padding:0;">...</pre>
<pre style="font-size:1em;margin:0;padding:0;"></pre>
<pre style="font-size:1em;margin:0;padding:0;">public { =&gt; void } get(boolean where) {</pre>
<pre style="font-size:1em;margin:0;padding:0;">    return where ? { =&gt; System.out.println("Hi"); }</pre>
<pre style="font-size:1em;margin:0;padding:0;">                 : <b>my.domain.Closures</b>.bye() ;</pre>
<pre style="font-size:1em;margin:0;padding:0;">}</pre>
<pre style="font-size:1em;margin:0;padding:0;"></pre>
<pre style="font-size:1em;margin:0;padding:0;"></pre>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;">Named types don&#8217;t enter into it. The closure&#8217;s type is captured over its functional type signature, which effectively gives it the flexibility of <a href="http://beust.com/weblog/archives/000476.html">structure typing</a> (but without contaminating the <i>structural</i>type system itself&#8211;unlike scala). Much more powerful indeed.</p>
<pre style="font-size:1em;margin:0;padding:0;"></pre>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;"><b>Other stuff</b></p>
<ul>
<li> <span style="font-family:'courier new', monospace;">this </span>keyword refers to the inner type and must be qualified to refer to the enclosing scope</li>
<li> Return types must be specific to the type being subclassed in the CICE. You can&#8217;t simply return a value from a block of code.</li>
<li>Same with checked exceptions thrown (there are workarounds with generic throws clauses)</li>
<li>Block-arguments also must match the signature of the interface type. Covariant argument typing is not possible (again messy workarounds are possible with generics)</li>
</ul>
<pre style="font-size:1em;margin:0;padding:0;"> </pre>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;">For these reasons I believe it is misleading to call CICE a &#8220;closures proposal&#8221;.  It should certainly not be considered in the same vein as BGGA for instance. Note that none of this speaks to the merits of CICE as a code footprint reduction proposal. Nor to BGGA in terms of its merit as a closures proposal. </p>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;">I am not endorsing either proposal, nor condemning CICE as a language feature. I just want to highlight that it is inaccurate to equate CICEs to closures.</p>
<p style="font-size:1em;color:#000000;line-height:1.75em;margin:0 0 1.5em;padding:0;"><a href="http://groups.google.com/group/warp-core">Email me your thoughts</a>.</p>
</div>
</div>
<div style="clear:both;"></div>
</div>
</div>
<div style="clear:both;border-top-width:1px;border-top-style:solid;border-top-color:#cccccc;margin-top:2em;">
<div class="wrapper"></div>
</div>
<p></span> </p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/rethrick.wordpress.com/1/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/rethrick.wordpress.com/1/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rethrick.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/rethrick.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/rethrick.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/rethrick.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/rethrick.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/rethrick.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/rethrick.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/rethrick.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/rethrick.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/rethrick.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/rethrick.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/rethrick.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/rethrick.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/rethrick.wordpress.com/1/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=rethrick.wordpress.com&amp;blog=3241505&amp;post=1&amp;subd=rethrick&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://rethrick.wordpress.com/2008/03/22/hello-world/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a620dbcc8718d730f9955d7932beee6b?s=96&#38;d=identicon" medium="image">
			<media:title type="html">rethrick</media:title>
		</media:content>
	</item>
	</channel>
</rss>
