{"id":74,"date":"2004-02-23T09:46:45","date_gmt":"2004-02-23T08:46:45","guid":{"rendered":"http:\/\/www.nobugs.org\/blog\/archives\/2004\/02\/23\/ocaml-assembly-output\/"},"modified":"2004-02-23T09:46:45","modified_gmt":"2004-02-23T08:46:45","slug":"ocaml-assembly-output","status":"publish","type":"post","link":"https:\/\/www.nobugs.org\/blog\/archives\/2004\/02\/23\/ocaml-assembly-output\/","title":{"rendered":"Ocaml assembly output"},"content":{"rendered":"<p>Having spend a lot of time recently <a href=\"http:\/\/www.datarescue.com\/idabase\/ida.htm\">looking<\/a> at the assembly generated by the MS C++ compiler and <a href=\"http:\/\/www.cormanlisp.com\/\">Corman lisp<\/a>, I was about to start investigating what style of assembly the <a href=\"http:\/\/caml.inria.fr\/\">ocaml compiler<\/a> generates for typical constructs.  Fortunately for me, some else has <a href=\"http:\/\/www.merjis.com\/richj\/computers\/ocaml\/tutorial\/ch11\/\">already got there first<\/a>.  It&#8217;s a pretty informative article, and goes a long way towards explaining why ocaml performs so well.  Well, actually, the real reason is that <a href=\"http:\/\/pauillac.inria.fr\/~xleroy\/\">Xavier<\/a> is one clever cookie.  <\/p>\n<p>I read one of his early reports on the <a href=\"http:\/\/caml.inria.fr\/ocaml\/papers.html\">ZINC system<\/a> when I was travelling around the world <a href=\"http:\/\/www.handhelds.org\/z\/wiki\/AndrewBirkett\">with iPAQ in tow<\/a>, and the major performance gain at that point, if I remember correctly, concerned avoiding unnecessary construction of closures for curried function.  So, even though function calls in ocaml are curried (ie. a &#8220;two argument function&#8221; is really a single argument function, which returns another single argument function) you don&#8217;t actually need to build up all the scaffholding for the intermediate function if you&#8217;re just going to immediately apply it to another value.  This stops you building lots of intermediate closures on the heap.  This was an innovation at the time, I imagine (in 1990).<\/p>\n<p>The article also describes the boxing scheme used in ocaml, which uses the bottom bit to indicate whether a bit of data is an integer, or a  pointer to a heap block.  If all your heap blocks are word aligned, the bottom bit is redundant anyway so this is a neat efficient trick.  [I know a few other unnamed people who should recognise such bittwiddling tomfoolery too \ud83d\ude09 ]<\/p>\n<p>Last week at work, I started rewriting a small bit of code because I knew that there was a more elegant (and therefore more likely to be correct and maintainable) way to express what it was doing.  Unfortunately, when I started editing the code I realised that I was thinking in ocaml!  The &#8220;elegant way&#8221; required ocaml-style variants and pattern matching, but I was coding in C++.  Eep!  The <a href=\"http:\/\/www.boost.org\/doc\/html\/variant.tutorial.html#variant.tutorial.basic\">closest C++ equivalent<\/a> is almost a joke in constrast to the ocaml version.<\/p>\n<p>Finally, to end this ocaml praise session, the internals of the compiler are elegant and clean.  The various sections of the compiler are split out using ocaml&#8217;s powerful module facilities, and the functional style of programming (ie. minimal use of state) makes understanding the code a lot easier.  By contrast, the internals of gcc are a hideous mess.  Actually no, the internals of gcc *are* a hideous mess, period.<\/p>\n<p>Mu, I&#8217;m somewhat paralysed with indecision regarding where I want to go with computer tools.  I have so many ideas and things I want to try out, and I&#8217;ve seen so many great ideas consigned to the historical bit bucket.  And also, despite the impression which this programming-only-blog might convey, there&#8217;s a million and one other non-computer things which I want to spend my time on.  I think at some point I need to lower my ambition and focus on improving one particular thing, rather than riding along on a cascade of new ideas.  But it&#8217;s annoying, because every day I see tools which I consider to be primitive and backwards compared to what is possible.  I want to &#8220;do an Alan Kay&#8221; and burn the disks &#8211; look around, take what is good, and throw the rest away. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Having spend a lot of time recently looking at the assembly generated by the MS C++ compiler and Corman lisp, I was about to start investigating what style of assembly the ocaml compiler generates for typical constructs. Fortunately for me, some else has already got there first. It&#8217;s a pretty informative article, and goes a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-74","post","type-post","status-publish","format-standard","hentry","category-general"],"_links":{"self":[{"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/posts\/74","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/comments?post=74"}],"version-history":[{"count":0,"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/posts\/74\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/media?parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/categories?post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nobugs.org\/blog\/wp-json\/wp\/v2\/tags?post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}