<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7357044393624548611</id><updated>2011-09-07T07:34:25.584-07:00</updated><category term='Conservative GC'/><category term='Python'/><category term='Go'/><category term='Conservative'/><category term='CLR'/><category term='Garbage collection'/><category term='GIL'/><category term='JVM'/><category term='Meta Computer Science'/><category term='Programming as craftsmanship'/><category term='reference counting'/><category term='Concurrency'/><title type='text'>Blaisorblade programming thoughts</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://blaisorbladeprog.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7357044393624548611/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://blaisorbladeprog.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Paolo G. Giarrusso</name><uri>http://www.blogger.com/profile/04485097839438234853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>9</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7357044393624548611.post-9052997499133387846</id><published>2010-07-28T06:33:00.000-07:00</published><updated>2010-07-28T06:33:15.687-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Concurrency'/><title type='text'>Automatic race detection</title><content type='html'>&lt;div&gt;I wanted to recommend a cool Google tool, based on Valgrind, which performs automatic race detection:&lt;/div&gt;&lt;a href="http://code.google.com/p/data-race-test/wiki/ThreadSanitizer"&gt;http://code.google.com/p/data-race-test/wiki/ThreadSanitizer&lt;/a&gt;&lt;br /&gt;&lt;div&gt;Also DRD and Helgrind exist (both part of Valgrind), but I've seen Helgrind considered "not very stable" more often than not (and it's still maybe a bit experimental). While ThreadSanitizer seems to be in active development and use, and it is supported by Google. And it even has Vim integration!&lt;br /&gt;I remembered of this during this discussion about something similar to race detection, in a&amp;nbsp;context halfway between&amp;nbsp;Python and share-nothing concurrency:&amp;nbsp;&lt;a href="http://codespeak.net/pipermail/pypy-dev/2010q3/006037.html"&gt;http://codespeak.net/pipermail/pypy-dev/2010q3/006037.html&lt;/a&gt;&lt;br /&gt;I hope to get your comments on this tool.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7357044393624548611-9052997499133387846?l=blaisorbladeprog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blaisorbladeprog.blogspot.com/feeds/9052997499133387846/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://blaisorbladeprog.blogspot.com/2010/07/automatic-race-detection.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7357044393624548611/posts/default/9052997499133387846'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7357044393624548611/posts/default/9052997499133387846'/><link rel='alternate' type='text/html' href='http://blaisorbladeprog.blogspot.com/2010/07/automatic-race-detection.html' title='Automatic race detection'/><author><name>Paolo G. Giarrusso</name><uri>http://www.blogger.com/profile/04485097839438234853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7357044393624548611.post-6949461704891995933</id><published>2010-07-27T08:29:00.000-07:00</published><updated>2010-09-10T07:50:29.725-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Meta Computer Science'/><title type='text'>Pretending you know what you're talking about - a book review</title><content type='html'>&lt;div style="margin: 0px;"&gt;Some books in Computer Science seem to do just that: pretending they know what they are talking about, when they in fact do not.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://norvig.com/java-iaq.html"&gt;A quote on this&lt;/a&gt;, not restricted to Java books:&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;blockquote&gt;Most Java books are written by people who couldn't get a job as a Java programmer (since programming almost always pays more than book writing; I know because I've done both). These books are full of errors, bad advice, and bad programs.&lt;/blockquote&gt;&lt;/div&gt;&lt;div style="margin: 0px; text-align: right;"&gt;Peter Norvig, Director of Research at Google, Inc.&lt;/div&gt;&lt;div style="margin: 0px;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin: 0px;"&gt;I have seen such books myself. But they do not get great reviews at Amazon and they do not get &lt;a href="http://developers.slashdot.org/article.pl?sid=02/06/25/1223234"&gt;slashdotted&lt;/a&gt;.&lt;/div&gt;&lt;div style="margin: 0px;"&gt;Instead,&amp;nbsp;&lt;a href="http://www.amazon.com/Virtual-Machine-Design-Implementation-Blunden/dp/1556229038/ref=cm_cr_pr_product_top"&gt;&lt;b&gt;Virtual Machine Design and Implementation in C/C++&lt;/b&gt;&lt;/a&gt;&amp;nbsp;does. Its author is great at pretending he knows what he is talking about. I started looking at it for my seminar on Virtual Machines, hoping to make some use of it, and I was deeply disappointed. Looking at the table of contents and index,&amp;nbsp;&lt;a href="http://www.amazon.com/Virtual-Machines-Versatile-Platforms-Architecture/dp/1558609105"&gt;Virtual Machines: Versatile Platforms for Systems and Processes&lt;/a&gt;&amp;nbsp;&lt;i&gt;seems&lt;/i&gt;&amp;nbsp;to be&amp;nbsp;a better book on the topic , which covers also system virtualization (a different topic having the same name), however I cannot really judge.&lt;br /&gt;&lt;a href="http://www.amazon.com/Virtual-Machines-Iain-D-Craig/dp/1852339691"&gt;Virtual Machines, by Iain D. Craig&lt;/a&gt;, seems instead devoted to semantic issues, and I am not qualified to judge that topic, only to say it is different.&lt;br /&gt;Back to the first book, after reading sample pages from Amazon preview (mostly the table of contents and the index), together with all Amazon reviews, I realized what is happening here.&lt;br /&gt;&lt;br /&gt;So, the rest of this post is a (meta-?)review against this book - which is much less interesting, unless you were actually considering to buy it.&lt;br /&gt;&lt;br /&gt;&lt;hr /&gt;&lt;br /&gt;The author &lt;b&gt;does know&lt;/b&gt; what he is talking about, and spent lots of time polishing it, he's just totally&amp;nbsp;&lt;b&gt;unaware&lt;/b&gt;&amp;nbsp;that it is &lt;b&gt;completely unrelated&lt;/b&gt; to the title of the book, since&amp;nbsp;he has no experience in the field. Reading the introduction after reading the above quote is enlightening -&amp;nbsp;the author mentions being poor (page xvii), and his experience (described at&amp;nbsp;page iv), like writing CASE tools in Java, is totally unrelated - if he couldn't get a better job, I'd say Norvig's quote is exactly about him. And&amp;nbsp;after reading this,&amp;nbsp;the mention he makes about when "he used to disable paging and run on pure SDRAM" smells of a lamer wanting to show off (in other contexts, it could be just a joke, I know).&lt;br /&gt;&lt;br /&gt;The author is just trying to learn by himself how to implement an emulator, and writing a diary on this.&amp;nbsp;If you care about real Virtual Machines (Parrot, the JVM, .NET, and so on), you need entirely different material. Say, JIT compilation. Other reviews mention some more points which are missing, but none of them had a real introduction to the field, so they are not aware of how much else is not in the book. Actually, maybe he knows how a VM looks from outside, but his attempts to move in that direction (like support for multithreading) look quite clumsy - he talks about that and then does not manage to implement it.&lt;br /&gt;&lt;br /&gt;Finally, the author seems to be an assembler programmer who is programming assembler in C++. As we remember,&amp;nbsp;&lt;a href="http://www.pbm.com/%7Elindahl/real.programmers.html"&gt;it is famously known that&lt;/a&gt; "the determined Real Programmer can write Fortran programs in any language", and it is still true with Assembler. Things like manual loop unrolling on debug statements (mentioned in reviews) are quite funny.&lt;br /&gt;&lt;br /&gt;In the end, I'd recommend this book to nobody - it might contain some interesting stuff about the actual topic, asa acknowledged by some reviews, but I would not buy a book because of this hope. Especially, not for who cares about Virtual Machines.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7357044393624548611-6949461704891995933?l=blaisorbladeprog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blaisorbladeprog.blogspot.com/feeds/6949461704891995933/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://blaisorbladeprog.blogspot.com/2010/07/pretending-you-know-what-youre-talking.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7357044393624548611/posts/default/6949461704891995933'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7357044393624548611/posts/default/6949461704891995933'/><link rel='alternate' type='text/html' href='http://blaisorbladeprog.blogspot.com/2010/07/pretending-you-know-what-youre-talking.html' title='Pretending you know what you&apos;re talking about - a book review'/><author><name>Paolo G. Giarrusso</name><uri>http://www.blogger.com/profile/04485097839438234853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7357044393624548611.post-3868915031396719146</id><published>2010-07-26T13:00:00.000-07:00</published><updated>2010-07-26T13:00:21.953-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Garbage collection'/><category scheme='http://www.blogger.com/atom/ns#' term='Conservative GC'/><category scheme='http://www.blogger.com/atom/ns#' term='JVM'/><category scheme='http://www.blogger.com/atom/ns#' term='CLR'/><title type='text'>JVM vs .NET CLR - a comparison between the VMs</title><content type='html'>I was reading a Java vs. F# comparison on &lt;a href="http://fsharpnews.blogspot.com/2010/05/java-vs-f.html"&gt;this post&lt;/a&gt;, and it ended up comparing the JVM vs. the CLR. It also tries to counter some points of &lt;a href="http://www.azulsystems.com/blog/cliff-click/2009-09-06-java-vs-c-performanceagain"&gt;a post by Cliff Click&lt;/a&gt;, but it does so in a bad way. That's why I try here to improve on that comparison.&lt;br /&gt;&lt;br /&gt;The three real limitations of the JVM, compared to the CLR, are the lack of:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;b&gt;tail calls&lt;/b&gt; (being addressed), which are important for functional languages running on top of the JVM, like Clojure and Scala&lt;/li&gt;&lt;li&gt;&lt;b&gt;value types&lt;/b&gt;: I was happy to read on that post that there's interest on that, and I already read this on Guy Steele's &lt;i&gt;&lt;a href="https://docs.google.com/viewer?url=http%3A%2F%2Flabs.oracle.com%2Ffeatures%2Ftenyears%2Fvolcd%2Fpapers%2F14Steele.pdf"&gt;Growing a Language&lt;/a&gt;.&lt;/i&gt;&lt;/li&gt;&lt;li&gt;&lt;b&gt;proper generics&lt;/b&gt;, without erasure:&amp;nbsp;problems were well-known when generics were introduced, but back-compatibility with binary libraries forced the current solution, so this one is not going to be solved.&lt;/li&gt;&lt;/ul&gt;&lt;div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;"&gt;As an additional, it seems to me that since Java and the JVM is not managed by a single company, but by the Java Community Process, addition of new features like these is much slower (but hopefully they are better designed).&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;b&gt;Combining generics with value types&lt;/b&gt; would &lt;b&gt;allow&lt;/b&gt; great memory (and thus performance) &lt;b&gt;savings&lt;/b&gt;: one could define &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;Pair&lt;a,b&gt;&lt;/a,b&gt;&lt;/span&gt; as a value type and then use an &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;ArrayList&lt;pair&gt;&lt;k,v&gt;&amp;gt;&lt;/k,v&gt;&lt;/pair&gt;&lt;/span&gt; as the backing storage for an hash table and pay no space overhead.&lt;br /&gt;&lt;br /&gt;An additional point for &lt;b&gt;dynamic languages&lt;/b&gt;, the lack of an &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;&lt;b&gt;invokedynamic&lt;/b&gt;&lt;/span&gt; primitive creates significant performance problems - for instance, Jython (a JITted language) is 2x slower than CPython (an interpreted language with a slow interpreter). Anecdotal evidence suggests that lack of support for inline caching is an important reason: namely, a reimplementation of Python on top of the JVM for a course project, which allowed inline caching, was much faster.&lt;br /&gt;&lt;br /&gt;About&amp;nbsp;&lt;b&gt;JVM &lt;/b&gt;vs&lt;b&gt; CLR JITs&lt;/b&gt;, discussing the quality of existing implementations:&amp;nbsp;Cliff Click mentioned old anecdotal evidence of the CLR JIT being slower because .NET is geared towards static compilation and not so much effort has been put into it. I guess that Click refers to some optimizations which are missing from the CLR. For instance, at some point in the past replacing &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;List&lt;/span&gt; (a class type) by &lt;span class="Apple-style-span" style="font-family: 'Courier New', Courier, monospace;"&gt;IList&lt;/span&gt; (an interface type), in C#, caused a 10x slowdown that a good JIT compiler is able to remove, as discussed in Click's blog post. Dunno if this still holds.&lt;br /&gt;&lt;br /&gt;Anyway, this comparison is about the JVM vs. the &lt;b&gt;Microsoft&lt;/b&gt; CLR implementation, &lt;b&gt;running only on Windows&lt;/b&gt;. &lt;b&gt;Mono&lt;/b&gt; is available for Linux, but it uses a partially conservative garbage collector based on Boehm's GC, and this gives really inferior results in some cases. The Boehm's authors claim &lt;a href="http://www.hpl.hp.com/personal/Hans_Boehm/gc/bounds.html"&gt;here&lt;/a&gt; that such cases can be easily fixed by changing the source program, but this is valid only when the application is written for this GC, not when you want to support programs written for an accurate GC.&lt;br /&gt;Some evidence about this, where the described worst case is implemented, is available&amp;nbsp;&lt;a href="http://flyingfrogblog.blogspot.com/2009/01/mono-22-still-leaks-memory.html"&gt;here&lt;/a&gt;.&lt;br /&gt;In the end, if you want to run over Linux, you have no real choice if you aim for the best and most robust performance, currently (as also suggested by &lt;a href="http://shootout.alioth.debian.org/fastest-programming-language.php?javasteady=on&amp;amp;java=on&amp;amp;csharp=on&amp;amp;calc=chart"&gt;this entry of the Language Shootout&lt;/a&gt;, but read their notes about the flawedness of such comparisons). We have to hope for improvements in Mono.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7357044393624548611-3868915031396719146?l=blaisorbladeprog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blaisorbladeprog.blogspot.com/feeds/3868915031396719146/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://blaisorbladeprog.blogspot.com/2010/07/jvm-vs-net-clr-comparison-between-vms.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7357044393624548611/posts/default/3868915031396719146'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7357044393624548611/posts/default/3868915031396719146'/><link rel='alternate' type='text/html' href='http://blaisorbladeprog.blogspot.com/2010/07/jvm-vs-net-clr-comparison-between-vms.html' title='JVM vs .NET CLR - a comparison between the VMs'/><author><name>Paolo G. Giarrusso</name><uri>http://www.blogger.com/profile/04485097839438234853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7357044393624548611.post-4535772887822216002</id><published>2010-06-12T09:33:00.000-07:00</published><updated>2010-06-12T09:33:47.867-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Programming as craftsmanship'/><title type='text'>Teach Yourself Programming in Ten Years</title><content type='html'>I came across this link by random browsing (Internet, you know?) and it's just wonderful.&lt;br /&gt;Just one quote, about the "Teach Yourself $Language_X in $small_number_of_days" books: "Bad programming is easy. Idiots can learn it in 21 days, even if they are dummies."&lt;br /&gt;&lt;br /&gt;&lt;a href="http://norvig.com/21-days.html"&gt;http://norvig.com/21-days.html&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7357044393624548611-4535772887822216002?l=blaisorbladeprog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blaisorbladeprog.blogspot.com/feeds/4535772887822216002/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://blaisorbladeprog.blogspot.com/2010/06/teach-yourself-programming-in-ten-years.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7357044393624548611/posts/default/4535772887822216002'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7357044393624548611/posts/default/4535772887822216002'/><link rel='alternate' type='text/html' href='http://blaisorbladeprog.blogspot.com/2010/06/teach-yourself-programming-in-ten-years.html' title='Teach Yourself Programming in Ten Years'/><author><name>Paolo G. Giarrusso</name><uri>http://www.blogger.com/profile/04485097839438234853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7357044393624548611.post-7934911209835294695</id><published>2010-06-08T19:31:00.000-07:00</published><updated>2010-06-09T13:58:15.382-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Meta Computer Science'/><title type='text'>Stereotypes, averages and benchmarks</title><content type='html'>&lt;div&gt;While reasoning on the idea of &lt;a href="http://unerasmusparticolare.blogspot.com/2010/06/looking-for-german-germans.html"&gt;stereotypes and how useful they are to understand a different culture&lt;/a&gt;, I realized that a stereotype, together with problems in using it, is at least as as bad as an average over a diverse set (here we'll ignore the fact that it's also a judgement by a culture on another culture, often presuming without reason that). And as we know, &lt;/div&gt;&lt;div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;And after realizing this, one sees connections with many issues in research and in Computer Science. For instance, averages of different benchmarks. It's often fine to benchmark two different algorithms on some data sets: if the algorithm domain is simple enough, the variation on different inputs is small, and the input can anyway be characterized by a small number of features. Think of sorting algorithms: the inputs you pass them can be random, Gaussian, ordered, reverse-ordered, and that's it more or less. OK, I'm oversimplifying a bit, I guess. But in other cases, the input space has a much higher number of dimensions.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;And then, even worse, benchmarks about programming languages. Not only we have all these problems about sampling the input space, but in this case the input spaces of two language implementations, for different languages, are not the same, and they don't match in a meaningful way: they are not isomorphic, just like spoken languages. In other words, there is not a unique way of translating a program, and when you do, there's no way to make the pieces match one-by-one. There are infinite ways to solve a problem in any Turing-complete language (and a not-so-small number of reasonable alternative), and an intrinsically different infinity of ways for another language. And maybe, your translation is too slow because it's not appropriate for that language, and you should write the same program in a more idiomatic style.&lt;/div&gt;&lt;div&gt;The same concept is expressed &lt;a href="http://shootout.alioth.debian.org/flawed-benchmarks.php"&gt;here&lt;/a&gt; in a less abstract way.&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;And in this situation, not only we have actual benchmarks about programming languages, but even performance myths for or against Java, C#, Python, C, and so on. I.e., &lt;i&gt;stereotypes&lt;/i&gt;, once again, about languages. And this time, mostly false.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;For instance, we could talk of people thinking that Java is slow and Python is more lightweight, while it's actually the other way around, as long as we speak of throughput. What those people think is only true about startup latency, and only partially about memory consumption (Python has prompter garbage collection because of refcounting, but its object layout could benefit some huge improvement). And now in this example, we see that not only the input space is high-dimensional, but that even the performance space cannot be characterized by a single number. To compare memory consumption, we need to give a function of the used memory for a given object set, for Java and for Python. And the object sets are, again, not isomorphic! We're over.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Trying to sum up too much any result is going to give us lies. We can't help it; we should stop asking for simple answers to hard questions, and for &lt;a href="http://en.wikipedia.org/wiki/No_Silver_Bullet"&gt;silver bullets&lt;/a&gt;, and for a lot of other easy things, and go instead to work hard and enjoy the results.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7357044393624548611-7934911209835294695?l=blaisorbladeprog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blaisorbladeprog.blogspot.com/feeds/7934911209835294695/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://blaisorbladeprog.blogspot.com/2010/06/stereotypes-averages-and-benchmarks.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7357044393624548611/posts/default/7934911209835294695'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7357044393624548611/posts/default/7934911209835294695'/><link rel='alternate' type='text/html' href='http://blaisorbladeprog.blogspot.com/2010/06/stereotypes-averages-and-benchmarks.html' title='Stereotypes, averages and benchmarks'/><author><name>Paolo G. Giarrusso</name><uri>http://www.blogger.com/profile/04485097839438234853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7357044393624548611.post-6126490297964620972</id><published>2010-06-08T17:16:00.000-07:00</published><updated>2010-10-08T15:03:53.875-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Garbage collection'/><category scheme='http://www.blogger.com/atom/ns#' term='Conservative'/><title type='text'>Why conservative garbage collection should be avoided</title><content type='html'>&lt;div&gt;Sometimes one still sees around conservative garbage collection. I do not know how common that is, but &lt;a href="http://mono-project.com/Compacting_GC"&gt;Mono&lt;/a&gt; does use it, as did many JS implementation in the pre-V8 era (according to V8 website). And that's really a pity!&lt;/div&gt;&lt;div&gt;A quick intro first: conservative GC means, by definition, that since you don't know which words are pointers and which are just random other stuff, you are &lt;i&gt;conservative&lt;/i&gt; and just assume they might all be pointers.&lt;/div&gt;&lt;div&gt;&lt;div&gt;There are two reasons why conservative GC is bad:&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;First, and most obvious: you might have false positives, so you keep in memory more objects than you should. I'm not really sure how common this is, and how relevant. But it's not the really bad thing. Two other problems come into play!&lt;/li&gt;&lt;li&gt;You may not write a compacting collector, i.e. one that moves objects to be consecutive in memory at garbage collection time. Such a collector allows memory allocation to be much faster than with malloc()/free(): if all allocated objects form a sequence, also free memory does. Then you know where you can get free memory.&lt;br /&gt;In contrast, malloc()/free() have to manage free lists of some kinds, which list the available blocks of free memory, and perform a search through them for a block of the appropriate size; the actual details of this vary, depending on the used algorithm, but it's anyway slower. And the fact that memory allocation is much faster with GC is one of the coolest things about it. Depending on the scenario, a program might even be faster with GC, and there are papers about that.&lt;/li&gt;&lt;li&gt;Another problem is that if you don't compact memory, you get memory fragmentation: free memory is fragmented in more small blocks that may be of little use when allocating a bigger object, and still may not be used by any other program on the system. It's theoretically possible that a substantial fraction of the used memory of your app is just wasted by fragmentation, even if it's not so easy for me to find a realistic scenario where this happens.&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7357044393624548611-6126490297964620972?l=blaisorbladeprog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blaisorbladeprog.blogspot.com/feeds/6126490297964620972/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://blaisorbladeprog.blogspot.com/2010/06/why-conservative-garbage-collection-is.html#comment-form' title='2 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7357044393624548611/posts/default/6126490297964620972'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7357044393624548611/posts/default/6126490297964620972'/><link rel='alternate' type='text/html' href='http://blaisorbladeprog.blogspot.com/2010/06/why-conservative-garbage-collection-is.html' title='Why conservative garbage collection should be avoided'/><author><name>Paolo G. Giarrusso</name><uri>http://www.blogger.com/profile/04485097839438234853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7357044393624548611.post-2253999899948444334</id><published>2010-05-04T10:07:00.000-07:00</published><updated>2010-09-10T07:45:12.501-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='GIL'/><category scheme='http://www.blogger.com/atom/ns#' term='reference counting'/><category scheme='http://www.blogger.com/atom/ns#' term='Go'/><category scheme='http://www.blogger.com/atom/ns#' term='Python'/><title type='text'>Refcounting and weirdness (in Python and Go)</title><content type='html'>&lt;div&gt;Reading Cliff Click's blog is always insightful. Thanks to his blog, I realized that a refcounted language would have a weird enough memory model, if it allowed real multithreading. Better yet: CPython folks still think there may be some way to remove the GIL and keep refcounting with its "nice properties", but here, we discover why they would get fundamentally braindead semantics.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Hey, this discussion could even be relevant about Google's new Go language, since they plan to use a (somehow smart) reference counting, as shown in their &lt;a href="http://golang.org/doc/devel/roadmap.html"&gt;roadmap&lt;/a&gt;. The below discussion however still refers to CPython, since I never studied the details of deferred reference counting schemes. And it seems that Recycler (the algorithm they want to implement) does not suffer of this problem, luckily. Still, it is probably still interesting to see the interplay of refcounting with other features. And this probably affects the reference counting scheme used in COM.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Let's suppose a version of CPython using refcounting for garbage collection, but without a Global Interpreter Lock, is produced somehow. I assume that a decently performing implementation would use atomic counters for refcounts, rather than protecting those counts with a lock. Let's call it "GIL-less refcounted CPython".&lt;/div&gt;&lt;div&gt;Now, clearing a reference in GIL-less refcounted CPython (i.e. setting it to null) would not be atomic. In fact, suppose T1 does this, in a situation where another thread T2 can access the pointer. T2 can read the pointer just before you clear it, and increment the refcount of the pointed-to object O1 just after T1 deleted O1 and its memory was reused for another object O2 (so no, you don't see a zero refcount, you just corrupt something unrelated). Possibly, those other threads may even try to modify O1 and end up modifying O2.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Quoting from &lt;a href="http://www.azulsystems.com/blog/cliff-click/2010-04-17-odds-n-ends-2"&gt;its post&lt;/a&gt;:&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-family: Helvetica,Arial,sans-serif; font-size: 11px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="font-size: 9pt; line-height: 13pt; margin: 0px; padding: 0px; text-align: left; text-decoration: none;"&gt;&lt;span class="Apple-style-span" style="font-family: Helvetica,Arial,sans-serif; font-size: 11px;"&gt;&lt;span style="font-size: 9pt; line-height: 13pt; margin: 0px; padding: 0px; text-align: left; text-decoration: none;"&gt;&lt;b style="font-size: 9pt; line-height: 13pt; margin: 0px; padding: 0px; text-align: left; text-decoration: none;"&gt;"Why is ref-counting hard to use in managing concurrent* structures?"&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="font-size: 9pt; line-height: 13pt; margin: 0px; padding: 0px; text-align: left; text-decoration: none;"&gt;&lt;span class="Apple-style-span" style="font-family: Helvetica,Arial,sans-serif; font-size: 11px;"&gt;&lt;br /&gt;The usual mistake is to put the count in the structure being managed. The failing scenario is where one thread gets a pointer to the ref-counted structure at about the same time as the last owner is lowering the count and thus preparing to delete. Timeline: T1 gets a ptr to the ref-counted structure. T2 has the last read-lock on the structure and is done with it. T2 lowers the count to zero (T1 still holds a ptr to the structure but is stalled in the OS). T2 frees the structure (T1s pointer is now stale). Some other thread calls malloc and gets the just-freed memory holding the structure. T1 wakes up and increments where the ref-count used to be (but is now in some other threads memory).&lt;/span&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: Helvetica,Arial,sans-serif; font-size: 11px;"&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Since in CPython refcounts are embedded in objects (and it can't be done in other ways), this problem applies.&lt;/div&gt;&lt;div&gt;*I think he really means, in the title, lockless structures, otherwise you can just use a lock to clear the pointer and free the object atomically wrt. readers. And that's the way you'd solve this in CPython: protect any pointer reachable by other threads with a lock.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;While the above reasoning just says "hey, if you keep refcounting, removing the GIL will give weird semantics", there is more to notice. Namely, the Python program would probably segfault, or experience undefined behavior. And that's not supposed to happen in a managed language, no matter how buggy is your script, unlike in C. I.e. Python is supposed to be a safe language.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Contrast this with the Java Memory Model. The JMM guarantees that reference updates are atomic even on 64bit platforms, so clearing a pointer is always atomic. Additionally, concurrency mistakes never cause totally undefined behavior.&lt;/div&gt;&lt;div&gt;This is inherited even by the Jython memory model, and in absence of documentation, Jython users know they can rely on this both on Jython and on CPython. Our GIL-less refcounted CPython would break this.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The same scenario could also happens when making the reference point to a different object, but doing this without a lock is not always safe. For instance, to make sure that "this.o = new C();" is safe, the field 'o' must be volatile, or the access must be locked, to ensure that the writes initializing the new object are visible before the write to the field 'o'.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7357044393624548611-2253999899948444334?l=blaisorbladeprog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blaisorbladeprog.blogspot.com/feeds/2253999899948444334/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://blaisorbladeprog.blogspot.com/2010/05/refcounting-and-weirdness-in-python-and.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7357044393624548611/posts/default/2253999899948444334'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7357044393624548611/posts/default/2253999899948444334'/><link rel='alternate' type='text/html' href='http://blaisorbladeprog.blogspot.com/2010/05/refcounting-and-weirdness-in-python-and.html' title='Refcounting and weirdness (in Python and Go)'/><author><name>Paolo G. Giarrusso</name><uri>http://www.blogger.com/profile/04485097839438234853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7357044393624548611.post-5026783504909891738</id><published>2009-01-07T14:54:00.000-08:00</published><updated>2010-05-04T10:09:20.588-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='GIL'/><category scheme='http://www.blogger.com/atom/ns#' term='Python'/><title type='text'>Python VM: refcounting and the GIL</title><content type='html'>What has been more surprising for me, when investigating about Python history, was that real multithreading is not supported. Better said, if you create multiple thread, they cannot run concurrently - the interpreter allows just one of them to run most of the time, because all the threads hold the Global Interpreter Lock most of the time.&lt;br /&gt;&lt;br /&gt;As discussed by &lt;a href="http://www.python.org/doc/faq/library/#can-t-we-get-rid-of-the-global-interpreter-lock"&gt;an official FAQ entry&lt;/a&gt;, removal of the GIL was attempted, but it gave a big slowdown (2x on Windows platforms, more on Linux, which was slower for locking at that time; NPTL probably fixed that). I don't remember any profiling results, but the discussion on the python-dev ML focused on what I think is the right problem: updating object refcounts in an atomic way. Luckily you don't need to acquire a lock for that, atomic operations suffice. But still, that's the biggest cost, and it has such a big impact. How can other languages (like Java) implement multithreading?&lt;br /&gt;&lt;br /&gt;The problem is that CPython is stuck with using the slow technique of reference counting instead of fast, modern, real GCs, so removing the GIL for CPython makes it twice as slow. Fine-grained threading is slightly slower, but not by that factor. Atomic manipulation of reference counts is what killed performance of GIL-free CPython (I can't provide benchmarks for this), and reference counting already slows down Python.&lt;br /&gt;&lt;br /&gt;Python developers still defend reference counting for its improved locality, which can improve performance; the reality is that the advantages of reduced memory usage and relative ease of implementation are outweighed by the caused slowdown, as shown by years of research and by practice of most current implementations.&lt;br /&gt;&lt;br /&gt;And even the ease of implementation issue, for extension developers, is debatable; I would like to compare Python C extension API with the one of OcaML, which has been described as powerful and easy to use by some of my colleagues, and is thought for usage of real garbage collector, but I didn't have time for that yet.&lt;br /&gt;&lt;br /&gt;Memory usage is the _only_ real drawback of garbage collectors: to get good performance (even better than C explicit memory management) the heap size must be bigger, so that the collector is not invoked too often, and can reclaim lots of garbage for each scan.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7357044393624548611-5026783504909891738?l=blaisorbladeprog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blaisorbladeprog.blogspot.com/feeds/5026783504909891738/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://blaisorbladeprog.blogspot.com/2009/01/python-vm-refcounting-and-gil.html#comment-form' title='4 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7357044393624548611/posts/default/5026783504909891738'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7357044393624548611/posts/default/5026783504909891738'/><link rel='alternate' type='text/html' href='http://blaisorbladeprog.blogspot.com/2009/01/python-vm-refcounting-and-gil.html' title='Python VM: refcounting and the GIL'/><author><name>Paolo G. Giarrusso</name><uri>http://www.blogger.com/profile/04485097839438234853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7357044393624548611.post-3636104703212702498</id><published>2009-01-06T21:37:00.000-08:00</published><updated>2010-05-04T10:09:37.053-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='GIL'/><category scheme='http://www.blogger.com/atom/ns#' term='Python'/><title type='text'>Python VM simplicity</title><content type='html'>It has been quite fun, while studying for the Virtual Machines course with Lars Bak (the head designer of Google V8) in Aarhus University, and working for the project, to dig in the archives of python-dev about Python history and its choices.&lt;br /&gt;&lt;br /&gt;For a VM developer, like for authors of optimizing compilers, the aim is to relieve developers from concerns about low-level optimizations, allowing them to concentrate on their programs and their high-level optimizations.&lt;br /&gt;&lt;br /&gt;It looks like Guido van Rossum has instead the concern to keep the Python interpreter (the main implementation, called CPython - other implementations exist) simple. So simple that a couple of groups in the course could prototype interpreters which are faster than the Python one. Obviously, support for some advanced features is missing, but at least my group tried to include everything having impact in the fast path.&lt;br /&gt;I'm not saying no optimization work is done on CPython, obviously, just complaining on the amount of it.&lt;br /&gt;&lt;br /&gt;And especially, I'm complaining about how braindead is to suggest developers who care about performance to write performance-critical code in C:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;That leads to more complexity for the users, because they have to give up simplicity or performance, or to switch to Jython (which is 2x slower than Python but supports thread) or to Java (which is anyway simpler than Python+C).&lt;/li&gt;&lt;li&gt;That leads to more maintainance costs for the Python VM itself, because the amount of extension code written in C is much bigger.&lt;/li&gt;&lt;li&gt;Also, having lots of independent developers writing extensions in C makes the transition costs for API changes even higher.&lt;/li&gt;&lt;/ul&gt;I want to elaborate on maintainance costs. One could think the tradeoff is just between the complexity of the Python VM core and of extensions. Actually, it's far worse. It's a tradeoff of manpower.&lt;br /&gt;There will likely be a few core developers working on the interpreter, and they're likely going to be the most experienced ones. And the core of an interpreter tends to stay small anyway.&lt;br /&gt;Instead, authors of extensions (especially independent ones) will likely have a more varied skill set. And extensions can be many more, i.e. much more code.&lt;br /&gt;&lt;br /&gt;It's like this also on the Linux kernel: while on the core exceptionally complex optimizations are accepted (when they do improve performance) and people still tend to get it right, there is much more driver code, much simpler, but often of poor quality, and causing most bugs.&lt;br /&gt;&lt;br /&gt;To show some numbers, in Python 2.7 SVN, I have 9.8M in Modules/ folder (C module code) and 1.3M in the Python folder (C interpreter code).&lt;br /&gt;Google V8, instead, writes as much as possible of the standard library in JavaScript, and saves the JIT-ed code to load it at startup.&lt;br /&gt;&lt;br /&gt;Finally, the implementation cost have been overestimated. Implementing a JIT, for instance, is not that complex. There are also possibilities to get a JIT from the interpreter, by basically pasting together the pieces of the interpreter that handle the various opcodes (see for instance "context threaded JIT", mentioned in this post about Apple Safari's new JavaScript VM:&lt;br /&gt;http://webkit.org/blog/214/introducing-squirrelfish-extreme/).&lt;br /&gt;&lt;br /&gt;A technique called "indirect threading" (having nothing to do with multithreading) adds at least some of that performance, and still it wasn't implemented (though &lt;a href="http://bugs.python.org/issue4753"&gt;this is maybe going to change&lt;/a&gt;).&lt;br /&gt;&lt;br /&gt;And finally, even a complete rewrite would not be as horrible as Python people think. Other big opensource projects are doing that all the time, or face extensive refactoring and changes of their core API quite often, like the Linux kernel; and also the Samba code has been undergoing a core some rewrite time ago.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7357044393624548611-3636104703212702498?l=blaisorbladeprog.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://blaisorbladeprog.blogspot.com/feeds/3636104703212702498/comments/default' title='Commenti sul post'/><link rel='replies' type='text/html' href='http://blaisorbladeprog.blogspot.com/2009/01/python-vm-simplicity.html#comment-form' title='0 Commenti'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7357044393624548611/posts/default/3636104703212702498'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7357044393624548611/posts/default/3636104703212702498'/><link rel='alternate' type='text/html' href='http://blaisorbladeprog.blogspot.com/2009/01/python-vm-simplicity.html' title='Python VM simplicity'/><author><name>Paolo G. Giarrusso</name><uri>http://www.blogger.com/profile/04485097839438234853</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
