<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Talk Like A Duck: Tag self</title>
    <link>http://talklikeaduck.denhaven2.com/articles/tag/self</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>In Ruby, it's not the dog, it's the tricks!</description>
    <item>
      <title>Ruby Extensions vs. Smalltalk Primitives</title>
      <description>&lt;p&gt;One of the ways in which Ruby differs from Smalltalk is in how much of the implementation is buried in C, which forms a barrier for deep understanding.&lt;/p&gt;
&lt;p&gt;For the purposes of this article, I&amp;#8217;m going to use the term extension a little loosely to refer to both core library and extension code written in C.&lt;/p&gt;
&lt;p&gt;For example, in Ruby, much of the code which implements core classes like Array, is implemented by C code.  This is good for performance, but for those with a need or desire to grok the code, not so much. In contrast, Smalltalk has a large brigade of Collection classes which are written in Smalltalk.&lt;/p&gt;
&lt;p&gt;This is not to say that Smalltalk doesn&amp;#8217;t use the equivalent of extensions. Smalltalk calls them primitives.&lt;p&gt;There are some interesting differences which might be interesting for the Ruby Core team to ponder, if they haven&amp;#8217;t already.&lt;/p&gt;

&lt;h2&gt;Smalltalk primitives&lt;/h2&gt;
&lt;p&gt;In Smalltalk-80 and it&amp;#8217;s descendants, primitive methods are implemented in a low-level languages like C or Assembler. They are attached to normal Smalltalk methods by a special syntax.  For example, here&amp;#8217;s a simppet from the Smalltalk-80 Blue Book, this is a method definition for the + method in SmallInteger (Smalltalks analogue to Fixnum in Ruby:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_smalltalk "&gt;1: + addend
2:   &amp;lt;primitive: 1&amp;gt;
3:   ^ super + addend&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The Smalltalk compiler turns method code into CompiledMethod objects which contain the bytecodes implementing the smalltalk code along with other information such as where to find temporary variables.  In the case of a method such as the bytecodes would be something like:&lt;/p&gt;
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_asm "&gt;   push self
   push addend
   send_super #+, 1  // invoke the inherited + method with 1 argument 
   return_top_of_stack&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;These are the bytecodes to implement line 3.&lt;/p&gt;
