<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.0.2" -->
<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/"
	>

<channel>
	<title>Pure Danger Tech</title>
	<link>http://tech.puredanger.com</link>
	<description>Alex Miller's technical blog</description>
	<pubDate>Mon, 06 Jul 2009 04:55:08 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.2</generator>
	<language>en</language>
			<item>
		<title>Lambda Lounge Fortress/Fan videos</title>
		<link>http://tech.puredanger.com/2009/07/05/lambda-july-videos/</link>
		<comments>http://tech.puredanger.com/2009/07/05/lambda-july-videos/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 00:32:17 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
		
	<category>languages</category>
	<category>st. louis</category>
	<category>Lambda Lounge</category>
	<category>Fortress</category>
	<category>Fan</category>
		<guid isPermaLink="false">http://tech.puredanger.com/2009/07/05/lambda-july-videos/</guid>
		<description><![CDATA[This past Thursday we had our monthly Lambda Lounge meeting.  If you&#8217;re interested, you can find info from my talk on the Sun research language Project Fortress here:

Slides
Video

And you can find info from Scott Bale&#8217;s talk on Fan here:

Slides
Video

If you&#8217;re interested in catching future video, you can catch the feed from blip.tv or we&#8217;re [...]]]></description>
			<content:encoded><![CDATA[<p>This past Thursday we had our monthly <a href="http://lambdalounge.org">Lambda Lounge</a> meeting.  If you&#8217;re interested, you can find info from my talk on the Sun research language <a href="http://projectfortress.sun.com/Projects/Community">Project Fortress</a> here:</p>
<ul>
<li><a href="http://www.slideshare.net/alexmiller/project-fortress">Slides</a></li>
<li><a href="http://lambdalounge.blip.tv/file/2318347/">Video</a></li>
</ul>
<p>And you can find info from <a href="http://puredanger.com/kablooie/">Scott Bale</a>&#8217;s talk on <a href="http://fandev.org/">Fan</a> here:</p>
<ul>
<li><a href="http://stllambdalounge.files.wordpress.com/2009/07/fanlambdalounge.pdf">Slides</a></li>
<li><a href="http://lambdalounge.blip.tv/file/2320926/">Video</a></li>
</ul>
<p>If you&#8217;re interested in catching future video, you can catch the <a href="http://lambdalounge.blip.tv/rss">feed from blip.tv</a> or we&#8217;re on <a href="http://lambdalounge.blip.tv/rss/itunes/">iTunes</a>.
</p>
]]></content:encoded>
			<wfw:commentRSS>http://tech.puredanger.com/2009/07/05/lambda-july-videos/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Fan and Fortress at the Lambda Lounge</title>
		<link>http://tech.puredanger.com/2009/06/29/fan-fortress-lounge/</link>
		<comments>http://tech.puredanger.com/2009/06/29/fan-fortress-lounge/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 03:04:56 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
		
	<category>languages</category>
	<category>st. louis</category>
	<category>Lambda Lounge</category>
	<category>Fortress</category>
	<category>Fan</category>
		<guid isPermaLink="false">http://tech.puredanger.com/2009/06/29/fan-fortress-lounge/</guid>
		<description><![CDATA[This month at the Lambda Lounge (Thursday, July 2nd, 6 pm), we will be bringing you two NEW languages that start with F.  
First, I will be introducing you to a research language called Fortress aimed at people doing scientific computation on large (peta-size) systems.  It is a language that explicitly tries to [...]]]></description>
			<content:encoded><![CDATA[<p>This month at the <a href="http://lambdalounge.org">Lambda Lounge</a> (Thursday, July 2nd, 6 pm), we will be bringing you two NEW languages that start with F.  </p>
<p>First, I will be introducing you to a research language called <a href="http://projectfortress.sun.com/Projects/Community">Fortress</a> aimed at people doing scientific computation on large (peta-size) systems.  It is a language that explicitly tries to meet the challenge of running systems up to a million cores and is &#8220;infested&#8221; with parallelism.  This is a language for tomorrow.</p>
<p>Next, <a href="http://kablooie.puredanger.com/">Scott Bale</a> will be talking about the <a href="http://fandev.org/">Fan programming language</a>.  This language takes the best of languages like Java and C#, throws out the cruft, and bleeds in some modern features.  Is it the next Java?  Java++?  Come find out&#8230;</p>
<p>Should be a great night with two languages making their debut at the Lounge.</p>
]]></content:encoded>
			<wfw:commentRSS>http://tech.puredanger.com/2009/06/29/fan-fortress-lounge/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Fortress rendering</title>
		<link>http://tech.puredanger.com/2009/06/29/fortress-rendering/</link>
		<comments>http://tech.puredanger.com/2009/06/29/fortress-rendering/#comments</comments>
		<pubDate>Mon, 29 Jun 2009 11:44:17 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
		
	<category>languages</category>
	<category>Lambda Lounge</category>
	<category>Fortress</category>
		<guid isPermaLink="false">http://tech.puredanger.com/2009/06/29/fortress-rendering/</guid>
		<description><![CDATA[One thing that I haven&#8217;t found time to write about yet is a hands-on lab I did at JavaOne about Project Fortress.  It was a very short overview about the language and then a series of hands-on exercises to get you cracking on the language.  Best of all, all the Fortress experts were [...]]]></description>
			<content:encoded><![CDATA[<p>One thing that I haven&#8217;t found time to write about yet is a hands-on lab I did at JavaOne about <a href="http://projectfortress.sun.com/Projects/Community">Project Fortress</a>.  It was a very short overview about the language and then a series of hands-on exercises to get you cracking on the language.  Best of all, all the Fortress experts were in the room helping out as people had questions so it was a great opportunity to get many questions answered quickly.  </p>
<p>Fortress is a language being developed as a research project at Sun.  Most people that have heard of it seem to vaguely know it has something to do with Fortran.  Turns out it really doesn&#8217;t have much of anything to do with Fortran other than a similar goal and intended user.  Fortress is targeted at large-scale scientific computing (like peta-scale large).</p>
<p>One thing you&#8217;ll notice immediately about it is that it has a strong mathematical syntax.  In many cases, things work differently than languages you may be more familiar with in favor of a declaration that is closer to normal mathematics.  For example, instead of writing <code>a * b</code> to multiply you simply write <code>a b</code>.  Putting two things next to each other is actually a special operator in Fortress called &#8220;juxtaposition&#8221; and can be overridden like many other operators.  For strings, juxtaposition means concatenation.  For a function, it means application.  </p>
<p>In addition, many traditional mathematical notations have textual representations you can use in your program, including all of the Greek symbols (alpha, beta, etc), various brackets, summation, sub-scripts, names like b&#8217;, etc.  The specification defines these textual representations and renderings, making full use of Unicode.  A program called <code>fortify</code> is provided to convert a Fortress program into LaTeX.  </p>
<p>I working on a Fortress presentation for the <a href="http://lambdalounge.org">Lambda Lounge</a> and lost a couple hours of my life the other night trying to do the conversion from LaTeX to PDF/image.  On Mac there are several comprehensive packages for stuff like this.  I tried to go with the smaller packages listed <a href="http://www.tug.org/mactex/morepackages.html">here</a> instead of the huge 1.2 GB <a href="http://www.tug.org/mactex/">MacTex package</a> but I ran into all kinds of problems with the missing St. Mary&#8217;s font of mathematical icons.  I have since learned that those problems are resolved by using the full package which includes the font.  </p>
<p>But I was also told of a <a href="http://projectfortress.sun.com/cgi-bin/fortifier">handy online tool </a>provided by the Fortress folks that will turn Fortress files into images and PDFs.  That is saving me from installing the giant LaTeX package for the moment.
</p>
]]></content:encoded>
			<wfw:commentRSS>http://tech.puredanger.com/2009/06/29/fortress-rendering/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Terracotta dev week</title>
		<link>http://tech.puredanger.com/2009/06/18/terracotta-dev-week/</link>
		<comments>http://tech.puredanger.com/2009/06/18/terracotta-dev-week/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 15:19:57 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
		
	<category>Java</category>
	<category>Terracotta</category>
	<category>Hibernate</category>
		<guid isPermaLink="false">http://tech.puredanger.com/2009/06/18/terracotta-dev-week/</guid>
		<description><![CDATA[This week the whole Terracotta engineering gang from all over the world is in San Francisco for Terracotta dev week.  We try to do this a couple times a year and it&#8217;s pretty intense.  The activities officially go from 8 am to 8 pm every day but it&#8217;s not uncommon for people to [...]]]></description>
			<content:encoded><![CDATA[<p>This week the whole Terracotta engineering gang from all over the world is in San Francisco for Terracotta dev week.  We try to do this a couple times a year and it&#8217;s pretty intense.  The activities officially go from 8 am to 8 pm every day but it&#8217;s not uncommon for people to be at the office late into the night.  </p>
<p>One activity we usually do is a performance tuning competition.  Being all about clustering apps and making them scale, we think it&#8217;s important for everyone on the team to be a guru in performance tuning.  We break everyone up into cross-functional teams and give them all the same app to work on.  In this case, we were working on an app built by our field guys to simulate single-sign on that was using MySQL and Hibernate for the backing store of authenticated credentials and every team got a 6-node cluster to work on (we&#8217;ve got a big perf lab :).  </p>
<p>For the next Terracotta release (3.1) we&#8217;re working on building the most kick-ass clustered Hibernate second-level cache in existence.  It will be the Chuck Norris of Hibernate second-level caches.  Anyhow, the test had two phases - a load phase (to fill the db and warm the cache) and a query phase.  The baselines for phase 1 were somewhere around 45 TPS and no one actually could baseline phase 2 because the app would run out of memory and crash.  Lots of room for improvement.</p>
<p>By the end of the competition, we had fixed some bugs in the app, made a bunch of configuration tuning changes for Hibernate and Terracotta, and even found a bunch of awesome improvements at various levels of Terracotta and the our cache implementation.  Most teams were seeing 3,000-4,000 TPS in phase 1, were able to load the entire 3 million cache entries without memory problems and were seeing in the region of 60,000 TPS in phase 2.  Pretty sweet.  Some of the memory-related optimizations we found are completely generic and should allow us to be much more efficient in all Terracotta apps.</p>
<p>At the moment, we are in the second day of a product improvement competition.  Generally we turn people loose and let them work on whatever they want with some focusing guidelines.  Last time we did this we had people try some crazy stuff but we didn&#8217;t put a high premium on actually getting something fully productized because it was a shorter competition.  This time, we put some more emphasis on making things that actually can go into the product right away and also a focus on usability and Hibernate users.  </p>
<p>The competition&#8217;s not over yet but there is some awesome stuff in work.  I suspect we&#8217;ll see even better Hibernate tooling come out of this (since several people are working on things for that) and several other usability and performance improvements.  It&#8217;s definitely a good thing to have the performance tuning competition first so people get a taste of where some bottlenecks are and where the tooling wasn&#8217;t helpful enough.  </p>
<p>All of this is scored by our judges (from other parts of the company) and we give out some prizes to those that have made the biggest impact.  We&#8217;ll finish out tomorrow with a bunch of brainstorming and meetings.  All in all, it&#8217;s a great team exercise and a lot of fun.  It&#8217;s also completely exhausting. :)
</p>
]]></content:encoded>
			<wfw:commentRSS>http://tech.puredanger.com/2009/06/18/terracotta-dev-week/feed/</wfw:commentRSS>
		</item>
		<item>
		<title>Entry set puzzler</title>
		<link>http://tech.puredanger.com/2009/06/11/collections-puzzler/</link>
		<comments>http://tech.puredanger.com/2009/06/11/collections-puzzler/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 02:51:03 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
		
	<category>Java</category>
	<category>collections</category>
		<guid isPermaLink="false">http://tech.puredanger.com/2009/06/11/collections-puzzler/</guid>
		<description><![CDATA[My colleague Chris Dennis, sent this puzzler around today:

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class EntrySetSize {

  public static void main(String[] args) {
    printSetSize(new ConcurrentHashMap());
    printSetSize(new HashMap());
  }

  private static void printSetSize(Map map) {
    map.put("hello", "world");
    map.put("world", "hello");
   [...]]]></description>
			<content:encoded><![CDATA[<p>My colleague Chris Dennis, sent this puzzler around today:</p>
<textarea name="code" class="java:nocontrols:nogutter" cols="60" rows="10">
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class EntrySetSize {

  public static void main(String[] args) {
    printSetSize(new ConcurrentHashMap());
    printSetSize(new HashMap());
  }

  private static void printSetSize(Map map) {
    map.put("hello", "world");
    map.put("world", "hello");
    Set set = new HashSet(map.entrySet());
    System.out.print(set.size() + " ");
  }
}
</textarea>
<p>The question is what does this program print when executed?  </p>
<ul>
<li>a) 2 2</li>
<li>b) 1 2</li>
<li>c) 0 2</li>
<li>d) something else&lt;/li>
</ul>
<p>It seems like the answer should be a) and you will actually see that in JDK 1.6+.  However, in JDK 1.5 you&#8217;ll see b).  Understanding why requires a bit of a jaunt through the CHM code, where dragons lurk.  Also, it&#8217;s actually possible to see several possible answers (at least a and b) in JDK 1.5 depending on what keys and values you use.</p>
<p><strong>map.entrySet()</strong></p>
<p>This call on a CHM does the following:</p>
<textarea name="code" class="java:nocontrols:nogutter" cols="60" rows="10">
    public Set&lt;Map.Entry&lt;K,V&gt;&gt; entrySet() {
        Set&lt;Map.Entry&lt;K,V&gt;&gt; es = entrySet;
        return (es != null) ? es : (entrySet = (Set&lt;Map.Entry&lt;K,V&gt;&gt;) (Set) new EntrySet());
    }
</textarea>
<p>All that does is create (and cache) an EntrySet instance, which is an inner (non-static) class specific to CHM.  It has access to the real CHM state and implements the interface using that state.  </p>
<p><strong>new HashSet()</strong></p>
<p>The HashSet is constructed with the EntrySet instance.  A new HashSet with an initialization collection will do this:</p>
<textarea name="code" class="java:nocontrols:nogutter" cols="60" rows="10">
    public HashSet(Collection&lt;? extends E&gt; c) {
        map = new HashMap&lt;E,Object&gt;(Math.max((int) (c.size()/.75f) + 1, 16));
        addAll(c);
    }
</textarea>
<p>So, the state from the source collection is added through addAll().</p>
<p><strong>HashSet.addAll()</strong></p>
<textarea name="code" class="java:nocontrols:nogutter" cols="60" rows="10">
    public boolean addAll(Collection&lt;? extends E&gt; c) {
        boolean modified = false;
        Iterator&lt;? extends E&gt; e = c.iterator();
        while (e.hasNext()) {
            if (add(e.next()))
                modified = true;
            }
            return modified;
        }
</textarea>
<p>There is some extra stuff here to manage the modified flag needed for fail-fast iteration but basically, this code walks through the collection&#8217;s iterator calling next(), then add() on the set under construction.  Here&#8217;s where things get interesting.  We need to look at what the EntrySet uses for an iterator and what happens when you call next().</p>
<p><strong>EntrySet.iterator()</strong></p>
<textarea name="code" class="java:nocontrols:nogutter" cols="60" rows="10">
        public Iterator&lt;Map.Entry&lt;K,V&gt;&gt; iterator() {
            return new EntryIterator();
        }
</textarea>
<p>Here we return yet another inner non-static class EntryIterator.  EntryIterator is defined as follows:</p>
<textarea name="code" class="java:nocontrols:nogutter" cols="60" rows="10">
final class EntryIterator extends HashIterator implements Map.Entry&lt;K,V&gt;, Iterator&lt;Entry&lt;K,V&gt;&gt;
</textarea>
<p>This should give you some pause.  We fully expect to see &#8220;extends HashIterator&#8221; - there are several key/value/entry iterator flavors buried in here and they share most of their implementation in HashIterator.  We also fully expect to see &#8220;implements Iterator&lt;Entry&lt;K,V&gt;&gt;&#8221; since that&#8217;s what we&#8217;re doing.  But seeing &#8220;implements Map.Entry&lt;K,V&gt;&#8221; should make you go huh?  This iterator walks over Entry&lt;K,V&gt; instances in the CHM yet it itself is typed as an entry?  </p>
<p>What does it do on Iterator.next()?</p>
<p><strong>EntryIterator.next()</strong></p>
<textarea name="code" class="java:nocontrols:nogutter" cols="60" rows="10">
        public Map.Entry&lt;K,V&gt; next() {
            nextEntry();
            return this;
        }
</textarea>
<p>Here we see the iterator advance to the next entry (it walks through segments at the high level and entries at the low level) but in the end it returns <strong>this</strong>?  </p>
<p>The key here is that the EntrySet&#8217;s iterator just stores the current key/value during iteration and returns itself as the entry object for every single actual entry.  If you modify that entry, it writes through appropriately to the CHM.  Presumably, this saves on the creation and destruction of lots of little Map.Entry instances that will be used just during iteration and typically never looked at again.</p>
<p>Therefore, when the EntrySet iterator is walked through in HashSet.addAll(), AND because of how EntryIterator.hashCode() is implemented:</p>
<textarea name="code" class="java:nocontrols:nogutter" cols="60" rows="10">
        public int hashCode() {
            // If not acting as entry, just use default.
            if (lastReturned == null)
                return super.hashCode();

            Object k = getKey();
            Object v = getValue();
            return ((k == null) ? 0 : k.hashCode()) ^
                   ((v == null) ? 0 : v.hashCode());
        }
</textarea>
<p>Here the hashCode is created as the bitwise OR of the key hash code and the value hash code.  So, regardless of what the key and value hashcodes are, a ^ b == b ^ a.  In our case we used two strings &#8220;hello&#8221; and &#8220;world&#8221; swapped.  Thus the second entry instance will be seen as an identical hash code (and instance) and will simply put over the first entry instance in the HashSet.</p>
<p>A subtly different bug will happen in the case where this property (of swapped key/value) doesn&#8217;t occur.  In that case, both entries will end up in the HashSet (size == 2 as expected) but they are the <em>same</em> instance and were put into the set with different hash codes.  It&#8217;s always a bad deal to put something in a HashSet or HashMap and then modify the object in ways that modify the hashCode().  This breaks assumptions in the HashMap class that can cause lost elements in the collection and other weird behavior.</p>
<p>As I mentioned, this same instance behavior is probably surprising to most people.  It&#8217;s doc&#8217;ed here in <a href="http://bugs.sun.com/view_bug.do?bug_id=6312706">Sun bug #6312706</a>; as a general problem in several maps (CHM, IdentityHashMap, and EnumMap).  That bug is marked as &#8220;fix understood&#8221; which is kind of surprising because this behavior is already changed and fixed in JDK 1.6.  In 1.6, the EntryIterator is implemented as:</p>
<textarea name="code" class="java:nocontrols:nogutter" cols="60" rows="10">
    final class EntryIterator
	extends HashIterator
	implements Iterator&lt;Entry&lt;K,V&gt;&gt;
    {
        public Map.Entry&lt;K,V&gt; next() {
            HashEntry&lt;K,V&gt; e = super.nextEntry();
            return new WriteThroughEntry(e.key, e.value);
        }
    }
</textarea>
<p>Here EntryIterator does not implement Map.Entry and next() returns a new instance of WriteThroughEntry, which causes the HashSet to be much happier on creation.</p>
<p>Interestingly, another smart colleague noted that the existing Terracotta ConcurrentHashMap instrumentation fixes the 1.5 implementation by creating a wrapper object in the iterator as well.
</p>
]]></content:encoded>
			<wfw:commentRSS>http://tech.puredanger.com/2009/06/11/collections-puzzler/feed/</wfw:commentRSS>
		</item>
	</channel>
</rss>
