<?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>A Public Scratchpad &#187; Optimization</title>
	<atom:link href="http://daniel.gredler.net/category/optimization/feed/" rel="self" type="application/rss+xml" />
	<link>http://daniel.gredler.net</link>
	<description></description>
	<lastBuildDate>Wed, 20 Jan 2010 16:51:39 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='daniel.gredler.net' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/e02d03261ade2963f044fba243814f10?s=96&#038;d=http://s2.wp.com/i/buttonw-com.png</url>
		<title>A Public Scratchpad &#187; Optimization</title>
		<link>http://daniel.gredler.net</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://daniel.gredler.net/osd.xml" title="A Public Scratchpad" />
	<atom:link rel='hub' href='http://daniel.gredler.net/?pushpress=hub'/>
		<item>
		<title>Optimizing Spring Application Startup Time</title>
		<link>http://daniel.gredler.net/2010/01/20/optimizing-spring-application-startup-time/</link>
		<comments>http://daniel.gredler.net/2010/01/20/optimizing-spring-application-startup-time/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 16:17:45 +0000</pubDate>
		<dc:creator>Daniel Gredler</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Spring]]></category>

		<guid isPermaLink="false">http://daniel.gredler.net/?p=243</guid>
		<description><![CDATA[The application I&#8217;m currently working on is built around Spring. A couple of weeks ago, as I waited for it to start up for the 27th time that day, I began wondering if there was any way to get it to start faster. Coincidentally, our whole development team had just received upgraded laptops, so the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=daniel.gredler.net&blog=887150&post=243&subd=gredler&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>The application I&#8217;m currently working on is built around Spring. A couple of weeks ago, as I waited for it to start up for the 27th time that day, I began wondering if there was any way to get it to start faster. Coincidentally, our whole development team had just received upgraded laptops, so the fact that numbers are stagnating in the &#8220;Megahertz&#8221; department (but not in the &#8220;CPU cores&#8221; department) had been recently emphasized.</p>
<p>Further, I had just read a series of <a href="http://beust.com/weblog/?p=531">articles</a> by Cédric Beust regarding recent improvements to the <a href="http://testng.org/">TestNG</a> threading engine. In short, TestNG allows you to run tests on multiple threads in order to shorten the test suite run time. However, when you start creating tests with dependencies on other tests, TestNG punts and runs all of these groups of dependent tests serially, on a single thread. Cédric wanted to remove this limitation, and approached the enhancement as a <a href="http://en.wikipedia.org/wiki/Topological_sorting">topological sort</a> problem, viewing the tests as nodes in a directed acyclic graph.</p>
<p>Most of the startup time in Spring-centric applications involves the initialization of singleton beans. It wasn&#8217;t too much of a mental leap to wonder if the same graph/sort approach could be applied to the application startup optimization question &#8212; with the Spring beans acting as the nodes in the graph, instead of the tests.</p>
<p>It turns out to be (sort of) possible, and (sort of) worth the time. I&#8217;ve posted the details, including the source, <a href="http://github.com/gredler/spriths">here</a>. Feel free to check it out and make suggestions. The Spring framework does contain a very coarse lock that required a pretty horrible hack in order to get any performance gains out of the experiment, but once this is worked out I don&#8217;t see why an implementation of this approach couldn&#8217;t make it into real applications. Again, see the <a href="http://github.com/gredler/spriths">project page</a> for more details.</p>
<p>On a side note, this is the first time that I&#8217;ve used <a href="http://github.com/">GitHub</a> to host any code, and I have to say that I <strong>really</strong> like the fact that the README file serves the dual purposes of local project documentation and homepage markup!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gredler.wordpress.com/243/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gredler.wordpress.com/243/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gredler.wordpress.com/243/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gredler.wordpress.com/243/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gredler.wordpress.com/243/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gredler.wordpress.com/243/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gredler.wordpress.com/243/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gredler.wordpress.com/243/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gredler.wordpress.com/243/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gredler.wordpress.com/243/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=daniel.gredler.net&blog=887150&post=243&subd=gredler&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://daniel.gredler.net/2010/01/20/optimizing-spring-application-startup-time/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d71895d31422ff2da109a665d5f6d256?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gredler</media:title>
		</media:content>
	</item>
		<item>
		<title>URL.hashCode() Considered Harmful</title>
		<link>http://daniel.gredler.net/2008/04/24/urlhashcode-considered-harmful/</link>
		<comments>http://daniel.gredler.net/2008/04/24/urlhashcode-considered-harmful/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 05:45:13 +0000</pubDate>
		<dc:creator>Daniel Gredler</dc:creator>
				<category><![CDATA[HtmlUnit]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Optimization]]></category>

		<guid isPermaLink="false">http://gredler.wordpress.com/?p=64</guid>
		<description><![CDATA[I just cut HtmlUnit&#8217;s build time by about 20% by changing four lines of code. How? HtmlUnit keeps a small cache of web requests in a HashMap, keyed on the request URL. The problem with this is twofold:

The URL.hashCode() method is synchronized.
The URL.hashCode() method triggers DNS lookups for the URL hosts.

The impact of item 2 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=daniel.gredler.net&blog=887150&post=64&subd=gredler&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p>I just cut HtmlUnit&#8217;s build time by about 20% by changing four lines of code. How? HtmlUnit keeps a small cache of web requests in a <em>HashMap</em>, keyed on the request URL. The problem with this is twofold:</p>
<ol>
<li>The URL.hashCode() method is synchronized.</li>
<li>The URL.hashCode() method triggers DNS lookups for the URL hosts.</li>
</ol>
<p>The impact of item 2 was magnified by the fact that some of the HtmlUnit unit tests use a mock web connection to connect to fake URLs. DNS (non)resolution of these fake URLs took an especially long time.</p>
<p>The fix was to key the map entries on the value of <em>URL.toString()</em> instead. Apparently I&#8217;m not the first person to stumble across <a href="http://www.sworddance.com/blog/2007/09/09/code-review-4-always-read-the-documentationcode-aka-javaneturl-is-evil/">this problem</a>. So think twice before coding your next <em>HashMap&lt;URL, XXX&gt;</em> <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/gredler.wordpress.com/64/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/gredler.wordpress.com/64/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gredler.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gredler.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gredler.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gredler.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gredler.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gredler.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gredler.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gredler.wordpress.com/64/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gredler.wordpress.com/64/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gredler.wordpress.com/64/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=daniel.gredler.net&blog=887150&post=64&subd=gredler&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://daniel.gredler.net/2008/04/24/urlhashcode-considered-harmful/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d71895d31422ff2da109a665d5f6d256?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gredler</media:title>
		</media:content>
	</item>
		<item>
		<title>Space vs Time</title>
		<link>http://daniel.gredler.net/2008/01/26/space-vs-time/</link>
		<comments>http://daniel.gredler.net/2008/01/26/space-vs-time/#comments</comments>
		<pubDate>Sat, 26 Jan 2008 08:56:37 +0000</pubDate>
		<dc:creator>Daniel Gredler</dc:creator>
				<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://gredler.wordpress.com/?p=60</guid>
		<description><![CDATA[A long, long time ago I took a college course, the title of which was Languages and Translation. The content of this sophomore-level course? A smörgåsbord of systems programming, heaps and stacks, pointers, *nix system calls, compilers, lex and yacc, grammars, lexical and semantic analysis, code optimization, and data representation &#8212; all taught and learned [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=daniel.gredler.net&blog=887150&post=60&subd=gredler&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.lyricsfreak.com/d/don+mclean/american+pie_20042099.html">A long, long time ago</a> I took a college course, the title of which was <a href="http://www-static.cc.gatech.edu/classes/AY2000/cs2330_summer/">Languages and Translation</a>. The content of this sophomore-level course? A smörgåsbord of systems programming, heaps and stacks, pointers, *nix system calls, compilers, lex and yacc, grammars, lexical and semantic analysis, code optimization, and data representation &#8212; all taught and learned in C. While learning C. Oh, and Lisp.</p>
<p>This course made quite an impression, mainly because of my initial inexperience. I should explain that the path which led me to L&amp;T went something like this:</p>
<ol>
<li>I&#8217;m a senior in high school. I&#8217;m applying to college. I need to choose a major. Hmm&#8230; writing that Blackjack game on my TI calculator was pretty cool. It was a whole 50 lines of code! Plus I&#8217;m in that typing class, closing in on 20 words per minute. Maybe I&#8217;ll try Computer Science!</li>
<li>I&#8217;m a freshman at Georgia Tech. First semester. I&#8217;m taking Intro to Computing. Man, this HTML nonsense sure uses a lot of brackets! And this Microsoft Access program is impossible to use!</li>
<li>Still a freshman at Tech. Second semester. This Intro to Programming class is pretty crazy! We&#8217;re using Java for the assignments. The TA mentioned in passing that there are no pointers in Java, but I have no idea what a pointer is, so I could care less. I&#8217;m beginning to grok object-oriented programming.</li>
<li>Welcome to Languages and Translation! Malloc, Malloc, Malloc! Realloc, Calloc, Malloc! <b>Bwahahahahahaha!</b></li>
</ol>
<p>Psychological damage aside, this was a great class. Jim Greenlee, who taught the course, was both an evil bastard and a great teacher. One of the tenets of code optimization which he often highlighted was &#8220;space versus time,&#8221; the idea that you can often optimize for one at the expense of the other, but rarely for both at the same time.</p>
<p>For example, a compiler can decide to inline a short function in order to avoid time-consuming stack allocations, but the compiled program will be larger (less time, but more space). Of course, if space (memory) is at a premium, your compiler might instead try to recognize common code sequences and hoist them into artificial functions (less space, but more time).</p>
<p>Flash forward 8 years. We have a client/server application at work which uses DTOs to transfer data to and from the client, and we use Hibernate on the server to persist our BOs. A specific server call, invoked in the presence of a large amount of data, brings the application to its knees.</p>
<p>Immediately we jumped to conclusions &#8212; <i>Arrgh! Hibernate is such a hog! If you don&#8217;t code things perfectly, you can&#8217;t scale! And sometimes not even then!</i> A quick profiling session confirmed our fears. Three hours later we had bypassed Hibernate in this specific instance, coding to the JDBC API instead. Unfortunately, this wasn&#8217;t the last of our performance problems. A second profiling session indicated that we had another bottleneck in our DTO-to-BO conversion routines!</p>
<p>Now, something which must be understood about Hibernate&#8217;s collection semantics is that when you use Hibernate to load BO <i>A</i>, which has an X-to-N relationship with BO <i>B</i>, you should (usually) use the collection of <i>B</i>s provided by Hibernate. For example, if you use Hibernate to load a <i>UserGroup</i>, and you want to modify the list of <i>Users</i> associated with said <i>UserGroup</i>, you should modify the existing list of <i>Users</i>. You should not create a new list, add <i>Users</i> to it, and then give the <i>UserGroup</i> the new list of <i>Users</i>. Why? Because creating a new list results in a <a href="http://www.hibernate.org/hib_docs/reference/en/html/performance.html#performance-collections-oneshotdelete">one-shot delete</a>, followed by N insert statements. This is usually not desired.</p>
<p>However, a naive approach to modifying the collection provided by Hibernate (clearing it and then adding the BOs which you know you want) is just as bad, because a call to <i>collection.clear()</i> also results in a one-shot delete. The best approach is one of minimal modification to the existing collection.</p>
<p>In the case of DTO-to-BO conversion, where the DTO representation of an object is being transferred to the corresponding BO representation, this means adding items to the BO&#8217;s collection that are in the DTO&#8217;s collection but not in the BO&#8217;s collection, and removing items from the BO&#8217;s collection that are in the BO&#8217;s collection but not in the DTO&#8217;s collection. Elements that are in both collections are simply ignored.</p>
<p>The obvious implementation of this algorithm looks something like this:</p>
<pre>for(DTO dto : dtos) {
 if(!contains(bos, dto)) add(bos, dto);
}

for(BO bo : bos) {
 if(!contains(dtos, bo)) remove(bos, bo);
}</pre>
<p>Unfortunately, when using lists, the <i>contains()</i> calls above hide a nested loop, resulting in <i>O(n<sup>2</sup>)</i> performance. Once <i>n</i> gets into the thousands, things start to get sluuuugish. Veeeeery sluuuugish.</p>
<p>The solution? Trade a little space for a lot of time! By constructing <i>HashMaps</i> which contain all of the BOs in the lists, keyed on business keys which uniquely identify the BOs, the <i>contains()</i> calls above can be performed in constant time by invoking <i>map.containsKey()</i>. The result is <i>O(n)</i> performance. Much better!</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/gredler.wordpress.com/60/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/gredler.wordpress.com/60/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/gredler.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/gredler.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/gredler.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/gredler.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/gredler.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/gredler.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/gredler.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/gredler.wordpress.com/60/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/gredler.wordpress.com/60/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/gredler.wordpress.com/60/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=daniel.gredler.net&blog=887150&post=60&subd=gredler&ref=&feed=1" />]]></content:encoded>
			<wfw:commentRss>http://daniel.gredler.net/2008/01/26/space-vs-time/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/d71895d31422ff2da109a665d5f6d256?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">gredler</media:title>
		</media:content>
	</item>
	</channel>
</rss>