<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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" version="2.0">

<channel>
	<title>(ns org.danlarkin.blog)</title>
	
	<link>http://blog.danlarkin.org</link>
	<description />
	<pubDate>Mon, 08 Dec 2008 04:20:32 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.1</generator>
	<language>en</language>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/blog-danlarkin-org" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="blog-danlarkin-org" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>“phrase from nearest book” meme</title>
		<link>http://blog.danlarkin.org/2008/11/phrase-from-nearest-book-meme/</link>
		<comments>http://blog.danlarkin.org/2008/11/phrase-from-nearest-book-meme/#comments</comments>
		<pubDate>Thu, 13 Nov 2008 03:13:09 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[meme]]></category>

		<guid isPermaLink="false">http://blog.danlarkin.org/?p=110</guid>
		<description><![CDATA[
Grab the nearest book.
Open it to page 56.
Find the fifth sentence.
Post the text of the sentence in your journal along with these instructions.
Don’t dig for your favorite book, the cool book, or the intellectual one: pick the CLOSEST.

Here&#8217;s mine:
&#8220;401 responses should include a WWW-Authenticate header field indicating the name of the protected realm.&#8221;
]]></description>
			<content:encoded><![CDATA[<ul>
<li>Grab the nearest book.</li>
<li>Open it to page 56.</li>
<li>Find the fifth sentence.</li>
<li>Post the text of the sentence in your journal along with these instructions.</li>
<li>Don’t dig for your favorite book, the cool book, or the intellectual one: pick the CLOSEST.</li>
</ul>
<p>Here&#8217;s mine:</p>
<p>&#8220;401 responses should include a <em>WWW-Authenticate</em> header field indicating the name of the protected realm.&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.danlarkin.org/2008/11/phrase-from-nearest-book-meme/feed/</wfw:commentRss>
		</item>
		<item>
		<title>apply _is_ lazy (and I was wrong)</title>
		<link>http://blog.danlarkin.org/2008/11/apply-is-lazy-and-i-was-wrong/</link>
		<comments>http://blog.danlarkin.org/2008/11/apply-is-lazy-and-i-was-wrong/#comments</comments>
		<pubDate>Thu, 06 Nov 2008 22:20:15 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[apply]]></category>

		<category><![CDATA[clojure]]></category>

		<category><![CDATA[heap]]></category>

		<category><![CDATA[retraction]]></category>

		<guid isPermaLink="false">http://blog.danlarkin.org/?p=92</guid>
		<description><![CDATA[So like Chouser and Rich said apply is, in fact, lazy. spread does not walk the whole list &#8212; just enough to satisfy the function passed to apply.
So one can (apply str (range 10e6)) and the only limit will be how big of a string can fit in one&#8217;s heap.
In fact,

user=&#62; &#40;time &#40;first &#40;apply #&#40;lazy-cat [...]]]></description>
			<content:encoded><![CDATA[<p>So like <a href="http://blog.danlarkin.org/2008/11/apply-is-not-lazy/#comment-3">Chouser</a> and <a href="http://blog.danlarkin.org/2008/11/apply-is-not-lazy/#comment-4">Rich</a> said <code>apply</code> is, in fact, lazy. <code>spread</code> does not walk the whole list &#8212; just enough to satisfy the function passed to <code>apply</code>.</p>
<p>So one can <code>(apply str (range 10e6))</code> and the only limit will be how big of a string can fit in one&#8217;s heap.</p>
<p>In fact,</p>

<div class="wp_syntax"><div class="code"><pre class="lisp lisp" style="font-family:monospace;">user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>time <span style="color: #66cc66;">&#40;</span>first <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">apply</span> #<span style="color: #66cc66;">&#40;</span>lazy<span style="color: #66cc66;">-</span>cat <span style="color: #66cc66;">%&amp;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>range 10e200<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #ff0000;">&quot;Elapsed time: 0.351 msecs&quot;</span>
0</pre></div></div>

<p>definitely shows that <code>apply</code> doesn&#8217;t evaluate the whole list.</p>
<p>Sorry guys <img src='http://blog.danlarkin.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.danlarkin.org/2008/11/apply-is-lazy-and-i-was-wrong/feed/</wfw:commentRss>
		</item>
		<item>
		<title>apply is not lazy</title>
		<link>http://blog.danlarkin.org/2008/11/apply-is-not-lazy/</link>
		<comments>http://blog.danlarkin.org/2008/11/apply-is-not-lazy/#comments</comments>
		<pubDate>Tue, 04 Nov 2008 05:10:23 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[apply]]></category>

		<category><![CDATA[clojure]]></category>

		<category><![CDATA[heap]]></category>

		<category><![CDATA[lazy]]></category>

		<category><![CDATA[lazy-apply]]></category>

		<guid isPermaLink="false">http://blog.danlarkin.org/?p=73</guid>
		<description><![CDATA[So I&#8217;ve been trying to figure out why clojure can&#8217;t deal with applying on a huge list.  Here&#8217;s an example of the problem:

user=&#62; &#40;apply str &#40;range 10e6&#41;&#41;
java.lang.OutOfMemoryError: Java heap space &#40;NO_SOURCE_FILE:0&#41;

And here&#8217;s the source for apply straight from boot.clj in the clojure distribution (as of r1081):

&#40;defn spread
  &#123;:private true&#125;
  &#91;arglist&#93;
  &#40;cond
 [...]]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;ve been trying to figure out why clojure can&#8217;t deal with <code>apply</code>ing on a huge list.  Here&#8217;s an example of the problem:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp lisp" style="font-family:monospace;">user<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">apply</span> str <span style="color: #66cc66;">&#40;</span>range 10e6<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
java.lang.OutOfMemoryError: <span style="color: #555;">Java</span> heap space <span style="color: #66cc66;">&#40;</span>NO_SOURCE_FILE:0<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>And here&#8217;s the source for <code>apply</code> straight from boot.clj in the clojure distribution (as of <a href="http://clojure.svn.sourceforge.net/viewvc/clojure/trunk/src/clj/clojure/boot.clj?revision=1081&#038;view=markup">r1081</a>):</p>

<div class="wp_syntax"><div class="code"><pre class="lisp lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>defn spread
  <span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">private</span> true<span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#91;</span>arglist<span style="color: #66cc66;">&#93;</span>
  <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cond</span>
   <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nil</span>? arglist<span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">nil</span>
   <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">nil</span>? <span style="color: #66cc66;">&#40;</span>rest arglist<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>seq <span style="color: #66cc66;">&#40;</span>first arglist<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
   :<span style="color: #555;">else</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">cons</span> <span style="color: #66cc66;">&#40;</span>first arglist<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#40;</span>spread <span style="color: #66cc66;">&#40;</span>rest arglist<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#40;</span>defn <span style="color: #b1b100;">apply</span>
  <span style="color: #ff0000;">&quot;Applies fn f to the argument list formed by prepending args to argseq.&quot;</span>
  <span style="color: #66cc66;">&#123;</span>:<span style="color: #555;">arglists</span> '<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>f args<span style="color: #66cc66;">*</span> argseq<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#91;</span>#<span style="color: #66cc66;">^</span>clojure.lang.IFn f <span style="color: #66cc66;">&amp;</span>amp<span style="color: #808080; font-style: italic;">; args]</span>
  <span style="color: #66cc66;">&#40;</span>. f <span style="color: #66cc66;">&#40;</span>applyTo <span style="color: #66cc66;">&#40;</span>spread args<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>I included the source for <code>spread</code> too because it&#8217;s called from <code>apply</code> and it&#8217;s the source of the problem.  <code>spread</code> walks the entire list it&#8217;s passed and returns a series of <code>cons</code> cells (a list).  So using <code>apply</code> forces an eager (non-lazy) evaluation of the list &#8212; which is absolutely not what I want with such a large list.</p>
<p>So I need a <code>lazy-apply</code>&#8230; I wonder if that&#8217;s even possible.</p>
<p><em>UPDATE:</em> I was wrong, <a href="http://blog.danlarkin.org/2008/11/apply-is-lazy-and-i-was-wrong/">apply is lazy</a>.  Thanks for the comments Chouser and Rich.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.danlarkin.org/2008/11/apply-is-not-lazy/feed/</wfw:commentRss>
		</item>
		<item>
		<title>clojure-json memory usage</title>
		<link>http://blog.danlarkin.org/2008/11/clojure-json-memory-usage/</link>
		<comments>http://blog.danlarkin.org/2008/11/clojure-json-memory-usage/#comments</comments>
		<pubDate>Mon, 03 Nov 2008 04:45:15 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[benchmark]]></category>

		<category><![CDATA[clojure]]></category>

		<category><![CDATA[heap]]></category>

		<category><![CDATA[json]]></category>

		<category><![CDATA[jswat]]></category>

		<category><![CDATA[microbenchmark]]></category>

		<category><![CDATA[profiling]]></category>

		<guid isPermaLink="false">http://blog.danlarkin.org/?p=48</guid>
		<description><![CDATA[Today I decided to do a little bit of microbenchmarking on my clojure JSON encoder.
Once again I import the encoder with

user=&#62;&#40;ns foo &#40;:require &#40;org.danlarkin &#91;json :as json&#93;&#41;&#41;&#41;
nil
foo=&#62;

And I run a simple benchmark of a list of 10e5 items

foo=&#62; &#40;dorun &#91;&#40;time &#40;json/encode &#40;range 10e5&#41;&#41;&#41;&#93;&#41;
&#34;Elapsed time: 5535.336 msecs&#34;
nil

That run is about the average running time on my [...]]]></description>
			<content:encoded><![CDATA[<p>Today I decided to do a little bit of microbenchmarking on my clojure JSON encoder.</p>
<p>Once again I import the encoder with</p>

<div class="wp_syntax"><div class="code"><pre class="lisp lisp" style="font-family:monospace;">user<span style="color: #66cc66;">=&gt;</span><span style="color: #66cc66;">&#40;</span>ns foo <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">require</span> <span style="color: #66cc66;">&#40;</span>org.danlarkin <span style="color: #66cc66;">&#91;</span>json :<span style="color: #555;">as</span> json<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #b1b100;">nil</span>
foo<span style="color: #66cc66;">=&gt;</span></pre></div></div>

<p>And I run a simple benchmark of a list of 10e5 items</p>

<div class="wp_syntax"><div class="code"><pre class="lisp lisp" style="font-family:monospace;">foo<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>dorun <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>time <span style="color: #66cc66;">&#40;</span>json<span style="color: #66cc66;">/</span>encode <span style="color: #66cc66;">&#40;</span>range 10e5<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #ff0000;">&quot;Elapsed time: 5535.336 msecs&quot;</span>
<span style="color: #b1b100;">nil</span></pre></div></div>

<p>That run is about the average running time on my machine.  So it&#8217;s about 5.5 seconds for a list of 10e5 items, not bad I guess.  But what happens when I try <code>(range 10e6)</code>?</p>

<div class="wp_syntax"><div class="code"><pre class="lisp lisp" style="font-family:monospace;">foo<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>dorun <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#40;</span>time <span style="color: #66cc66;">&#40;</span>json<span style="color: #66cc66;">/</span>encode <span style="color: #66cc66;">&#40;</span>range 10e6<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
java.lang.OutOfMemoryError: <span style="color: #555;">Java</span> heap space <span style="color: #66cc66;">&#40;</span>NO_SOURCE_FILE:0<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Uh oh!  So why is it happening? My first guess was maybe my algorithm is keeping too much garbage as it iterates through the list but it turns out I can&#8217;t even create a list of 10e6 items in the first place:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp lisp" style="font-family:monospace;">foo<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">apply</span> <span style="color: #b1b100;">list</span> <span style="color: #66cc66;">&#40;</span>range 10e6<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
java.lang.OutOfMemoryError: <span style="color: #555;">Java</span> heap space <span style="color: #66cc66;">&#40;</span>NO_SOURCE_FILE:0<span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>I can get the list of <code>(range 10e6)</code> to evaluate if I run the jvm with a max heap size of 1GiB (pass <code>-Xmx1024m</code> to <code>java</code>) but encoding it to JSON still blows the heap.</p>
<p>This would be a great time to hook up a debugger like <a href="http://code.google.com/p/jswat/">JSwat</a> and see what&#8217;s going on except that I can&#8217;t get it to run on my machine (NullPointerException when I start it up&#8211; I&#8217;ve got an email out asking for help).</p>
<p>I took a guess that maybe my excessive memory usage was coming from passing java.lang.String objects around everywhere so I started a <a href="http://github.com/danlarkin/clojure-json/tree/writer">new branch</a> to use java.io.Writer objects which I believed to be more memory-efficient.  It didn&#8217;t help this situation, though, since even with the writer branch I&#8217;m overflowing a 1GiB heap.</p>
<p>Hopefully in the near future I can get JSwat working and figure out what&#8217;s taking up so much RAM.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.danlarkin.org/2008/11/clojure-json-memory-usage/feed/</wfw:commentRss>
		</item>
		<item>
		<title>First clojure project</title>
		<link>http://blog.danlarkin.org/2008/11/first-clojure-project/</link>
		<comments>http://blog.danlarkin.org/2008/11/first-clojure-project/#comments</comments>
		<pubDate>Sun, 02 Nov 2008 02:30:47 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[clojure]]></category>

		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://blog.danlarkin.org/?p=13</guid>
		<description><![CDATA[Today I wrote and uploaded to github my very first clojure project.  It&#8217;s a JSON-encoder.  It takes arbitrarily-nested clojure datastructure and returns a JSON-encoded string.
As the README says, installing is as simple as adding the src directory to your classpath and importing it into your namespace with something like

&#40;ns foo &#40;:require &#40;org.danlarkin &#91;json [...]]]></description>
			<content:encoded><![CDATA[<p>Today I wrote and uploaded to <a href="http://github.com/danlarkin/clojure-json/tree/master">github</a> my very first clojure project.  It&#8217;s a JSON-encoder.  It takes arbitrarily-nested clojure datastructure and returns a JSON-encoded string.</p>
<p>As the README says, installing is as simple as adding the src directory to your classpath and importing it into your namespace with something like</p>

<div class="wp_syntax"><div class="code"><pre class="lisp lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span>ns foo <span style="color: #66cc66;">&#40;</span>:<span style="color: #555;">require</span> <span style="color: #66cc66;">&#40;</span>org.danlarkin <span style="color: #66cc66;">&#91;</span>json :<span style="color: #555;">as</span> json<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Using it is pretty simple too,</p>

<div class="wp_syntax"><div class="code"><pre class="lisp lisp" style="font-family:monospace;">foo<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>json<span style="color: #66cc66;">/</span>encode <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span>,<span style="color: #cc66cc;">3</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">5</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #ff0000;">&quot;[1,2,3,4,5]&quot;</span>
foo<span style="color: #66cc66;">=&gt;</span> <span style="color: #66cc66;">&#40;</span>json<span style="color: #66cc66;">/</span>encode <span style="color: #66cc66;">&#123;</span><span style="color: #ff0000;">&quot;a&quot;</span> <span style="color: #cc66cc;">1</span> <span style="color: #ff0000;">&quot;b&quot;</span> <span style="color: #cc66cc;">2</span> <span style="color: #ff0000;">&quot;c&quot;</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #ff0000;">&quot;{<span style="color: #000099; font-weight: bold;">\&quot;</span>a<span style="color: #000099; font-weight: bold;">\&quot;</span>:1,<span style="color: #000099; font-weight: bold;">\&quot;</span>b<span style="color: #000099; font-weight: bold;">\&quot;</span>:2,<span style="color: #000099; font-weight: bold;">\&quot;</span>c<span style="color: #000099; font-weight: bold;">\&quot;</span>:3}&quot;</span>
 </pre></div></div>

<p>It took me a few hours to code this up, a length of time I&#8217;m pretty happy with considering I haven&#8217;t done <em>that</em> much in clojure yet and I had to consult <a href="http://json.org">json.org</a> quite a bit.</p>
<p>Next step: a JSON-decoder.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.danlarkin.org/2008/11/first-clojure-project/feed/</wfw:commentRss>
		</item>
	</channel>
</rss><!-- Dynamic Page Served (once) in 0.471 seconds -->