&lt;p&gt;The effect of line 2 is to mark the compiled method as being associated with a numbered primitive.  A Ruby implementation of a Smalltalk VM might have code like this.
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="constant"&gt;Module&lt;/span&gt; &lt;span class="constant"&gt;VM&lt;/span&gt;

  &lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;CompiledMethod&lt;/span&gt;

  &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;execute&lt;/span&gt;
      &lt;span class="keyword"&gt;begin&lt;/span&gt;
        &lt;span class="keyword"&gt;return&lt;/span&gt; &lt;span class="ident"&gt;exec_prim&lt;/span&gt; &lt;span class="keyword"&gt;if&lt;/span&gt; &lt;span class="ident"&gt;has_prim?&lt;/span&gt;
      &lt;span class="keyword"&gt;rescue&lt;/span&gt; &lt;span class="constant"&gt;PrimitiveFailed&lt;/span&gt;
      &lt;span class="keyword"&gt;end&lt;/span&gt;
      &lt;span class="keyword"&gt;return&lt;/span&gt; &lt;span class="constant"&gt;VM&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;interpret&lt;/span&gt;&lt;span class="punct"&gt;(&lt;/span&gt;&lt;span class="ident"&gt;byte_codes&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;    &lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;So the idea is that, if a compiled method has an associated primitive method the VM executes it. If it succeeds then that&amp;#8217;s the end of this method invocation. If it fails then execution falls through to interpret the byte codes.&lt;/p&gt;
&lt;p&gt;In the case of the method we are considering the primitive suceeds if the argument is also a SmallInteger, and the result doesn&amp;#8217;t overflow othewise it fails.  This allows primitives to quickly handle the 80% cases, and fall back on high-level smalltalk code for the more complicated cases.&lt;/p&gt;
&lt;h2&gt;User Primitives&lt;/h2&gt;
&lt;p&gt;Later Smalltalk implementations such as Smalltalk/V and &lt;span class="caps"&gt;IBM&lt;/span&gt;/Smalltalk allowed user written primitives. These were usually referenced by namerather than a number.  The same flow allowed fallback to Smalltalk code on failure of a user primitive, either for error recovery, or for balancing implementation complexity against performance.&lt;/p&gt;
&lt;h2&gt;Ruby Extensions&lt;/h2&gt;
&lt;p&gt;In contrast, Ruby methods are either wholly normal Ruby code, or wholly extension methods written in C functions registered using ruby api calls like rb_define_method. These methods are somewhat invisible to the ruby programmer.  This can be confusing, particularly when you are trying to read code which is partially implemented in C and partially in Ruby.  Just this morning I was trying to debug somebody else&amp;#8217;s code which was using the eventmachine gem.  Looking at the source for eventmachine showed methods being invoked with no definition visible in the ruby source.  These methods actually live in a C extension to the class.  
&lt;h2&gt;Could Ruby Use Something like Smalltalk&amp;#8217;s Primitive Methods?&lt;/h2&gt;
&lt;p&gt;It might be nice if this could be made visible using a mechanism like Smalltalk&amp;#8217;s primitive not only to document such cases, but as a way to do th ekind of complexity/performance balancing I&amp;#8217;ve described.&lt;/p&gt;
&lt;p&gt;It might be interesting to think about combining this with the ideas behind &lt;a href="http://www.zenspider.com/ZSS/Products/RubyInline/"&gt;ruby-inline&lt;/a&gt;, and to allow the primitives to be written in-line as well.  The difference I would see would be the introduction of the idea of failure/recovery and better support for usage of the ruby.h api inside the inlined primitives.&lt;p&gt;
&lt;p&gt;Just a thought.&lt;/p&gt;
&lt;h2&gt;Postscript &amp;#8211; Related Technology&lt;/h2&gt;
&lt;p&gt;Although I consider the Smalltalk primitive idea as something which might be added to Ruby, Smalltalk and related languages have added more modern techniques for moving the implementation of the core up into the higher-level language.
&lt;p&gt;&lt;a href="http://www.squeak.org"&gt;Squeak smalltalk&lt;/a&gt;Uses a language called slang (not tobe confused with s-lang), as the source language for the Squeak VM.  Slang has a Smalltalk-like syntax but it is easily translated to C. Squeak user primitives are written in slang as plug-ins.&lt;/p&gt;
&lt;p&gt;Evan Phoenix&amp;#8217;, &lt;a href="http://rubini.us/"&gt;Rubinius&lt;/a&gt; started out as a Ruby VM being written in Ruby or a slang-like language with a ruby-like syntax, but it seems to have changed tack to use a handcoded port of Evan&amp;#8217;s earlier Ruby code to C.  This ported VM is called Shotgun.  Shotgun seems to be taking an &lt;a href="http://rubini.us/pages/library"&gt;approach quite similar to the original Smalltalk-80 numbered primitives&lt;/a&gt; to implement the functions of the ruby standard library. I don&amp;#8217;t beleive that they are using the primitive failure with fallback idea though.&lt;/p&gt;
&lt;p&gt;Java has the Java Native Interface (or &lt;span class="caps"&gt;JNI&lt;/span&gt;) which is similar to ruby&amp;#8217;s extensions in that it provides a c-API to the &lt;span class="caps"&gt;JVM&lt;/span&gt;.  It differs from Ruby in that Java classes must have declarations of any native functions they provide and are responsibole for loading the load library containing the binary. &lt;/p&gt;
&lt;p&gt;I must confess that it&amp;#8217;s been a few years since I stopped keeping up with Java evolution, so there may be new things in Java in this area.&lt;/p&gt; 
&lt;p&gt;An interesting historical sidenote is that the original VisualAge for Java which was written before the advent of &lt;span class="caps"&gt;JNI&lt;/span&gt; used what was called the &amp;#8220;UVM&amp;#8221; or Universal Virtual Machine.  This was a version of the &lt;span class="caps"&gt;IBM&lt;/span&gt; Smalltalk VM extended to support both Smalltalk and Java bytecodes.  The &lt;span class="caps"&gt;UVM&lt;/span&gt; used Smalltalk as the language in which Java native methods were written.  When Sun came out with the &lt;span class="caps"&gt;JNI&lt;/span&gt;, with requirements for C language primitives, VA/Java moved from a Smalltalk VM to either the Sun or &lt;span class="caps"&gt;IBM&lt;/span&gt; JVMs.
&lt;/p&gt;
&lt;p&gt;After Dave Ungar cut his teeth (and earned his PhD) working on the &lt;span class="caps"&gt;UCB&lt;/span&gt; implementation of Smalltalk-80, he took on the challenge of implementing a dynamic language completely in itself in the aptly named self language.  The philosophy behind self abhored statically optimized techniques, including prmitives written in a low-level language, in favor of dynamically optimizing code at run-time.  This was the genesis of the &lt;span class="caps"&gt;JIT&lt;/span&gt; and HotSpot approaches commonly used in Java.  It was also the basis of the Strongtalk project which saw members of Dave&amp;#8217;s self team taking some of the ideas from Self back to a Smalltalk implementation.</description>
      <pubDate>Mon, 04 Jun 2007 10:16:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:00b2fa56-26ef-458d-b4c1-1de6a7f62e8b</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2007/06/04/ruby-extensions-vs-smalltalk-primitives</link>
      <category>ruby</category>
      <category>smalltalk</category>
      <category>smalltalkideas</category>
      <category>self</category>
      <category>rubinius</category>
      <category>squeak</category>
    </item>
    <item>
      <title>Boolean Implementations, Ruby, Smalltalk and self</title>
      <description>&lt;p&gt;The topic of defining a new ruby class which could have instances that, like false and nil were seen as a boolean which was not true, just came up on ruby-talk.&lt;/p&gt;


	&lt;p&gt;This has come up before, and it turns out that in Ruby being untrue is reserved to these two specific instances.&lt;/p&gt;


The boolean test is pretty deeply engrained into the implementation of ruby.  The actual test seems to be defined in the &lt;span class="caps"&gt;RTEST&lt;/span&gt; macro in ruby.h
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_C "&gt;#define Qnil   ((VALUE)4)
#define RTEST(v) (((VALUE)(v) &amp;amp; ~Qnil) != 0)&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Which means that any object whose reference &lt;span class="caps"&gt;VALUE&lt;/span&gt; has any bit set other than the 3rd &lt;span class="caps"&gt;LSB&lt;/span&gt; is true.&lt;/p&gt;


	&lt;p&gt;In ruby, the control flow statements like &amp;#8216;if&amp;#8217; aren&amp;#8217;t messages, but are &amp;#8216;compiled&amp;#8217; into a direct test and conditional branch.&lt;/p&gt;


Even Smalltalk, which defined even if/then/else as a message, e.g.
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;    booleanValue ifTrue:[Transcript show:'true'] ifFalse: [Transcript show:'false']&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
Tended to cheat in the implementation&amp;#8230;

	&lt;p&gt;In most Smalltalk implementations #ifTrue;ifFalse: and its ilk are never sent, but are, like in ruby, compiled into test and branch code.  Some implementations might have had a fallback if booleanValue wasn&amp;#8217;t actually a boolean, but &lt;span class="caps"&gt;IIRC&lt;/span&gt; most would trigger a MustBeBoolean exception.&lt;/p&gt;


	&lt;p&gt;By the way, those [...] are the Smalltalk analog to ruby&amp;#8217;s blocks.  In Smalltalk, blocks can be used as the value of any argument to a method, and a method could take more than one block argument.&lt;/p&gt;


	&lt;p&gt;But blocks are also another area where Smalltalk implementations tend to cheat a bit.&lt;/p&gt;


	&lt;p&gt;Smalltalk maintains the fiction that ifTrue:ifFalse: is really a message, and the methods in True and False are there to see:&lt;/p&gt;


in the True class
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;   ifTrue: trueBlock ifFalse; falseBlock
       ^trueBlock value&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
and in the False class
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;   ifTrue: trueBlock ifFalse: falseBlock
        ^falseBlock value&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

The sending the message value to a Smalltalk block is analogous to sending call to a Proc in ruby, although the actuall message varies with the arity of the block.  In the case of ifTrue:ifFalse, the block arguments don&amp;#8217;t really bedome block objects, they get compiled as in-line code.

 A lot of Smalltalkers ran across a head-scratcher when they got to the point of looking at the implementation of the value method in block which looks something like thi
&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;   value
      &amp;quot;return the result of evaluating the receiver&amp;quot;
      ^self value&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
This certainly looks like it should be an infinite loop, but it isn&amp;#8217;t.  The trick is that in almost all circumstances, the Smalltalk compiler compiles sending value to a block into either special bytecodes.  The only reason that the value method in block needs to be there is to handle cases like:

      aBlock perform:#value

	&lt;p&gt;where perform is the analog to ruby&amp;#8217;s send.&lt;/p&gt;


	&lt;p&gt;Smalltalk VM implementors like to say that it&amp;#8217;s okay to cheat as long as they don&amp;#8217;t get caught. MustBeBoolean is one area where they do get caught.&lt;/p&gt;


&lt;h2&gt;Pushing the Envelope&lt;/h2&gt;
It&amp;#8217;s quite a daunting task to fully implement a computation model where everything is a message and get reasonable performance. Smalltalk pushed this model quite far, but bowed to practicality in a few cases.

	&lt;p&gt;Ruby, although it&amp;#8217;s more dynamic than Smalltalk in a lot of ways, makes more concessions in it&amp;#8217;s current implementation.&lt;/p&gt;


	&lt;p&gt;Dave Ungar&amp;#8217;s &lt;a href="http://citeseer.ist.psu.edu/context/14801/0"&gt;self language&lt;/a&gt;, in the spirit of Nigel Tufnel, turned theknob up to eleven, and eschewed the pre-optimization of even if tests.  Instead, the self implementation developed and used sophistacated run-time type inferencing and analysis to generate optimized code at runtime by detecting the common cases of a boolean receiver and converting the message send to tests and branches, while avoiding supporting the less common case.&lt;/p&gt;


	&lt;p&gt;In common with Ruby, self had a more dynamic object model than Smalltalk, based on delegation rather than inheritance.  Something which engendered roaring &lt;a href="http://citeseer.ist.psu.edu/context/9545/0"&gt;debates in the early &lt;span class="caps"&gt;OOPSLA&lt;/span&gt; community&lt;/a&gt; over how delegation and inheritance are related.  The status of that discussion ca. 1989 is captured in &lt;a href="http://citeseer.ist.psu.edu/stein89shared.html"&gt;&amp;#8220;The Treaty of Orlando.&amp;#8221;&lt;/a&gt; which documented the observation that the differences were really a matter of perception and viewpoint.&lt;/p&gt;


	&lt;p&gt;The real contribution of self was setting the bar high in terms of the difficulty of implementing a simple and dynamic specification, and then jumping over it. The self team later applied the lessons they had learned to the implementation of Strongtalk, a Smalltalk VM which applied the techniques of the self implementation to both make Smalltalk more dynamic, and better performing.&lt;/p&gt;


	&lt;p&gt;Many of those sessons should be applicable to implementations of Ruby. I hope to see that unfold over time.&lt;/p&gt;</description>
      <pubDate>Tue, 10 Oct 2006 12:43:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:7d212280-75dc-4804-b031-b02120ba05d4</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2006/10/10/boolean-implementations-ruby-smalltak-and-self</link>
      <category>ruby</category>
      <category>smalltalk</category>
      <category>implementation</category>
      <category>self</category>
      <category>performance</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/50</trackback:ping>
    </item>
    <item>
      <title>Performance Anxiety</title>
      <description>&lt;p&gt;I've been meaning to write about Ruby performance for a while, and a recent blog post by an old friend and colleague, got me off my proverbial.&lt;/p&gt;

&lt;p&gt;The old friend is John Duimovich, who wrote about the relative performance of &lt;i&gt;C++&lt;/i&gt; and &lt;i&gt;Smalltalk&lt;/i&gt; and &lt;a href="http://duimovich.blogspot.com/2006/09/performance-is-not-optional.html"&gt;what that could mean for ruby.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;John's message is important for those who bemoan the performance of Ruby, and I plan to expand on that message in this and later posts to this blog, but first a few words about Mr. Duimovich.&lt;/p&gt;

&lt;h2&gt;Consider the source&lt;/h2&gt;
&lt;p&gt;In his day job, paraphrasing his self description John "works for IBM on &lt;i&gt;Java&lt;/i&gt; virtual machines and is the lead on the Eclipse tools project management commitee."&lt;/p&gt;

&lt;p&gt;But some of my readers might be interested in John's background.  John was for a very long time, the lead of the &lt;i&gt;Smalltalk&lt;/i&gt; and &lt;i&gt;Java&lt;/i&gt; virtual machine team at Object Technology International (OTI) dating from before the time it was acquired by IBM.  Among other things John was responsible for the development of embedded &lt;i&gt;Smalltalk&lt;/i&gt; virtual machines from OTI, which spawned the VM used in Smalltalk/V Mac, IBM Smalltalk (used in IBM/VisualAge), the 'Universal' Virtual machine which implemented Java on an extended Smalltalk VM, and which was used for the early releases of IBM/VisualAge for Java, and the J9 Java VM.  A good deal of what I know about implementing VMs comes from working, lunching, and bar-hopping with John.&lt;/p&gt;
&lt;p&gt;John had become OTI's Chief Technology Officer before OTI got assimilated into the IBMborg.&lt;/p&gt;

&lt;p&gt;John is a brilliant guy, with a great sense of humor. Two characteristics which seem to have been requirements for a job at OTI.  I'm still not sure how I ended up spending several years there.&lt;/p&gt;

&lt;h2&gt;Dynamically Typed Doesn't Need to Mean Slow&lt;/h2&gt;
&lt;p&gt;I encourage you to read John's blog post yourself, but to summarize; John ran across another blog item which gave a benchmark written in &lt;i&gt;C++&lt;/i&gt;, &lt;i&gt;Ruby&lt;/i&gt; and &lt;i&gt;Python&lt;/i&gt;. The &lt;i&gt;C++&lt;/i&gt; version runs in under 1/10 of the time needed for either the &lt;i&gt;Ruby&lt;/i&gt; or &lt;i&gt;Python&lt;/i&gt; versions.&lt;/p&gt;

&lt;p&gt;John duplicated the results on his machine, then decided to port the &lt;i&gt;Ruby&lt;/i&gt; version of the benchmark to &lt;i&gt;Smalltalk&lt;/i&gt;.  He then ran it using VisualAge Smalltalk.&lt;/p&gt;

&lt;p&gt;And the &lt;i&gt;Smalltalk&lt;/i&gt; version runs in the same time as the optimized C++ version!&lt;/p&gt;

&lt;p&gt;How can this be?&lt;/p&gt;

&lt;h2&gt;The Value of Pole Vaulting&lt;/h2&gt;
&lt;p&gt;Languages like &lt;i&gt;Smalltalk&lt;/i&gt; and &lt;i&gt;Self&lt;/i&gt; started from the position that a clean object-oriented language was more important than one which makes compromises to make efficient implementation obvious.&lt;/p&gt;

&lt;p&gt;Early implementations of &lt;i&gt;Smalltalk&lt;/i&gt; used obvious implementations of some features, which were 'fast enough' in many cases, but by no means fast.  Two areas which cried for improvement were method dispatch and garbage colection.  The obvious techniques were walking up the class hierarchy each time a method was needed, and relatively easy to implement GC techniques like reference counting, and mark-and-sweep.  Reference counting has a fairly high cost for each change of an object reference, and also has the drawback of leaking memory because cyclical references lead to garbage which is uncollectable.  Mark and sweep delays the overhead until storage is exhausted, but leads to more perceptible pauses when the application gets paused so that the housemaid cleans the room.&lt;/p&gt;

&lt;p&gt;Encountering (or having set) this high bar, various implementors of these languages found very clever techniques for both problems.  Dave Ungar made measurements of the lifespans of &lt;i&gt;Smalltalk&lt;/i&gt; objects and observed that most objects  died very shortly after being instantiated, with few living a long life.  This led to the invention of generational GC techniques, which quickly dispatched young dead objects, which are the vast majority.&lt;/p&gt;

&lt;p&gt;Method dispatching techniques of efficiently implemented dynamically typed languages tend to use clever caching algorithms which can get to what is probably the right method quickly, with a quick test to make sure that the right method was found.&lt;/p&gt;

&lt;p&gt;These dispatching techniques turn out to be faster than the virtual function pointer dispatching made possible by strongly-typed languages like &lt;i&gt;C++&lt;/i&gt;.  In fact, I've heard that more modern implementations of these languages have actually used a more dynamic method dispatch mechanism internally in order to &lt;b&gt;increase&lt;/b&gt; performance.&lt;/p&gt;

&lt;p&gt;Anoher implementation choice is how to represent executable code. Most efficient implementations use a combination of byte-code representation, and some form of just-in-time translation of byte-codes to machine code.  Just how to divide execution between byte-code and machine code is a complicated decision.  Back when DIgitalk first produced a version of Smalltalk/V for OS/2, they decided to eschew byte-codes entirely and generate 80286 machine code.  The reason was that they were tired of hearing complaints about &lt;i&gt;Smalltalk&lt;/i&gt; being an 'interpreted' language.&lt;/p&gt;

&lt;p&gt;The surprising result of this experiment was that the resulting implementation was slower.  Machine code was bigger, so it took longer to load, and caused more swapping.  These costs were paid whether the code in question was executed once or a million times.&lt;/p&gt;

&lt;p&gt;Again caching was the basis for getting the best of both worlds.  Peter Deutsch of Xerox, later ParcPlace, had introduced the notion of translating byte-codes to machine code into a cache during execution, David Ungar's implementation of &lt;i&gt;Self&lt;/i&gt; introduced the notion of using light-weight profiling techniques to avoid the overhhead of translating byte-coded methods which were infrequently executed.&lt;/p&gt;

&lt;p&gt;Another area which posed difficulties in implementation was control flow.  Smalltalk-80 defines all control flow as methods. Even primitive control flow constructs such as if (&lt;b&gt;ifTrue:&lt;/b&gt; in &lt;i&gt;Smalltalk&lt;/i&gt;) were &lt;i&gt;implemented&lt;/i&gt; as methods on Boolean classes.  This is one area where &lt;i&gt;Smalltalk&lt;/i&gt; implementations &lt;i&gt;cheated&lt;/i&gt; compiling such methods in to testing and branching byte-codes, and requiring the receivers to be boolean instances.&lt;/p&gt;

&lt;p&gt;&lt;i&gt;Self&lt;/i&gt; eschewed this &lt;i&gt;early optimization&lt;/i&gt;.  Ungar's team instead relied on run-time type inference in order to dynamicaly generate code which achieved the same or better performance when such a message was sent to a boolean without restricting other cases.&lt;/p&gt;

&lt;h2&gt;The Current State of Ruby Implementation&lt;/h2&gt;
&lt;p&gt;&lt;i&gt;Ruby&lt;/i&gt; performance today is surprisingly &lt;i&gt;acceptable&lt;/i&gt; for a wide range of uses.&lt;/p&gt;

&lt;p&gt;This is despite the fact that the implementation is relatively straightforward, almost to the point of being naive. In the current standard implementation of Ruby:
&lt;ul&gt;
&lt;li&gt;Method dispatch is done by walking up the 'class' hierarchy looking for methods in a hash table in each class/module.&lt;/li&gt;
&lt;li&gt;Garbage Collection is done by a simple mark and sweep algorithm.&lt;/li&gt;
&lt;li&gt;Executable code is represented by a parse tree which is executed by traversal.&lt;/li&gt;
&lt;/ul&gt;&lt;/p&gt;
&lt;p&gt;This is not meant to understate the achievements of Matz and the ruby developers. &lt;i&gt;Ruby&lt;/i&gt; as it is definitely usable for many production uses.&lt;/p&gt;

&lt;p&gt;The point is how much better &lt;i&gt;Ruby&lt;/i&gt; performance can get as the implementation matures.  A virtual machine, with byte-codes, and better GC is on the roadmap.  &lt;i&gt;Ruby&lt;/i&gt; virtual machines such as YARV, and JRuby are showing glimmers of the value of implementing &lt;i&gt;Ruby&lt;/i&gt; as a virtual machine.  If &lt;i&gt;Ruby&lt;/i&gt; continues to grow in acceptance, I've no doubt that other clever implementers with experience in efficently implementing dynamically typed languages will provide more implementations.&lt;/p&gt;

&lt;p&gt;My prediction is that the future will be so bright that we're going to have to wear (ruby colored) shades!&lt;/p&gt;</description>
      <pubDate>Tue, 26 Sep 2006 15:17:00 -0400</pubDate>
      <guid isPermaLink="false">urn:uuid:6db85990-2780-4ce7-91fd-b122e3398ce5</guid>
      <author>Rick DeNatale</author>
      <link>http://talklikeaduck.denhaven2.com/articles/2006/09/26/performance-anxiety</link>
      <category>ruby</category>
      <category>smalltalk</category>
      <category>smalltalk</category>
      <category>ruby</category>
      <category>self</category>
      <category>performance</category>
      <category>implementation</category>
      <trackback:ping>http://talklikeaduck.denhaven2.com/articles/trackback/39</trackback:ping>
    </item>
  </channel>
</rss>
