<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Building a Better Ruby</title>
	<atom:link href="http://betterruby.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://betterruby.wordpress.com</link>
	<description>Covering the developmnet of Rubinius, a new Ruby implementation and virtual machine</description>
	<pubDate>Wed, 16 Apr 2008 07:22:51 +0000</pubDate>
	<generator>http://wordpress.org/?v=MU</generator>
	<language>en</language>
			<item>
		<title>Rubinius on GitHub</title>
		<link>http://betterruby.wordpress.com/2008/04/11/rubinius-on-github/</link>
		<comments>http://betterruby.wordpress.com/2008/04/11/rubinius-on-github/#comments</comments>
		<pubDate>Fri, 11 Apr 2008 01:59:56 +0000</pubDate>
		<dc:creator>agardiner</dc:creator>
		
		<category><![CDATA[News]]></category>

		<category><![CDATA[git]]></category>

		<category><![CDATA[github]]></category>

		<guid isPermaLink="false">http://betterruby.wordpress.com/?p=31</guid>
		<description><![CDATA[With all the recent press given to Git and GitHub, I thought it worth mentioning that, while the main Rubinius Git repository continues to be hosted at git.rubini.us, there is now a post-commit hook that pushes all commits onto the Rubinius GitHub repository.
So you can now take advantage of the great GitHub features such as [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>With all the <a href="http://www.rubyinside.com/github-officially-launches-git-hosting-a-go-go-853.html">recent</a> <a href="http://tomayko.com/writings/the-thing-about-git">press</a> given to <a href="http://git.or.cz/">Git</a> and <a href="http://github.com/">GitHub</a>, I thought it worth mentioning that, while the main Rubinius Git repository continues to be hosted at <a href="http://git.rubini.us/?p=code;a=summary">git.rubini.us</a>, there is now a post-commit hook that pushes all commits onto the Rubinius GitHub <a href="http://github.com/evanphx/rubinius/tree">repository</a>.</p>
<p>So you can now take advantage of the great GitHub <a href="http://github.com/guides">features</a> such as news feeds, effortless forking, source code browsing etc against the Rubinius source. Neat!</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/betterruby.wordpress.com/31/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/betterruby.wordpress.com/31/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/betterruby.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/betterruby.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/betterruby.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/betterruby.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/betterruby.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/betterruby.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/betterruby.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/betterruby.wordpress.com/31/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/betterruby.wordpress.com/31/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/betterruby.wordpress.com/31/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=betterruby.wordpress.com&blog=2942765&post=31&subd=betterruby&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://betterruby.wordpress.com/2008/04/11/rubinius-on-github/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/agardiner-128.jpg" medium="image">
			<media:title type="html">agardiner</media:title>
		</media:content>
	</item>
		<item>
		<title>Shotgun Rewrite Underway</title>
		<link>http://betterruby.wordpress.com/2008/04/11/shotgun-rewrite-underway/</link>
		<comments>http://betterruby.wordpress.com/2008/04/11/shotgun-rewrite-underway/#comments</comments>
		<pubDate>Fri, 11 Apr 2008 01:31:52 +0000</pubDate>
		<dc:creator>agardiner</dc:creator>
		
		<category><![CDATA[News]]></category>

		<category><![CDATA[shotgun]]></category>

		<category><![CDATA[rewrite]]></category>

		<guid isPermaLink="false">http://betterruby.wordpress.com/?p=30</guid>
		<description><![CDATA[Some big changes are underway on the Rubinius VM at present: Shotgun is being completely rewritten! This change was brought about by some fairly significant rework required in order to change the behavior of argument evaluation in method calls.
Currently, Rubinius evaluates method arguments from right-to-left, whereas MatzRuby and JRuby evaluate arguments left-to-right. So code like [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Some <a href="http://git.rubini.us/?p=code;a=shortlog;h=refs/heads/cpp">big changes</a> are underway on the Rubinius VM at present: Shotgun is being completely rewritten! This change was brought about by some fairly significant rework <a href="http://rubinius.lighthouseapp.com/projects/5089/tickets/372-get-activesupport-2-0-tests-passing">required</a> in order to change the behavior of argument evaluation in method calls.</p>
<p>Currently, Rubinius evaluates method arguments from right-to-left, whereas MatzRuby and JRuby evaluate arguments left-to-right. So code like the following:</p>
<blockquote>
<pre>a = [1,2,3]
foo(a.shift, a.shift, a.shift)</pre>
</blockquote>
<p>evaluates to <tt>foo(1,2,3)</tt> in MatzRuby and JRuby, but to <tt>foo(3,2,1)</tt> in Rubinius.</p>
<p>While it is generally considered unwise to rely on argument evaluation order (languages such as C specify that <a href="http://www.embedded.com/story/OEG20020429S0037">argument evaluation order is undefined</a>, and at the discretion of the compiler writer), it turns out there is a significant base of Ruby code that does in fact depend upon this behavior, not the least being Rails&#8217; ActiveSupport.</p>
<p>As a result, it was decided to rework Rubinius to also evaluate method arguments in left-to-right order. This requires changes to both the compiler and to the Shotgun VM, since the order in which arguments are passed on the stack now needs to be reversed.</p>
<h2>From C to C++</h2>
<p>While making the necessary changes to Shotgun, a tipping point was reached, and Evan decided to bite the bullet and re-write Shotgun in C++. The reasons he gave for this decision were as follows:</p>
<ol>
<li><strong>Tests!</strong>: Shotgun had evolved from an initial prototype, and unlike the rest of the Rubinius code base, had very little in the way of test coverage. The substantial changes required to the VM internals to accommodate the argument order reversal, and the lack of tests to validate the changes, was the single biggest factor leading to the decision. The new VM aims to have 100% test coverage using <a href="http://cxxtest.sourceforge.net/">CxxTest</a>.</li>
<li><strong>Modularity:</strong> The opportunity provided by starting fresh, as well as the better code organisation capabilities (classes, namespaces, etc) provided by C++ mean that the new VM will be more modular, which should make it easier to extend and maintain.</li>
<li><strong>Better match between VM and Ruby semantics:</strong> The use of an object-oriented language for the VM provides a better semantic match to Ruby. Language support for method chaining, exceptions, and so forth mean that the VM implementation will more closely mirror the semantics of Ruby. This (combined with a cleaner architecture) should make it more understandable to Rubinius contributors, as well as potentially a better target for Garnet/Duby style code generation than C.</li>
<li><strong>STL:</strong> Many of the built-in types required by the VM (tuple, array, hash, list, etc) can be built on classes provided by the C++ Standard Template Library.</li>
<li><strong>Stronger typing: </strong>C++ is a stronger typed language than C, and this should help reduce problems such as the &#8220;Attempted field access on non-reference&#8221; errors that often occur when working on the VM.</li>
</ol>
<p>The decision to rewrite Shotgun was a big one, and will certainly set back progress a little in the short-term. However, the cleaner architecture, test coverage, and other advantages accruing from the change should pay-off substantially over time.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/betterruby.wordpress.com/30/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/betterruby.wordpress.com/30/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/betterruby.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/betterruby.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/betterruby.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/betterruby.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/betterruby.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/betterruby.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/betterruby.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/betterruby.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/betterruby.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/betterruby.wordpress.com/30/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=betterruby.wordpress.com&blog=2942765&post=30&subd=betterruby&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://betterruby.wordpress.com/2008/04/11/shotgun-rewrite-underway/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/agardiner-128.jpg" medium="image">
			<media:title type="html">agardiner</media:title>
		</media:content>
	</item>
		<item>
		<title>Building Rubinius in Ruby</title>
		<link>http://betterruby.wordpress.com/2008/04/02/building-rubinius-in-ruby/</link>
		<comments>http://betterruby.wordpress.com/2008/04/02/building-rubinius-in-ruby/#comments</comments>
		<pubDate>Tue, 01 Apr 2008 23:53:53 +0000</pubDate>
		<dc:creator>agardiner</dc:creator>
		
		<category><![CDATA[News]]></category>

		<category><![CDATA[turtles]]></category>

		<guid isPermaLink="false">http://betterruby.wordpress.com/?p=28</guid>
		<description><![CDATA[I had intended to write a post in the near future about why building Rubinius in Ruby was important - but I see today that Mathieu Martin has beaten me to it, with the first in a series of articles on Rubinius. In Part 1: Rubies all the way down, he makes the case for [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I had intended to write a post in the near future about why building Rubinius in Ruby was important - but I see today that <a href="http://programblings.com/">Mathieu Martin</a> has beaten me to it, with the first in a series of articles on Rubinius. In <a href="http://programblings.com/2008/04/01/rubinius-for-the-layman-part-1-rubies-all-the-way-down/">Part 1: Rubies all the way down</a>, he makes the case for building Rubinius in Ruby, setting out the pros and dispelling the myths about dynamic languages being too slow to self-host. It&#8217;s a great read, and a much more eloquent argument than I could have made.</p>
<p>I&#8217;m certainly looking forward to future articles in this series!</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/betterruby.wordpress.com/28/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/betterruby.wordpress.com/28/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/betterruby.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/betterruby.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/betterruby.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/betterruby.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/betterruby.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/betterruby.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/betterruby.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/betterruby.wordpress.com/28/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/betterruby.wordpress.com/28/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/betterruby.wordpress.com/28/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=betterruby.wordpress.com&blog=2942765&post=28&subd=betterruby&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://betterruby.wordpress.com/2008/04/02/building-rubinius-in-ruby/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/agardiner-128.jpg" medium="image">
			<media:title type="html">agardiner</media:title>
		</media:content>
	</item>
		<item>
		<title>How Rubinius SendSites Work - Part 2</title>
		<link>http://betterruby.wordpress.com/2008/04/01/how-rubinius-sendsites-work-part-2/</link>
		<comments>http://betterruby.wordpress.com/2008/04/01/how-rubinius-sendsites-work-part-2/#comments</comments>
		<pubDate>Tue, 01 Apr 2008 04:28:07 +0000</pubDate>
		<dc:creator>agardiner</dc:creator>
		
		<category><![CDATA[shotgun]]></category>

		<category><![CDATA[sendsite]]></category>

		<guid isPermaLink="false">http://betterruby.wordpress.com/?p=23</guid>
		<description><![CDATA[In part 1 of this post, we introduced the concept of Rubinius SendSites and looked at the Ruby class / C struct used to represent them; in part 2, we will be looking at the life-cycle of SendSite objects, and in particular, how they are used to optimise the method dispatch process.

SendSite Instantiation
The lifecycle of [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>In <a href="http://betterruby.wordpress.com/2008/03/19/how-rubinius-sendsites-work-part-1/">part 1</a> of this post, we introduced the concept of Rubinius <b>SendSites</b> and looked at the Ruby class / C struct used to represent them; in part 2, we will be looking at the life-cycle of SendSite objects, and in particular, how they are used to optimise the method dispatch process.</p>
<p align="center"><a href="http://betterruby.files.wordpress.com/2008/03/2006-09_035-london-night.jpg" title="2006-09_035-london-night.jpg"><img src="http://betterruby.files.wordpress.com/2008/03/2006-09_035-london-night.jpg" alt="2006-09_035-london-night.jpg" /></a></p>
<h2>SendSite Instantiation</h2>
<p>The lifecycle of a SendSite starts with instantiation, which happens in one of two ways:</p>
<ul>
<li>when Ruby source is compiled to bytecode, and</li>
<li>when an .rbc (Rubinius compiled) file is unmarshaled.</li>
</ul>
<h4>Compilation</h4>
<p>SendSite objects are initially created during the bytecode compilation process; at all points in the compiled bytecode where a method call exists, a SendSite object is created (using <b>SendSite.new</b>) for that message send site (see <b>#send</b>, <b>#send_with_block</b>, <b>#send_with_register</b>, and <b>#send_super</b> in <a href="http://git.rubini.us/?p=code;a=blob;f=lib/compiler/generator.rb;h=03f76546d3ba0e9499dff8af7f9426add9d9b8af;hb=HEAD">lib/compiler/generator.rb</a>). The resulting SendSite object is stored in the CompiledMethod <a href="http://betterruby.wordpress.com/rubinius-glossary.html/#literals_tuple">literals tuple</a>, and the index of this SendSite literal is inserted into the bytecode as the argument to the send_<i>*</i> opcode.</p>
<p>By way of example, take a look at the following simple hello_world.rb script:</p>
<blockquote>
<pre>puts "hello world"
puts "bye!"</pre>
</blockquote>
<p>Using the Rubinius debugger, we can examine the bytecode that is generated for this script (and which will be saved in compiled form as hello_world.rbc):</p>
<blockquote>
<pre>
ads@ads-kubuntu:~/rubinius$ shotgun/rubinius -debug hello_world.rb[Debugger activated]
rbx:debug&gt; d 0 25
   Bytecode instructions [0-25] in compiled method __script__:
           # line 1:       puts &#8220;hello world&#8221;
  =&gt; 0000: push_literal    &#8220;hello world&#8221;
     0002: string_dup
     0003: push_self
     0004: set_call_flags  1
     0006: send_stack      #&lt;SendSite:0&#215;39 name=puts hits=0 misses=0&gt;, 1
     0009: pop
           # line 2:       puts &#8220;bye!&#8221;
     0010: push_literal    &#8220;bye!&#8221;
     0012: string_dup
     0013: push_self
     0014: set_call_flags  1
     0016: send_stack      #&lt;SendSite:0&#215;41 name=puts hits=0 misses=0&gt;, 1
     0019: pop
     0020: push_true
     0021: sret</pre>
</blockquote>
<p>Here we can see two SendSite objects used on the two calls to the <b>puts</b> method. Notice in particular that each send instruction has its own distinct SendSite object, despite the same selector (puts) being used.</p>
<h4>Unmarshaling .rbc files</h4>
<p>When a Ruby source (.rb) file is first compiled, a corresponding .rbc file is also created; this compiled file will be used instead of the .rb file each subsequent time the source file is run or required, provided recompilation is not necessary. So the other place where SendSite objects can be instantiated is in the unmarshal_sendsite function in <a href="http://git.rubini.us/?p=code;a=blob;f=shotgun/lib/cpu_marshal.c;h=00b955e4821989c16632526a673ed5be5c961fdb;hb=HEAD">shotgun/lib/cpu_marshal.c</a>.</p>
<h4>send_site_create</h4>
<p>Ultimately, whether created via compilation or unmarshaling, a SendSite object is created via a call to <b>send_site_create</b> in <a href="http://git.rubini.us/?p=code;a=blob;f=shotgun/lib/sendsite.c;h=afbbb61fd899c703cb93169591306704eea830bb;hb=HEAD">shotgun/lib/sendsite.c</a>; (the <b>SendSite.new</b> Ruby method calls <b>SendSite.create</b>, which is implemented as a Rubinius <a href="http://betterruby.wordpress.com/rubinius-glossary.html/#primitive">primitive</a>: a Ruby method whose body is implemented in C code, rather than Ruby).</p>
<p>The C function <b>send_site_create</b> initializes the SendSite struct, looking up the Selector from the method name, and setting the SendSite lookup function to <b>_cpu_ss_basic</b>, which is found in <a href="http://git.rubini.us/?p=code;a=blob;f=shotgun/lib/cpu_instructions.c;h=b064acad3ae2615dee5462bc906c6608a3990e00;hb=HEAD">shotgun/lib/cpu_instructions.c</a>. At this point, our SendSite is ready for action.</p>
<h2>SendSites and Method Dispatch</h2>
<p><i>(Note: The following description of the method dispatch process is likely to change in future, although the general principles should remain the same).</i></p>
<p>When a method call is performed, via the execution of a send_* instruction, the SendSite lookup function is used to determine what actions are taken to dispatch the method. The following code shows how the lookup function is used as a <a href="http://en.wikipedia.org/wiki/Function_pointer">function pointer</a>, and is lifted from <b>cpu_send_message</b> in <a href="http://git.rubini.us/?p=code;a=blob;f=shotgun/lib/cpu_instructions.c;h=b064acad3ae2615dee5462bc906c6608a3990e00;hb=HEAD">shotgun/lib/cpu_instructions.c</a>:</p>
<blockquote>
<pre>  ss = SENDSITE(msg-&gt;send_site);
  msg-&gt;state = state;
  msg-&gt;c = c;
  msg-&gt;name = ss-&gt;name;
  ss-&gt;lookup(msg);</pre>
</blockquote>
<p>The very first time a SendSite is used, the lookup function in the SendSite struct is set to <b>_cpu_ss_basic</b> as we saw above. This is just one of a number of different functions that can be used by a SendSite as the send site lookup function.</p>
<h4>_cpu_ss_basic</h4>
<p>This is the slow path lookup function that uses no optimisations to dispatch a method. It calls <b>cpu_lookup_method</b> to find the method on the receiver (navigating up the superclass/metaclass hierarchy until it finds the method or falls back to method_missing), determines if the method is handled by method_missing or not, and then does a very important thing: it <i>patches</i> (modifies) the SendSite lookup function using either <b>cpu_patch_mono</b> or <b>cpu_patch_missing</b>. Next, it attempts to execute the method as a primitive, and then finally, calls <b>cpu_perform</b>, which is the function that actually sends the message by creating a new method context and activating it.</p>
<p>Once a send site has been dispatched the first time via this slow path, it will have been patched to use a more optimal lookup function, based upon the type of receiver/method that was found, so that subsequent sends from the same location use an optimised dispatch process represented by one of the specialised lookup functions described next.</p>
<h4>Specialised lookup functions</h4>
<p>Each of the following SendSite method lookup functions represents an optimised method dispatch process:</p>
<dl>
<dt>_cpu_ss_mono</dt>
<dd>A lookup function that attempts to use a CompiledMethod cached in the SendSite from the last send at the same send site. </dd>
<dt>_cpu_ss_mono_prim</dt>
<dd>A lookup function that attempts to use the primitive whose index is cached in the SendSite from the last send at the same send site. Note that this lookup function is patched into a SendSite by the <a href="http://rubini.us/doc/vm/op_codes/send_primitive.html">send_primitive</a> instruction. </dd>
<dt>_cpu_ss_mono_ffi</dt>
<dd>A lookup function that is used when a call to a native method using <a href="http://betterruby.wordpress.com/rubinius-glossary/#ffi">FFI</a> is encountered. Note that this lookup function is patched into a SendSite by the primitive <b>nfunc_call</b>, which is provides the implementation of the FFI<b> NativeFunction#call</b> method. </dd>
<dt>_cpu_ss_missing</dt>
<dd>A lookup function used when a receiver is found to contain no method matching the selector (method name). If the receiver is of the same class as the last send, it adds the method name to the list of arguments on the stack, and then dispatches to the cached method_missing implementation. </dd>
<dt>_cpu_ss_disabled</dt>
<dd>A lookup function that is used when a SendSite reaches a threshhold of misses (currently 10,000). It is the equivalent of the slow path in _cpu_ss_basic, but without any attempt to (re-)patch the lookup function. This ensures the SendSite uses the slow path on each dispatch, which is probably appropriate if the SendSite has missed this many times. This lookup function is patched into a SendSite by_cpu_ss_mono when it hits the threshhold. </dd>
</dl>
<h4>Lookup function patching</h4>
<p>Each time (other than the first) that a SendSite is used to dispatch a method call, a check needs to be performed to determine if the class of the receiver object matches that which is cached in the SendSite. If the receiver <i>is</i> the same, the optimised path represented by the current lookup function can proceed, and method dispatch is relatively swift. However, when the receiver class is different than the class cached on the SendSite, it is necessary to drop back to the slow approach represented by _cpu_ss_basic, find the appropriate method using the receiver class hierarchy, and then re-patch the lookup function based upon the current receiver object&#8217;s class.</p>
<p>Each of the above lookup functions (with the obvious exception of _cpu_ss_disabled) performs this same check at the start of the function, falling back to _cpu_ss_basic if the receiver class does not match. Similarly, we&#8217;ve seen above that _cpu_ss_basic handles the patching for _cpu_ss_mono and _cpu_ss_mono_missing, and described how the other special cases are handled.</p>
<h4>Flushing the cache</h4>
<p>Astute observers might be wondering &#8220;what happens when a method on a class is redefined?&#8221;. In this situation, any previously executed SendSites would be caching a now superseded CompiledMethod instance, and this would not be detected just by checking the receiver&#8217;s class during method dispatch.</p>
<p>The answer is that whenever a method is added or redefined, all SendSites using the method selector are reset to use _cpu_ss_basic. This is achieved using the Selector class, instances of which maintain a list of all SendSites using the given selector. See the function <b>selector_clear_by_name</b> in <a href="http://git.rubini.us/?p=code;a=blob;f=shotgun/lib/selector.c;h=5834f0f79001ca9bc74e07865e2de873de3e4586;hb=HEAD">shotgun/lib/selector.c</a> if you are interested in the details of how this is achieved.</p>
<h2>Future Plans</h2>
<p>At present, there are only a small number of relatively simple optimised method dispatch functions available for use with SendSites, and all of these lookup functions are monomorphic. In future, however, the flexibility and rich type information gathered by SendSites are likely to be exploited by further reworking of the method dispatch process, and additional lookup function implementations. Some ideas under consideration include:</p>
<ul>
<li><a href="http://www.springerlink.com/content/l83h8487w3363684/">Polymorphic inline caches</a> for use when a selector is found to resolve to different receivers. The most common receivers will be cached, and a quick scan of these receiver types will be performed before dropping back to the slow path if the receiver is not matched. This should improve dispatch performance for messages that commonly resolve to different receivers, such as to_s.</li>
<li>Making the dispatch process more modular and flexible to allow chaining, whereby steps in the method dispatch process can be chained together and performed one after another. This will be useful for preventing a proliferation of specialised dispatch functions in combination with other pointcut style functions, such as invoking the debugger or an instrumenting profiler. Instead, these steps could be optionally added/enabled for individual SendSites, providing a finer grain of control.</li>
</ul>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/betterruby.wordpress.com/23/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/betterruby.wordpress.com/23/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/betterruby.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/betterruby.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/betterruby.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/betterruby.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/betterruby.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/betterruby.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/betterruby.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/betterruby.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/betterruby.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/betterruby.wordpress.com/23/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=betterruby.wordpress.com&blog=2942765&post=23&subd=betterruby&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://betterruby.wordpress.com/2008/04/01/how-rubinius-sendsites-work-part-2/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/agardiner-128.jpg" medium="image">
			<media:title type="html">agardiner</media:title>
		</media:content>

		<media:content url="http://betterruby.files.wordpress.com/2008/03/2006-09_035-london-night.jpg" medium="image">
			<media:title type="html">2006-09_035-london-night.jpg</media:title>
		</media:content>
	</item>
		<item>
		<title>How Rubinius SendSites Work - Part 1</title>
		<link>http://betterruby.wordpress.com/2008/03/19/how-rubinius-sendsites-work-part-1/</link>
		<comments>http://betterruby.wordpress.com/2008/03/19/how-rubinius-sendsites-work-part-1/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 11:39:30 +0000</pubDate>
		<dc:creator>agardiner</dc:creator>
		
		<category><![CDATA[shotgun]]></category>

		<category><![CDATA[selector]]></category>

		<category><![CDATA[sendsite]]></category>

		<guid isPermaLink="false">http://betterruby.wordpress.com/?p=3</guid>
		<description><![CDATA[Recently, Rubinius switched from using a simple method dispatch caching mechanism to using a significantly more powerful mechanism known as a SendSite. Over the next couple of posts, we&#8217;ll look into the Rubinius SendSite implementation, commencing with an overview of what SendSites are in part 1. In  part 2, we&#8217;ll examine how SendSites are [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Recently, Rubinius switched from using a simple method dispatch caching mechanism to using a significantly more powerful mechanism known as a <b>SendSite</b>. Over the next couple of posts, we&#8217;ll look into the Rubinius SendSite implementation, commencing with an overview of what SendSites are in part 1. In  part 2, we&#8217;ll examine how SendSites are used in the method dispatch process.</p>
<p align="center"><a href="http://betterruby.files.wordpress.com/2008/03/2006-09_042-london-night.jpg" title="2006-09_042-london-night.jpg"><img src="http://betterruby.files.wordpress.com/2008/03/2006-09_042-london-night.jpg" alt="2006-09_042-london-night.jpg" /></a></p>
<h2>Origins</h2>
<p>Before we dive in and start looking at the Rubinius SendSite class, it may be worthwhile reviewing some of the terminology that will be used, and particularly, the origins of the term <i><b>SendSite</b></i>.</p>
<p>Ruby and Rubinius draw heavily on the Smalltalk language and implementation; within Smalltalk, perhaps <i>the</i> central concept is the idea of message passing, whereby objects interact via the sending of messages; we talk of objects <i>sending</i> messages to receivers and getting back responses. In practice, this is almost identical to saying that code <i>calls</i> a method and gets back a result, which is how the process is commonly described in most languages.</p>
<p>However, there is one key distinction: message sending makes clearer the concept of <a href="http://en.wikipedia.org/wiki/Duck_typing" title="duck-typing">duck-typing</a>, and encourages a coding style known as <a href="http://pragmaticprogrammer.com/articles/tell-dont-ask">&#8220;Tell, Don&#8217;t Ask&#8221;</a>. In Smalltalk and Ruby, we don&#8217;t really care what the type of the receiver is; we only care whether or not it can respond to the message we send. Similarly, in the &#8220;Tell, Don&#8217;t Ask&#8221; coding style, we tell receiver objects what we want them to do based on our internal state, we don&#8217;t ask the receiver for details of their state in order to make decisions. The result is that it is easier to replace the receiver object with another object that understands the same message, but perhaps performs the request in a different way.</p>
<h2>What is a SendSite?</h2>
<p>Ultimately, it is this very capability that complicates method dispatch in Ruby, and makes the use of method caching and other optimisations desirable: if the receiver class can change at <i>any</i> time, resolving exactly which implementation of the message to dispatch to cannot be determined definitively <i>until the actual point-in-time when the message is dispatched</i>. However, it is also true that <b>most</b> times, a given message send (i.e. send site) in a piece of code will resolve at dispatch time to the <b>same</b> receiving code (i.e. method)&#8230;</p>
<p>If we could therefore somehow cache the result of this method resolve process, the next time we reach the same send site, we can perform a quick check to determine if the receiving method is still the same as last time, and if so, use an optimised dispatch process. This could could range from the simple, such as jumping directly to the method code via a cached reference, to the complex, such as in-lining and JIT-ing frequently called methods into directly executable machine code at the send site.</p>
<p>The Rubinius SendSite, therefore, is an object that is created for every send site (method call) in the Rubinius bytecode, and facilitates these kinds of optimisations.</p>
<p>With that bit of background behind us, let&#8217;s dive in and see how Rubinius defines a SendSite&#8230;</p>
<h2>SendSite: Half Ruby class, half C struct</h2>
<p>We saw above that a SendSite represents a location in code where a message send (aka method call) takes place. At its most basic, a SendSite needs only record the name of the message that is to be sent; indeed, before SendSites were added, a reference to the Ruby symbol identifying the message name was all that was recorded in the Rubinius bytecode. However, by replacing the symbol of the message name with a data structure, we gain the ability to store additional information at the send site, and in particular, information that can be used to speed up method dispatch.</p>
<p>Rubinius SendSites, like a number of other core classes integral to the Shotgun VM, need to be accessible from both Ruby and C code. As most of the use of SendSite is in C code in the VM, and is performance critical, SendSite instance data is stored in the fields of a C struct:</p>
<dl>
<dt>name:</dt>
<dd>The name of the message (i.e. method) this send site sends (calls)</dd>
<dt>cm:</dt>
<dd>A reference back to the <tt>CompiledMethod</tt> instance in which the send site exists.</dd>
<dt>selector:</dt>
<dd>A reference to the <tt>Selector</tt> instance corresponding to the message name (see Selectors below) </dd>
<dt>data1:</dt>
<dd>The receiver class</dd>
<dt>data2:</dt>
<dd>The CompiledMethod corresponding to this message on the receiver class, as encountered on the last dispatch. When a message is dispatched, this is the target object that needs to be located; it contains the bytecode for the method on the receiver. </dd>
<dt>data3:</dt>
<dd>The module</dd>
<dt>data4:</dt>
<dd>The primitive index if the SendSite resolves to a primitive method</dd>
<dt>c_data:</dt>
<dd>A pointer to some C data;</p>
<ul>
<li>For an FFI send site, holds the address of the FFI stub function to call.</li>
<li>For a primitive send site, holds the address of the primitive function to call.</li>
</ul>
</dd>
<dt>hits, misses:</dt>
<dd>Counters for the number of times the SendSite has successfully and unsuccessfully cached the receiver method respectively.</dd>
<dt>lookup:</dt>
<dd>A function pointer (functor) to the method lookup function that will be used by the SendSite to perform method dispatch.</dd>
</dl>
<p>Ruby code can access most fields of this C struct via the <code>SendSite#at</code> method, which is implemented as a Rubinius <a href="http://rubini.us/doc/vm/vm_interfaces.html" title="primitives">primitive</a>.</p>
<p>The two most important data items in a SendSite are the symbol of the method name to which the SendSite relates, and the address of a lookup function to use to resolve the message name to a method object to which to dispatch. These two fields (and the reference to the containing CompiledMethod) are the only ones populated when a SendSite is initialized, and are sufficient to resolve a message send to a receiver method (albeit, via a slower path).</p>
<h2>Selectors</h2>
<p>We saw above that a SendSite contains a reference to a <tt>Selector</tt> object. A Selector is an object that represents a message (i.e. method) name. It consists of the symbol of a message, plus an array of links back to <i>every</i> SendSite that uses the same message. This can be extremely useful, as it provides the ability to locate all direct uses of a particular message (although indirect uses such as via send and the various evals are not caught).</p>
<p>Selectors are not used in the method dispatch process; they exist solely to provide a reverse lookup for a given method name to the SendSites that use it. Nonetheless, this is an extremely useful capability; it is used to find and reset SendSites impacted by a redefinition of a method, and is also extremely handy for finding the messages most often used. In fact, it is this capability that lies behind the <b>-ps</b> and <b>-pss</b> flags that can be used when launching Shotgun; upon exiting, these flags cause a summary to be printed of the most frequently encountered Selectors and SendSites respectively:<br />
<code><br />
ads@ads-kubuntu:~/rubinius$ shotgun/rubinius -ps 10 -e &#8216;0&#8242;</code><br />
<code><br />
Total Selectors: 1168<br />
Top 10, by receives:<br />
</code></p>
<table>
<tr>
<th>name</th>
<th>receives</th>
<th>send sites</th>
</tr>
<tr>
<td>at</td>
<td>15694</td>
<td>131</td>
</tr>
<tr>
<td>equal?</td>
<td>13074</td>
<td>47</td>
</tr>
<tr>
<td>misses</td>
<td>12748</td>
<td>2</td>
</tr>
<tr>
<td>hits</td>
<td>12746</td>
<td>2</td>
</tr>
<tr>
<td>[]</td>
<td>11842</td>
<td>1180</td>
</tr>
<tr>
<td>kind_of?</td>
<td>5865</td>
<td>183</td>
</tr>
<tr>
<td>&lt;=</td>
<td>4390</td>
<td>53</td>
</tr>
<tr>
<td>size</td>
<td>4293</td>
<td>225</td>
</tr>
<tr>
<td>hash</td>
<td>3967</td>
<td>11</td>
</tr>
</table>
<p>Note that this shows the most frequently sent <i>messages</i>, which is <b>not</b> the same as the most frequently executed <i>methods</i>; for that, we need to know the receiver as well. For example, the method #at is the most frequently exexcuted message, but is actually distributed across three different receiver methods (<code>Time#at</code>, <code>Tuple#at</code>, and <code>Array#at</code>).</p>
<p>In Part 2, we&#8217;ll look at the lifecycle of a SendSite, and see how it influences the method dispatch process.<span id="more-3"></span></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/betterruby.wordpress.com/3/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/betterruby.wordpress.com/3/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/betterruby.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/betterruby.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/betterruby.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/betterruby.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/betterruby.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/betterruby.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/betterruby.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/betterruby.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/betterruby.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/betterruby.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=betterruby.wordpress.com&blog=2942765&post=3&subd=betterruby&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://betterruby.wordpress.com/2008/03/19/how-rubinius-sendsites-work-part-1/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/agardiner-128.jpg" medium="image">
			<media:title type="html">agardiner</media:title>
		</media:content>

		<media:content url="http://betterruby.files.wordpress.com/2008/03/2006-09_042-london-night.jpg" medium="image">
			<media:title type="html">2006-09_042-london-night.jpg</media:title>
		</media:content>
	</item>
		<item>
		<title>Shotgun: The Rubinius Virtual Machine</title>
		<link>http://betterruby.wordpress.com/2008/03/18/shotgun-the-rubinius-virtual-machine/</link>
		<comments>http://betterruby.wordpress.com/2008/03/18/shotgun-the-rubinius-virtual-machine/#comments</comments>
		<pubDate>Tue, 18 Mar 2008 01:39:09 +0000</pubDate>
		<dc:creator>agardiner</dc:creator>
		
		<category><![CDATA[shotgun]]></category>

		<category><![CDATA[compiled method]]></category>

		<category><![CDATA[context]]></category>

		<category><![CDATA[machine]]></category>

		<category><![CDATA[task]]></category>

		<guid isPermaLink="false">http://betterruby.wordpress.com/?p=13</guid>
		<description><![CDATA[As I stated in my introductory post, I intend with this blog to delve into some of the implementation details of Rubinius. However, as I&#8217;ve contemplated various topics to write about, I&#8217;ve realised I first need to introduce some of the core underlying concepts and (Ruby) classes unique to Rubinius.
The most important of these (and [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>As I stated in my introductory post, I intend with this blog to delve into some of the implementation details of Rubinius. However, as I&#8217;ve contemplated various topics to write about, I&#8217;ve realised I first need to introduce some of the core underlying concepts and (Ruby) classes unique to Rubinius.</p>
<p>The most important of these (and the topic of this post) are those that relate to the Rubinius execution environment: the Shotgun Virtual Machine, and the various Ruby classes that provide access to Shotgun internals.</p>
<p align="center"><a title="louvre.jpg" href="http://betterruby.files.wordpress.com/2008/03/louvre.jpg"><img src="http://betterruby.files.wordpress.com/2008/03/louvre.jpg" alt="louvre.jpg" /></a><a title="paris-2006-09_027.jpg" href="http://betterruby.files.wordpress.com/2008/03/paris-2006-09_027.jpg"> </a></p>
<h2>Shotgun: A Virtual Machine</h2>
<p>As mentioned <a href="http://rubini.us">elsewhere</a>, Rubinius is heavily influenced by the implementation of Smalltalk-80, and borrows many of the same concepts and even some of it&#8217;s class names from there. Like Smalltalk and Java, Rubinius compiles Ruby source code into a lower-level machine-independent instruction set that is executed on a virtual machine, known as Shotgun.</p>
<p>The Shotgun virtual machine has many similarities to a real computer, such as (virtual) CPUs and an instruction set, but also many higher-level abstractions (such as managed memory and a garbage collector), that make it easier to target as an execution environment for a high-level dynamic language such as Ruby.</p>
<p>Shotgun is currently written in C, although some portions of the source code are actually generated from Ruby (e.g. the opcode and primitive implementations are defined as embedded C code inside Ruby methods). In the future (post-1.0), the plan is to have more of the C code generated from Ruby or a Ruby-like language (Garnet), much as how Squeak (a Smalltalk implementation) <a href="http://portal.acm.org/citation.cfm?id=263754&amp;coll=portal&amp;dl=ACM/">implemented a virtual machine in Squeak</a>.</p>
<h3>Shotgun Architecture</h3>
<p>Shotgun is written in a relatively clean and easy to follow style. It contains no global variables, and consists of a layered architecture: at the root is an <strong>environment</strong>, within which <strong>machines</strong> are instantiated. Each machine represents an entire Ruby/Rubinius virtual machine, and runs in its own native (OS) thread. Machines can communicate via an inter-machine message channel, but are otherwise totally separate and isolated.</p>
<p>Within a machine, there exists a virtual <strong>CPU</strong>, which runs one or more (green) <strong>threads</strong>. A Shotgun CPU effectively represents a native thread on the underlying hardware, whereas a Shotgun thread represents a Ruby thread. Just like a real CPU, the Shotgun virtual CPU pre-emptively multi-tasks (Shotgun) threads. At present, a Shotgun machine always has a single CPU, so all Shotugn threads within a single machine therefore execute on a single native thread. In the future (again, post-1.0) it is planned to implement what is known as an <em>m:n</em> threading model, whereby a pool of <em>m</em> native threads are used to execute <em>n</em> Ruby threads.</p>
<p>At the next level down from threads are what are known as <strong>tasks</strong>. Each Shotgun task maintains an operand stack (Shotgun is a <a href="http://en.wikipedia.org/wiki/Stack_machine">stack-based</a> VM) and a reference to the current execution <strong>context</strong>. Tasks are very similar to threads, but lack pre-emption or scheduling. In practice, they are similar to Ruby 1.9 fibres, although unlike fibres, there is currently no way to co-operatively multi-task (or yield to a co-routine) using Rubinius tasks.</p>
<p>A <strong>context </strong>represents something similar to a stack frame in C or Java. It represents the current execution context, and as such, it provides:</p>
<ul>
<li>a link back to the caller of the current method;</li>
<li>a reference to the compiled method currently being executed;</li>
<li>instruction (IP), stack (SP), and frame (FP) pointers for the current instruction, current stack operand, and the operand stack pointer location at the commencement of the current method respectively;</li>
<li>the current scope for resolving constant and method lookups; and</li>
<li>storage for all local variables in the current scope.</li>
</ul>
<p>Finally, each context has an associated <strong>compiled method</strong>, which contains the instruction bytecodes to be executed for the method to which the context relates. Compiled methods are the result of compiling Ruby source into Shotgun bytecode, and are the units of execution in Shotgun. A compiled method contains:</p>
<ul>
<li> the bytecode instruction sequence that tells Shotgun what actions to take;</li>
<li>the number and names of any local variables used in the bytecode;</li>
<li>the static scope, used for resolving constant and method lookups; and</li>
<li>a <strong>tuple </strong>containing the literals contained in the source code that cannot be represented directly as opcode arguments (e.g. strings, symbols, method calls etc).</li>
</ul>
<h2>Key Rubinius Classes</h2>
<p>Without further ado, let&#8217;s look at the Ruby classes that correspond to the concepts above&#8230; but this time, we&#8217;ll work from the bottom up.</p>
<h3>InstructionSequence</h3>
<p>In Rubinius, Ruby code is compiled down to <a title="bytecode" href="http://en.wikipedia.org/wiki/Bytecode">bytecode</a> , which is then executed by Shotgun, the Rubinius virtual machine. The compilation process is reasonably complex (see <a title="Rubinius Compiler Walkthrough" href="http://rubini.us/rbx_documentation/introduction-to-the-compiler/">here</a> for a detailed overview), but the end result is that Ruby code is converted into a sequence of integers, representing the VM opcodes and any arguments they take. The class that represents this bytecode in Rubinius is <strong>InstructionSequence</strong>, which is a sub-class of ByteArray.</p>
<p>The InstructionSequence class does not have many useful instance methods, since it is essentially a representation of the Shotgun machine language. However, the class source file defines a number of related classes for working with InstructionSequences that <em>are</em> useful, including:</p>
<h4>InstructionSet</h4>
<p>Defines the full set of Shotgun instructions or opcodes, and includes useful metadata about each instruction. This includes information about the number and purpose of any opcode arguments, whether the opcode changes the flow of execution, the number of stack operands consumed and produced, etc.</p>
<h4>Encoder</h4>
<p>This class is used to encode and decode an instruction sequence between symbolic and bytecode representations. It is used by the compiler, to convert a generated instruction sequence consisting of opcode symbols and arguments into the actual bytecode executed by Shotgun and saved to disk in .rbc files. It is also used by tools such as the debugger to disassemble the bytecode of a CompiledMethod into something that can be displayed on screen, or to modify bytecode to support debugging.</p>
<h3>CompiledMethod</h3>
<p>A CompiledMethod represents the compiled source code for a Ruby method (or top-level script, i.e. Ruby code that is not part of a method body). As such, a CompiledMethod contains an InstructionSequence instance containing the compiled bytecode for the method source, the number and names of any local variables used in the method, details of the method scope, and a whole bunch of other attributes.</p>
<p>A CompiledMethod is the main executable unit in a Rubinius program. It is the output created by the Rubinius compiler that is then passed to Shotgun for execution and/or persisted to disk. CompiledMethod instances can be obtained from any method definition using the #compiled_method accessor on a Method or UnboundMethod object.</p>
<p>CompiledMethod objects are also nested; each Ruby source file that is compiled by Rubinius creates a single top-level CompiledMethod object named <strong>__script__</strong>, which is then run when the (compiled) file is loaded. Any CompiledMethod can contain other CompiledMethod objects as literals; so when a Ruby script is executed that contains, for example, a def statement, the bytecode for the new method will be compiled into its own CompiledMethod object, and this CompiledMethod will then be added to the literals tuple of the containing CompiledMethod. From there, it can then be referenced by opcodes such as <a href="http://rubini.us/doc/vm/op_codes/add_method.html">add_method</a>, which hook a CompiledMethod up to a symbol in a method table.</p>
<h3>MethodContext and BlockContext</h3>
<p>The next level up from a CompiledMethod is an execution context, in the form of either a MethodContext or a BlockContext (depending upon whether we are dealing with the execution of a method or a block). Where a CompiledMethod represents the executable instructions for a given method or top-level script, an execution context represents the actual execution of Rubinius code.</p>
<p>MethodContext and BlockContext instances provide a way to inspect and modify the execution environment. Not surprisingly, they are therefore a key component enabling the Rubinius debugger to do its thing. However, they also make implementation of eval bindings and continuations almost trivial, since an execution context contains all the necessary details to resolve binding references relative to some other context (e.g. a caller&#8217;s context), and to save and restore execution state.</p>
<h3>Task</h3>
<p>As we saw earlier, a Shotgun task maintains an operand stack and a reference to the current execution context. Tasks are also the building blocks for Ruby threads, and provide a way to transfer an execution context from one Ruby thread to another.</p>
<p>The Task class provides access to a the current execution contex, via Task#current_context, and to the operand stack (the latter being of interest primarily to the debugger).</p>
<h3>Thread</h3>
<p>The Thread class provides an implementation of the Ruby Thread class semantics using a combination of Ruby code, Tasks, and Rubinius (Shotgun) <a href="http://rubini.us/doc/vm/vm_interfaces.html">primitives</a>: the execution context for a thread is maintained via an associated Task, and methods that control thread scheduling and execution are implemented as primitives.</p>
<h2>Conclusion</h2>
<p>In this post, we&#8217;ve  introduced the Shotgun virtual machine, and looked at how it models an execution environment through the concepts of machines, cpus, tasks, etc. However, there is a good deal more to Shotgun that we&#8217;ve not even touched on, and which will have to be saved for a future post.</p>
<p>I hope you&#8217;ve found this post informative; feel free to ask questions, provide feedback, or indicate the areas you&#8217;d like to know more about using the comments facility below.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/betterruby.wordpress.com/13/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/betterruby.wordpress.com/13/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/betterruby.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/betterruby.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/betterruby.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/betterruby.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/betterruby.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/betterruby.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/betterruby.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/betterruby.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/betterruby.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/betterruby.wordpress.com/13/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=betterruby.wordpress.com&blog=2942765&post=13&subd=betterruby&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://betterruby.wordpress.com/2008/03/18/shotgun-the-rubinius-virtual-machine/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/agardiner-128.jpg" medium="image">
			<media:title type="html">agardiner</media:title>
		</media:content>

		<media:content url="http://betterruby.files.wordpress.com/2008/03/louvre.jpg" medium="image">
			<media:title type="html">louvre.jpg</media:title>
		</media:content>
	</item>
		<item>
		<title>Hello World!</title>
		<link>http://betterruby.wordpress.com/2008/02/22/hello-world/</link>
		<comments>http://betterruby.wordpress.com/2008/02/22/hello-world/#comments</comments>
		<pubDate>Fri, 22 Feb 2008 04:28:58 +0000</pubDate>
		<dc:creator>agardiner</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[I guess it&#8217;s kind of traditional to start a blog with a bit of background about the who, what, and why&#8230; so here goes!

Who: I&#8217;m an IT consultant with a degree in Economics and a Computer Science major. Mostly I work in the Business Intelligence and Data Warehousing space, but I also like to build [...]]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I guess it&#8217;s kind of traditional to start a blog with a bit of background about the who, what, and why&#8230; so here goes!</p>
<p align="center"><a href="http://betterruby.files.wordpress.com/2008/03/provence-2006-05-171.jpg" title="provence-2006-05-171.jpg"><img src="http://betterruby.files.wordpress.com/2008/03/provence-2006-05-171.jpg" alt="provence-2006-05-171.jpg" /></a></p>
<p><span style="font-weight:bold;">Who:</span> I&#8217;m an IT consultant with a degree in Economics and a Computer Science major. Mostly I work in the Business Intelligence and Data Warehousing space, but I also like to build stuff, and so I&#8217;ve done a lot of work coding in Java, and over the last couple of years, Ruby. I started contributing to Rubinius in October 2007, with most of my interest and effort focused on the Rubinius compiler and debugger. You can find me on the <a href="http://www.donttreadonme.co.uk/rubinius-irc/index.html" title="#rubinius">#rubinius</a> IRC channel as <b>agardiner</b>.</p>
<p><span style="font-weight:bold;">What:</span> This blog is intended to focus on <a href="http://rubini.us" title="Rubinius">Rubinius</a>, a new implementation of Ruby that is written predominantly in Ruby, and which is accelerating towards a 1.0 release. If you haven&#8217;t heard of Rubinius, there are a bunch of good resources you can read to get up to speed; see the links in the sidebar.</p>
<p><span style="font-weight:bold;">Why:</span> I&#8217;m starting this blog as a way to share my knowledge, raise the profile of Rubinius, and encourage others to jump in. Since Rubinius is Ruby built in Ruby, the barriers to entry are incredibly low to anyone with a decent knowledge of Ruby. This is quite different to the situation with all other Ruby implementations, which require a good knowledge of Ruby <span style="font-style:italic;font-weight:bold;">and </span>some other language.</p>
<p>I intend to blog initially about some of the cool things that set Rubinius apart from other Ruby implementations, as well as covering some of the challenges in building the Rubinius debugger. So if you are interested in Rubinius, check back here periodically, and let me know what you think!</p>
<p>Cheers,</p>
<p>Adam</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/betterruby.wordpress.com/1/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/betterruby.wordpress.com/1/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/betterruby.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/betterruby.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/betterruby.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/betterruby.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/betterruby.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/betterruby.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/betterruby.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/betterruby.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/betterruby.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/betterruby.wordpress.com/1/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=betterruby.wordpress.com&blog=2942765&post=1&subd=betterruby&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://betterruby.wordpress.com/2008/02/22/hello-world/feed/</wfw:commentRss>
	
		<media:content url="http://a.wordpress.com/avatar/agardiner-128.jpg" medium="image">
			<media:title type="html">agardiner</media:title>
		</media:content>

		<media:content url="http://betterruby.files.wordpress.com/2008/03/provence-2006-05-171.jpg" medium="image">
			<media:title type="html">provence-2006-05-171.jpg</media:title>
		</media:content>
	</item>
	</channel>
</rss>