<?xml version="1.0" encoding="US-ASCII"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
    <title>Whiteknight's Parrot Blog</title>
    
    <link href="http://whiteknight.github.com/parrot/index.html" />
    <updated>2012-02-26T18:24:54-08:00</updated>
    <id>http://whiteknight.github.com/parrot/index.html</id>
    <author>
        <name>Andrew Whitworth (Whiteknight)</name>
        <email>wknight8111@gmail.com</email>
    </author>

    
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/afwknight-parrot" /><feedburner:info uri="afwknight-parrot" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
        <title>Introspection, Disasembly and PACT (GSOC)</title>
        <link href="http://feedproxy.google.com/~r/afwknight-parrot/~3/qQYjnw9B6vM/gsoc_idea_pact.html" />
        <updated>2012-02-21T00:00:00-08:00</updated>
        <id>http://whiteknight.github.com/2012/02/21/gsoc_idea_pact</id>
        <content type="html">&lt;p&gt;In &lt;a href='/2012/02/15/gsoc_season_starting.html'&gt;my post a few days ago I mentioned Google Summer of Code 2012&lt;/a&gt; and gave a lightning list of simple project ideas that might be worth pursuing. Today I&amp;#8217;m going to expand on one of these ideas because it&amp;#8217;s fertile ground for many possible GSOC projects, including the possibility of several projects concurrently if we have multiple students interested in it.&lt;/p&gt;

&lt;p&gt;Parrot has a lot of introspection ability, but we don&amp;#8217;t really have the tools necessary to introspect bytecode. We need some kind of tool that, given a Sub PMC or a PackfileView PMC or similar will be able to provide a disassembled representation of the actual opcodes. Here&amp;#8217;s a basic code example of what I am talking about:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function foo() { var x = 2; ... }

function bar() {
    var disassem = new Parrot.PACT.Disassembler();
    using foo;
    var raw = disassem(foo);
    var reg = raw.registers();          // Get register counts
    var lex = raw.lexicals();           // Get info about lexicals
    var constants = raw.constants()     // Referenced constants
    var ops = raw.opcodes();            // Symbolic Opcodes
    say(ops[0]);                        // &amp;quot;set_p_i, $P0, 2&amp;quot;, etc
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;These are just some random ideas and not all of them are necessary to implement. The most important part, in my mind, is getting a list of symbolic Opcode PMCs. Each Opcode PMC would have this general form:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Parrot.PACT.Opcode {
    var opname;         // The name or short name of the op
    var opnumber;       // The number of the opcode
    var oplib;          // The oplib which owns it
    var args;           // Array of Arguments
                        // Either Register or Constant
    ...
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I prefix the disassembler classname with the namespace &amp;#8220;Parrot.PACT&amp;#8221; because eventually this should be an integral component of the PACT library. When we use PACT to assemble packfiles (and, ultimately, bytecode files) we&amp;#8217;ll be constructing a list of these Opcode PMCs and then using a serializer to write them down to raw bytecode.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;                  Serializer
Array of Opcodes ------------&amp;gt; Packfile Bytecode Segment

          Deserializer
Bytecode --------------&amp;gt; Array of Opcodes&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;An excellent proof of concept system would combine these two mechanisms together into a faithful round-trim assembly/disassembly mechanism. In fact, there are multiple little potential projects here that can be arranged and ordered/prioritized to create a summer-long project or many:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a tool to disassemble raw bytecode into Opcode PMCs, and create a disassembler program to interact with the user and print the disassembly listings to file/console.&lt;/li&gt;

&lt;li&gt;Create a tool for round-trip disassembly and assembly. Write the disassembly type, then write a tool that does the reverse operation (take a list of Opcodes and write a valid Packfile or bytecode segment).&lt;/li&gt;

&lt;li&gt;Create the tool to disassemble raw bytecode, then write a utility layer to construct a control flow graph from those Opcodes. This layer could be used in turn to create things like code complexity analyzers, or even simple decompilers (for the very ambitious student).&lt;/li&gt;

&lt;li&gt;Write a tool to take a stream of Opcode PMCs and other related data (tables of constant values, annotations, debugging symbols, etc) and write them into a valid and executable packfile. This would be the base layer of the PACT assembly engine, and would be used to help build compilers and other tools.&lt;/li&gt;

&lt;li&gt;Construct anything else PACT-related (AST and manipulators, CFG/DAG and friends, PIR-&amp;gt;Opcode assembler, etc). There is lots of fertile ground here for projects (and we have a lot of ideas and designs already put together for these things).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There are lots of ideas here and I&amp;#8217;ve still only scratched the surface. My goal with this post is to show how fertile this ground is, how much available work there is to be had and how many new features we desperately need.&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s a basic flow graph of things I&amp;#8217;m envisioning as eventual parts of PACT or its close cousins. This will show the kinds of components that PACT may either eventually contain or serve as the common substrate for:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;                                      PIR and PASM Code
                                              |
            Optimizers         Analyzers &amp;lt;-+  |  +-&amp;gt; Debugger/Live
            ^ |    ^ |           ^         |  |  |            Interpreter
            | V    | V           |         |  V  |
HLL code -&amp;gt; AST -&amp;gt; Control Flow Graph -&amp;gt; Opcode stream -&amp;gt; Packfile
             ^       |           ^         |  ^  ^           |
             |       |           |         |  |  |           |
            HLL &amp;lt;----+           +---------+  |  +-----------+
       (Decompiled)                           |              |
                                              |              |
                                             PIR Code &amp;lt;------+
                                          (Disassembly)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;One day when I have more time I may try to put this together into a real image of some variety. ASCII graphics were good enough for our digital ancestors and they will suffice here for a first draft. As you can see this graph contains several components, any one of which or any small subsection might make for an interesting and extremely rewarding project over the summer. This also ignores the inherent complexity and layered architecture possible in things like the AST transformations and optimizations, register allocation, etc. My point is that even the blocks on the graph above can be further decomposed into a variety of smaller but still interesting projects. If any of this stuff looks interesting to you, please get in touch ASAP so we can start talking and planning. Obviously this is more work than one person will do in one summer, so we want to make sure we are coordinating between all interested parties.&lt;/p&gt;

&lt;p&gt;I think that if we start on the left side of this chart and implement the routines for reading from and writing to packfiles first, we can start building layers of additional functionality on top of them. This gives us an ability to break such a big system up into managable parts, to complete some of those parts in small summer-sized chunks, and to be able to use intermediate implementations to solve real problems while we wait for the rest of the system to grow and mature.&lt;/p&gt;

&lt;p&gt;If we had multiple students interested in working on PACT in one capacity or another it would be an awesome way to maximize developer resources and help push forward the idea of code reusability. I&amp;#8217;m really excited about this whole area and would love it if some students were interested in it too.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/afwknight-parrot/~4/qQYjnw9B6vM" height="1" width="1"/&gt;</content>
    <feedburner:origLink>http://whiteknight.github.com/2012/02/21/gsoc_idea_pact.html</feedburner:origLink></entry>
    
    <entry>
        <title>Compile Winxed With Winxed</title>
        <link href="http://feedproxy.google.com/~r/afwknight-parrot/~3/kzPPaZ2PpCc/pure_winxed_compiler.html" />
        <updated>2012-02-18T00:00:00-08:00</updated>
        <id>http://whiteknight.github.com/2012/02/18/pure_winxed_compiler</id>
        <content type="html">&lt;p&gt;You can compile Winxed code with Winxed itself! What&amp;#8217;s that you say? The Winxed compiler is bootstrapped and self-hosted, and is written in Winxed and already compiles winxed? Well, that&amp;#8217;s all true. Sort of. However there is one small caveat: The Winxed driver program historically has not been able to perform the last step of compilation. The driver compiles winxed code down to PIR, but then uses the &lt;code&gt;spawnw&lt;/code&gt; opcode to invoke an instance of Parrot to compile the PIR down to PBC.&lt;/p&gt;

&lt;p&gt;I&amp;#8217;m pleased to say that this last step is no longer necessary. At least, not in Winxed master (which has not yet been snapshotted into Parrot core).&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s a small toy compiler driver that uses Parrot&amp;#8217;s PackfileView PMC to compile a &lt;code&gt;.winxed&lt;/code&gt; file down into &lt;code&gt;.pbc&lt;/code&gt; without spawning any child processes:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function get_winxed_compiler(string pbc_name = &amp;quot;winxedst3.pbc&amp;quot;)
{
    var wx_pbc = load_packfile(pbc_name);
    for (var load_sub in wx_pbc.subs_by_tag(&amp;quot;load&amp;quot;))
        load_sub();
    return compreg(&amp;quot;winxed&amp;quot;);
}

function main[main](var args)
{
    var wx_compreg = get_winxed_compiler();
    string winxedcc_name = args.shift();
    string infile_name = args.shift();
    string outfile_name = args.shift();

    string code = (new &amp;#39;FileHandle&amp;#39;).readall(infile_name);
    var pf = wx_compreg.compile(code);
    pf.write_to_file(outfile_name);
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&amp;#8217;s less than 20 lines of Winxed code to get the Winxed compiler object loaded, to compile the code and to output the PBC to file. We can make this better, of course, by being more flexible in the handling of arguments and printing out basic help and error messages and all that stuff. Eventually we are going to update the winxed executable itself to use this trick instead of spawning the child process. This should, I hope, have a noticably beneficial effect on compiling with Winxed from the commandline. For large, long builds like Rosella has, any speed improvements are appreciated.&lt;/p&gt;

&lt;p&gt;One particularly interesting tidbit to notice is the very first line: A new syntax for handling optional parameters. I put a patch for that feature together last week and NotFound decided he could do the same thing but better than I did. So, the latest Winxed compiler (again, not yet snapshotted into Parrot Core) supports this syntax for providing default values to optional arguments. I hope that this feature is included with the 4.1 release next week. Here are some examples of the new feature in action:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// This...
function foo(var bar [optional], int has_bar [opt_flag])
{
    if (!has_bar)
        bar = default_bar_value();
    ...
}

// ...is the same as this
function foo(var bar = default_bar_value())
{
    ...
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The initializer can be any expression value, including expressions involving previous arguments:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function foo (var bar, var baz = bar.some_method(bar))&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This new syntax probably has a few kinks to work out still, but it&amp;#8217;s a very cool and very appreciated addition. I&amp;#8217;m hoping to use this new syntax to clean up a lot of code in Rosella and hopefully in Jaesop too.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/afwknight-parrot/~4/kzPPaZ2PpCc" height="1" width="1"/&gt;</content>
    <feedburner:origLink>http://whiteknight.github.com/2012/02/18/pure_winxed_compiler.html</feedburner:origLink></entry>
    
    <entry>
        <title>Compiling Templates</title>
        <link href="http://feedproxy.google.com/~r/afwknight-parrot/~3/cvI9r-8Stds/rosella_template_compilation.html" />
        <updated>2012-02-16T00:00:00-08:00</updated>
        <id>http://whiteknight.github.com/2012/02/16/rosella_template_compilation</id>
        <content type="html">&lt;p&gt;I had a precious few hours to myself yesterday and was able to do some updating work on Rosella&amp;#8217;s Template library. I was able to use the time to implement a feature I had wanted for a while: template compilation. You can now compile a template into winxed code or even compile it all the way down to a Packfile. Actually, that&amp;#8217;s sort of a lie. The Winxed compiler &lt;code&gt;.compile()&lt;/code&gt; method returns an Eval PMC not a PackfileView PMC. I&amp;#8217;m going to submit a patch for that soon, and when I do you&amp;#8217;ll be able to save the template to a .pbc file.&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s how to use the Template library in the basic, interpreted way:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var engine = new Rosella.Template.Engine();
string output = engine.generate(template, context);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The template is a string with a format that I&amp;#8217;ve demonstrated before, and the &lt;code&gt;context&lt;/code&gt; is any user-defined data structure that you want to use to populate the variables in the template. I won&amp;#8217;t go into detail about those things in this post. Now, after my recent changes and additions, you can compile your template to an executable Sub:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var engine = new Rosella.Template.Engine();
var sub = engine.compile(template);
string output = sub(context);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Or, if you really want to see the generated winxed code, you can get that:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;string wx_code = engine.compile_to_winxed(template);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The compilation process does take some time, there&amp;#8217;s no way to deny that. There are ways to mitigate that expense, of course. You can compile ahead of time and save the code to a file or even a .pbc and execute that later. There are several strategies if you&amp;#8217;re really interested, I won&amp;#8217;t go into too much detail here. Once the code is compiled, which can and should be done ahead of time, the time savings during execution are significant. Here&amp;#8217;s some benchmarking I&amp;#8217;ve done to time a relatively simple template with a ten thousand iteration &lt;code&gt;&amp;lt;$ repeat $&amp;gt;&lt;/code&gt; loop:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Interpreted:
0.969569s - %100.000000
0.796563s - %82.156419 (-%17.843581 compared to base)
0.900937s - %92.921402 (-%7.078598 compared to base)

Compiled:
0.365500s - %37.697161 (-%62.302839 compared to base)
0.498571s - %51.421914 (-%48.578086 compared to base)
0.367616s - %37.915399 (-%62.084601 compared to base)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In some cases, pre-compiling the template can take a third as much time as interpreting the template directly. Almost every timing I&amp;#8217;ve seen is around 50% or better of the interpreted time.&lt;/p&gt;

&lt;p&gt;This is a very new feature and I haven&amp;#8217;t added it to the test suite yet. Expect rough edges as I play with it and optimize it. If you&amp;#8217;re doing a lot of templating with Rosella, this feature could help save some time for you and plus it was a really fun thing to work on.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/afwknight-parrot/~4/cvI9r-8Stds" height="1" width="1"/&gt;</content>
    <feedburner:origLink>http://whiteknight.github.com/2012/02/16/rosella_template_compilation.html</feedburner:origLink></entry>
    
    <entry>
        <title>GSOC 2012 Is Starting</title>
        <link href="http://feedproxy.google.com/~r/afwknight-parrot/~3/oAO5JU3LCdk/gsoc_season_starting.html" />
        <updated>2012-02-15T00:00:00-08:00</updated>
        <id>http://whiteknight.github.com/2012/02/15/gsoc_season_starting</id>
        <content type="html">&lt;p&gt;You might not see it, but I&amp;#8217;m starting to get very excited. Discussions about the Google Summer of Code program is starting up for the 2012 summer. Projects in years past have lead to some awesome developments in Parrot, either directly or indirectly, and 2012 could easily deliver more.&lt;/p&gt;

&lt;p&gt;For prospective students, here are some blog posts I&amp;#8217;ve written in years past about the process, and what you need to do to get started:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href='http://whiteknight.github.com/2011/04/11/gsoc_proposals.html'&gt;GSOC Proposals&lt;/a&gt;&lt;/li&gt;

&lt;li&gt;&lt;a href='http://whiteknight.github.com/2011/03/27/gsoc_students_next_steps.html'&gt;Next Steps&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you&amp;#8217;re interested in participating in GSoC this year, with Parrot or any other organization, I suggest you read those two posts. They&amp;#8217;re important. Many students in school or freshly graduated may know how to code but might not know much about some of the tangential topics: source control (git, in the case of Parrot), documentation, unit testing, refactoring, etc. Now would be a great time to start brushing up on all those topics, so you don&amp;#8217;t waste time during the summer getting your tools in place.&lt;/p&gt;

&lt;p&gt;As usual I will try to post some ideas for projects here on this blog. If you are an eligible student and you are interested in one or more of these ideas please get in touch with me or other interested Parrot developers. If you have other ideas that I don&amp;#8217;t mention, that&amp;#8217;s cool too. Get in touch anyway and we can start talking about those ideas. &lt;strong&gt;The important thing is to talk to us&lt;/strong&gt;. Seriously, it&amp;#8217;s important.&lt;/p&gt;

&lt;p&gt;Here are a few ideas off the top of my head that might be worth some more investigation. I might write additional posts about these ideas if people want more information about them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Opcode disassembler&lt;/strong&gt;. We have a growing set of tools for working with bytecode from running Parrot code, but we don&amp;#8217;t have all the pieces that we would need to make a fully self-hosted disassembler. What we still need are tools to read out the raw bytecode and convert into a sequence of Opcode PMC representations, then a driver program to turn that output into readable (and hopefully round-trim compilable) disassembly output.&lt;/li&gt;

&lt;li&gt;&lt;strong&gt;PACT&lt;/strong&gt; An alternate to Parrot&amp;#8217;s venerable PCT library, called PACT, has been in the planning stages for many months. What we need is somebody with the time and motivation to start putting those ideas into practice. A toolkit library that works with syntax trees and outputs working bytecode libraries would be quite an awesome thing to have, and would help us push the state-of-the-art for Parrot compilers up a few notches. Intended to be a layered system, the successful student could implement only a few of the necessary layers.&lt;/li&gt;

&lt;li&gt;&lt;strong&gt;Jaesop Stage 1&lt;/strong&gt; Jaesop is a bootstrapped JavaScript compiler. Right now there is a stage 0 compiler which uses node.js to compile JS into Winxed. We need a stage 1 compiler, written in JavaScript, that can run on stage 0 and compile itself. It&amp;#8217;s an interesting, mind-bendy kind of project. If you know compilers and you like JavaScript, this might be the project for you.&lt;/li&gt;

&lt;li&gt;&lt;strong&gt;Anything Python-Related&lt;/strong&gt; Parrot wants and needs more Python love. We&amp;#8217;ve had a few attempts at making a working Python compiler before on Parrot. Working on any of those, starting a new attempt, or working on other ways to integrate Python and Parrot would all be greeted with some eagerness.&lt;/li&gt;

&lt;li&gt;&lt;strong&gt;New Object Model&lt;/strong&gt; Parrot needs a new object model. Right now we have 6model waiting in the environs, but we haven&amp;#8217;t integrated it yet. There is some question about whether we want to copy+paste merge 6model as it is, or if we want to try and make a more custom adaptation of it from the ground up. Proposals to do either, or anything closely related, would be very interesting.&lt;/li&gt;

&lt;li&gt;&lt;strong&gt;LAPACK Bindings for PLA&lt;/strong&gt; I&amp;#8217;ve wanted PLA to get LAPACK bindings for a long time now, and I&amp;#8217;ve never had the time to do it myself. I&amp;#8217;ve never really even had the time to design what such a thing would look like. I suspect we can do the lion&amp;#8217;s share through raw NCI. Tools to solve matrices for eigenvalues and eigenvectors and common transformations and decompositions would be very interesting indeed.&lt;/li&gt;

&lt;li&gt;&lt;strong&gt;New PackFile Loader&lt;/strong&gt; I&amp;#8217;ve complained about the unnecessarily magic behavior of our packfile loader before. A new, re-written packfile loader would not automatically assemble Namespace or MultiSub PMCs at load time. Down this path lay the possibility for significant performance improvements and complexity reductions in some of our oldest and least-friendly code. It would require serious changes to IMCC, PIR, and higher-level compilers like Winxed. The new NQP already does the right kind of thing and would serve as a great example to follow.&lt;/li&gt;

&lt;li&gt;&lt;strong&gt;Anything Thread or Asynchrony Related&lt;/strong&gt; Parrot has Green Threads on Unixy platforms. Extending proper support to Windows and elsewhere would be awesome (again, something I want to do but have not had time or a Windows machine for testing). Asynchronous IO using new threading primitives and anything else that you could think to build with the new threading system would be awesome. Adding in types that would help the effort such as lock-free arrays and hashes would be nice assets. Adding in support for concurrency primitives like locks, mutexes and critical sections would also be cool (and implementing those in terms of green thread primitives would be even cooler).&lt;/li&gt;

&lt;li&gt;&lt;strong&gt;Divided VTABLE&lt;/strong&gt; Parrot&amp;#8217;s VTABLE is a huge monolithic structure, and there have been many suggestions recently that we break it down smaller chunks based on roles. The &amp;#8220;Number&amp;#8221; role would contain arithmetic vtables, while the &amp;#8220;invokable&amp;#8221; role would have VTABLE_invoke and friends. GCable role, Array role, Hash role, Metaobjecet role, etc. These are all things that we could use and would decrease overall memory footprint and increase flexibility in the system. Bonus points if this work was integrated closely with 6model and other proposed changes in our PMC subsystem.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are just a few of the ideas I have on the top of my head this morning. Some, I&amp;#8217;m sure, are too big. Others are too small. But in each is the kernel of a good idea and if anybody reading this is interested we should start the conversation now to get these vague ideas focused into compelling proposals.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/afwknight-parrot/~4/oAO5JU3LCdk" height="1" width="1"/&gt;</content>
    <feedburner:origLink>http://whiteknight.github.com/2012/02/15/gsoc_season_starting.html</feedburner:origLink></entry>
    
    <entry>
        <title>Rosella Reflect</title>
        <link href="http://feedproxy.google.com/~r/afwknight-parrot/~3/H0rkW-i5TQc/rosella_reflect.html" />
        <updated>2012-02-01T00:00:00-08:00</updated>
        <id>http://whiteknight.github.com/2012/02/01/rosella_reflect</id>
        <content type="html">&lt;p&gt;Earlier this month I released the new Reflect library in Rosella. I hadn&amp;#8217;t mentioned it before, but the library is sufficiently interesting that I want to talk about it at least a little bit. The Reflect library adds in tools for reflection. Somewhere, an etymologist weeps a tear of joy for the creative naming, I&amp;#8217;m sure.&lt;/p&gt;

&lt;p&gt;The Reflect library adds in wrappers for classes and packfiles that makes them easier to work with for many operations. First, I&amp;#8217;d like to use a couple code examples to show the most basic API:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// Get the Sub PMC that we&amp;#39;re currently executing
var s = Rosella.Reflect.get_current_sub();

// Get the current context
var c = Rosella.Reflect.get_current_context();

// Get the current object, if the current Sub is a method call
var obj = Rosella.Reflect.get_current_object();

// Get the class of the current object, if the current Sub is a method call
var c = Rosella.Reflect.get_current_class();

// Get a Module object for the packfile where the current Sub is defined
var m = Rosella.Reflect.get_current_module();

// Get a reflection wrapper object for the given Parrot Class PMC
var r = Rosella.Reflect.get_class_reflector(myClass);

// Get a Module object for the packfile in &amp;quot;foo/bar.pbc&amp;quot;, loading it as
// necessary
var m = Rosella.Reflect.Module.load(&amp;quot;foo/bar.pbc&amp;quot;);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&amp;#8217;s the basic API that the library provides to get basic information about where execution is happening at the moment when the call is made. Once you have a Module object or a Class reflector object, you can do all sorts of cool things that used to be a pain in the butt to do manually:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var m = Rosella.Reflect.get_current_module();
say(m);          // Stringified, produces the name and version of the packfile
m.load();        // Execute all :tag(&amp;quot;load&amp;quot;) and :load functions
n.init();        // Execute all :tag(&amp;quot;init&amp;quot;) and :init functions
say(m.version(); // Get the version string of the packfile &amp;quot;X.Y.Z&amp;quot;
say(m.path());   // The on-disk path to the current packfile

// Get a hash of all Class PMCs defined at compile-time (using the :method
// flag on Subs) defined in the packfile, keyed by name
var c = m.classes();

// Get a list of all non-:anon functions defined in the packfile
var f = m.functions();

// Get a hash of all non-:anon functions in the packfile, organized into
// a hash keyed by namespace
var f = m.functions_by_ns();

// Get a hash of all NameSpace PMCs defined at compile-time
var ns = m.namespaces();&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Once you have Class and NameSpace PMCs from the packfile, you can start to do all sorts of cool operations and analyses on them. Once you have a Class reflector object, you can do even more stuff with that:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var c = Rosella.Reflect.get_current_class();

// Create a new object of the current type
var o = c.new();

// Say the name of the class
say(c.name());

// Attributes are encapsulated as objects. You can get an Attribute
// reflector and use it later to get and set values on objects of this
// type or subclasses
var attr = c.get_attr(&amp;quot;foo&amp;quot;);
var value = attr.get_value(o);
attr.set_value(o, &amp;quot;whatever&amp;quot;);

// Methods are also encapsulated. You can get a method reflector now and
// invoke it on objects later (including objects of different types)
var method = c.get_method(&amp;quot;bar&amp;quot;);
var result = method.invoke(o);
var meths = c.get_all_methods();

// Basic capability detection. Determine if objects are members of the
// class or their subsets, and determine if the class can perform certain
// methods
if (c.isa(o)) { ... }
if (c.can(&amp;quot;bar&amp;quot;)) { ... }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I hope the code examples make up for the terse explanations.&lt;/p&gt;

&lt;p&gt;The Reflect library is currently focused on reading data from things like Classes and Packfiles, not on creating these things like the new PACT project is supposed to do. I want to extend this library even further with abilities to further introspect functions down to the opcode level and then&amp;#8230;Well, when we have a stream of opcodes to analyze the possibilities are endless. I&amp;#8217;d also like the ability to get better introspection of the interpreter and global state, though a cleaner interface than the hodge-podge of &lt;code&gt;interpinfo&lt;/code&gt; opcodes and ParrotInterpreter PMC methods and whatever else we currently use.&lt;/p&gt;

&lt;p&gt;As always, using the interface Rosella provides will help to insulate you from changes to the various underlying mechanisms when we finally get around to cleaning them up and making them sane. There isn&amp;#8217;t a huge push to make such cleanups on a large scale yet, but I wouldn&amp;#8217;t be surprised if a few things started getting prettified in the coming months at a slow pace.&lt;/p&gt;

&lt;p&gt;I&amp;#8217;ve already started using the new library in several of the Rosella utility programs such as those that create a winxed header file or a test suite from an existing packfile. In all cases the updated programs are both cleaner and have more functionality than the previous incarnations. Expect to see this library improve and grow in 2012 and beyond, and expect to see it work closely with PACT, once that project gets moving forward.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/afwknight-parrot/~4/H0rkW-i5TQc" height="1" width="1"/&gt;</content>
    <feedburner:origLink>http://whiteknight.github.com/2012/02/01/rosella_reflect.html</feedburner:origLink></entry>
    
    <entry>
        <title>Rosella Test Updates and Upgrades</title>
        <link href="http://feedproxy.google.com/~r/afwknight-parrot/~3/zuAtihOgeGE/rosella_test_cleanups.html" />
        <updated>2012-01-30T00:00:00-08:00</updated>
        <id>http://whiteknight.github.com/2012/01/30/rosella_test_cleanups</id>
        <content type="html">&lt;p&gt;The first half of this month was dominated with some epic illnesses between my family members and myself, family functions and home maintenance. What little spare time I&amp;#8217;ve had otherwise has been devoted to writing code, as opposed to writing blog posts about writing code. The blog has suffered.&lt;/p&gt;

&lt;p&gt;The past couple days I&amp;#8217;ve been working on Rosella&amp;#8217;s Test library. It&amp;#8217;s an old but good library and is, as far as I am aware, the most full-featured and easy to use testing tool in the Parrot ecosystem. With some of these most recent changes the library is better still.&lt;/p&gt;

&lt;h3 id='matchers'&gt;Matchers&lt;/h3&gt;

&lt;p&gt;Kakapo had a series of Matcher routines and objects as part of it&amp;#8217;s testing facilities, and for a long time I&amp;#8217;ve been wanting to port some of those ideas over to Rosella. As of last week, I have a simple version of them. Matchers allow you to ask the question &amp;#8220;is this thing like that thing&amp;#8221;, with a custom set of rules. Let me give a basic example.&lt;/p&gt;

&lt;p&gt;Previously in Rosella if you were unit testing a method which returned an array and you wanted to check that the array contained the right values, you would have to do something like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var result = obj.my_method();
var expected = [1, 2, 3, 4, 5];
self.assert.is_true(result != null);
self.assert.equal(elements(result), elements(expected));
for (int i = 0; i &amp;lt; elements(expected); i++) {
    self.assert.equal(result[i], expected[i]);
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&amp;#8217;s a lot of work, although you can cut it down a little bit if you know for certain that the array isn&amp;#8217;t null. With the new matcher functionality, you pass in two arrays and the Test library will match them for you:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var result = obj.my_method();
self.assert.is_match(result, [1, 2, 3, 4, 5]);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Internally the Test library maintains a list of matchers by name. When you pass in two objects, it loops over the list looking for a matcher that can handle the pair. In this case, one of the default matchers the library provides looks for objects which implement the &lt;code&gt;&amp;quot;array&amp;quot;&lt;/code&gt; role, and then does element-wise matching on them. Another similar matcher does the same for hash-like objects that implement the &lt;code&gt;&amp;quot;hash&amp;quot;&lt;/code&gt; role.&lt;/p&gt;

&lt;p&gt;Another matcher checks to see if one or both of the two objects are strings, and then does a string comparison on them (converting the other, if it isn&amp;#8217;t a string already) and the last of the default matchers is used to compare floating point values with a certain error tolerance.&lt;/p&gt;

&lt;p&gt;Since matchers are stored in a hash, you can access them by name, delete them, add your own, and replace existing ones if you want new matching semantics. This is especially useful in something like Parrot-Linear-Algebra, where I can say&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$!assert.is_match($matrix_a, $matrix_b);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&amp;#8230;and the library will automatically compare the dimensions of the matrices and the contents of them without needing nested loops and other distractions.&lt;/p&gt;

&lt;h3 id='nested_tap'&gt;Nested TAP&lt;/h3&gt;

&lt;p&gt;Another item I&amp;#8217;ve had on my wishlist for a while now has been nested TAP. I&amp;#8217;ve always wanted to support it, and in theory at least the system was designed modularly enough to generate it without too much hassle. Last weekend I put on the finishing touches and now am proud to say that Rosella.Test can run nested tests and generate nested TAP. At the moment the interface to use it is a little ugly (I&amp;#8217;m actively soliciting feedback!), but the capabilities are all there:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function my_test_method()
{
    self.status.suite().subtest(class MySubtestClass);
}

function my_vector_test_method()
{
    self.status.suite().subtest_vector(
        function(var a, var b) { ... },
        [1, 2, 3, 4, 5]
    );
}

function my_list_test_method()
{
    self.status.suite().subtest_list(
        function(var test) { ... },
        function(var test) { ... },
        function(var test) { ... }
    );
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here&amp;#8217;s an example of output from a similar test file in the Rosella suite:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;1..4
    1..2
    ok 1 - test_1A
    ok 2 - test_1B
    # You passed all 2 subtests
ok 1 - test_1
    1..3
    ok 1 - test 1
    ok 2 - test 2
    ok 3 - test 3
    # You passed all 3 subtests
ok 2 - test_2
    1..5
    ok 1 - test 1
    ok 2 - test 2
    ok 3 - test 3
    ok 4 - test 4
    ok 5 - test 5
    # You passed all 5 subtests
ok 3 - test_3
    1..1
    not ok 1 - test 1
    # failure
    # Called from &amp;#39;fail&amp;#39; (rosella/test.winxed : 481)
    # Called from &amp;#39;&amp;#39; (t/winxed_test/Nested.t : 40)
    # Called from &amp;#39;&amp;#39; (rosella/test.winxed : 1589)
    # Looks like you failed 1 of 1 subtests run
ok 4 - test_4
# You passed all 4 tests&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The fourth test expects a failure in the subtest, which is why it says it passes when there is clearly some failure diagnostics appearing. This brings me to my next point&amp;#8230;&lt;/p&gt;

&lt;h3 id='cleaner_diagnostics'&gt;Cleaner Diagnostics&lt;/h3&gt;

&lt;p&gt;Before when you ran a test and had a failure, you might see something like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;not ok 2 - ooopsie_doopsies
# objects not equal &amp;#39;0&amp;#39; != &amp;#39;1&amp;#39;
# Called from &amp;#39;throw&amp;#39; (rosella/test.winxed : 851)
# Called from &amp;#39;internal_fail&amp;#39; (rosella/test.winxed : 1853)
# Called from &amp;#39;fail&amp;#39; (rosella/test.winxed : 481)
# Called from &amp;#39;equal&amp;#39; (rosella/test.winxed : 577)
# Called from &amp;#39;ooopsie_doopsies&amp;#39; (t/core/Error.t : 18)
# Called from &amp;#39;execute_test&amp;#39; (rosella/test.winxed : 1455)
# Called from &amp;#39;__run_test&amp;#39; (rosella/test.winxed : 1483)
# Called from &amp;#39;run&amp;#39; (rosella/test.winxed : 1392)
# Called from &amp;#39;test&amp;#39; (rosella/test.winxed : 1747)
# Called from &amp;#39;_block1000&amp;#39; (t/core/Error.t : 7)
# Called from &amp;#39;_block1177&amp;#39; ( : 158)
# Called from &amp;#39;eval&amp;#39; ( : 151)
# Called from &amp;#39;evalfiles&amp;#39; ( : 0)
# Called from &amp;#39;command_line&amp;#39; ( : 0)
# Called from &amp;#39;main&amp;#39; ( : 1)
# Called from &amp;#39;(entry)&amp;#39; ( : 0)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&amp;#8217;s a huge mess, and it&amp;#8217;s a mess from two sides. At the top of the backtrace, you see all sorts of Rosella internal functions involved in the assertion and error handling. The bottom half of the backtrace is devoted to entry-way stuff. In this case there&amp;#8217;s NQP-related entry code and then the Rosella entry code. You, as the test writer, don&amp;#8217;t care about any of that. All you care about is the code you wrote and where its broken. If you have to dig through a huge backtrace to figure out where the error is, that&amp;#8217;s a big waste of time and effort.&lt;/p&gt;

&lt;p&gt;Now, Rosella filters that crap out for you. Here&amp;#8217;s the same exact failure with the new backtrace reporting:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;not ok 2 - ooopsie_doopsies
# objects not equal &amp;#39;0&amp;#39; != &amp;#39;1&amp;#39;
# Called from &amp;#39;equal&amp;#39; (rosella/test.winxed : 577)
# Called from &amp;#39;ooopsie_doopsies&amp;#39; (t/core/Error.t : 18)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here you see the important parts of the backtrace only: The parts you wrote and the one assertion that failed. You don&amp;#8217;t see the internal garbage, you don&amp;#8217;t see the entry-way garbage, because those things aren&amp;#8217;t of interest to the test writer.&lt;/p&gt;

&lt;h3 id='parrotlinearalgebra'&gt;Parrot-Linear-Algebra&lt;/h3&gt;

&lt;p&gt;Another small project I did a few days ago was getting the PLA test suite working again. It&amp;#8217;s a testament to how stable both BLAS and Parrot&amp;#8217;s extending interfaces are. Recent Rosella refactors removed some of the special-purpose features that existed only for PLA and for no other reason (and which were a pain in the butt to maintain). I fixed up the test suite and PLA builds and runs perfectly now.&lt;/p&gt;

&lt;p&gt;That&amp;#8217;s what I&amp;#8217;ve been up to this month. I&amp;#8217;m mostly done with my cleanups to the Test library now, barring a few more interface improvements I want to make. After that I&amp;#8217;ve got a few projects to tackle inside libparrot itself. I&amp;#8217;ll write more about those topics when I have something to say.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/afwknight-parrot/~4/zuAtihOgeGE" height="1" width="1"/&gt;</content>
    <feedburner:origLink>http://whiteknight.github.com/2012/01/30/rosella_test_cleanups.html</feedburner:origLink></entry>
    
    <entry>
        <title>Parrot 4.0.0 "Hyperstasis" Released!</title>
        <link href="http://feedproxy.google.com/~r/afwknight-parrot/~3/nxJqYoJ_oGw/parrot_4_hyperstasis.html" />
        <updated>2012-01-17T00:00:00-08:00</updated>
        <id>http://whiteknight.github.com/2012/01/17/parrot_4_hyperstasis</id>
        <content type="html">&lt;blockquote&gt;
&lt;p&gt;At one extreme, it is possible to approach the subject on a high mathematical epsilon-delta level, which generally results in many undergraduate students not knowing what&amp;#8217;s going on. At the other extreme, it is possible to wave away all the subtleties until neither the student nor the teacher knows what&amp;#8217;s going on.&lt;/p&gt;

&lt;p&gt;-Stanley J. Farlow, Preface to Partial Differential Equations for Scientists and Engineers&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;On behalf of the Parrot team, I&amp;#8217;m proud to announce Parrot 4.0.0, also known as &amp;#8220;Hyperstasis&amp;#8221;. &lt;a href='http://parrot.org/'&gt;Parrot&lt;/a&gt; is a virtual machine aimed at running all dynamic languages.&lt;/p&gt;

&lt;p&gt;Parrot 4.0.0 is available on &lt;a href='ftp://ftp.parrot.org/pub/parrot/releases/stable/4.0.0/'&gt;Parrot&amp;#8217;s FTP site&lt;/a&gt;, or by following the download instructions at &lt;a href='http://parrot.org/download'&gt;http://parrot.org/download&lt;/a&gt;. For those who would like to develop on Parrot, or help develop Parrot itself, we recommend using Git to retrieve the source code to get the latest and best Parrot code.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Parrot 4.0.0 News:
    - Core
        + Several cleanups to the interp subsystem API
        + Cleanups and documentation additions for green threads and timers
        + Iterator PMC and family now implement the &amp;quot;iterator&amp;quot; role
        + A bug in Parrot_ext_try was fixed where it was not popping a context correctly
    - Documentation
        + Docs for all versions of Parrot ever released are now available
          at http://parrot.github.com
    - Tests
        + Timer PMC tests were converted from PASM to PIR&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The SHA256 message digests for the downloadable tarballs are:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;a1e0bc3de509b247b2cea4863cc202cdceeaa329729416115d3c20a162a0dd88 parrot-4.0.0.tar.bz2
a63d45f50f7dd8ba76395cd2af14108412398ac24b8d827db369221cdb37fada parrot-4.0.0.tar.gz&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Many thanks to all our contributors for making this possible, and our sponsors for supporting this project. Our next scheduled release is 21 February 2012.&lt;/p&gt;

&lt;p&gt;Enjoy!&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/afwknight-parrot/~4/nxJqYoJ_oGw" height="1" width="1"/&gt;</content>
    <feedburner:origLink>http://whiteknight.github.com/2012/01/17/parrot_4_hyperstasis.html</feedburner:origLink></entry>
    
    <entry>
        <title>Rosella Date</title>
        <link href="http://feedproxy.google.com/~r/afwknight-parrot/~3/dtnXBYahEKQ/rosella_date.html" />
        <updated>2012-01-15T00:00:00-08:00</updated>
        <id>http://whiteknight.github.com/2012/01/15/rosella_date</id>
        <content type="html">&lt;p&gt;The Advent calendar idea ended up terribly. Let us never speak of it again. The holiday season was particularly busy this year with a higher-than-average load of family- friend- and work-related activities. Combine that with an unexpected (and absolutely unappreciated) invasion of a particularly unpleasant and long-lasting stomach bug, and you have something of a perfect storm. I won&amp;#8217;t go into the details any further on this particular blog, but I will mention in passing that I&amp;#8217;ve become extremely suspicious about the basic hygene habits of the other little turdbags that my son goes to daycare with.&lt;/p&gt;

&lt;p&gt;To start 2012 off, and to get back into the swing of coding-for-pleasure, yesterday I went through and got the new Rosella Date library ready for prime time. The library is imperfect and incomplete, but those are things that can be fixed using the patented Andrew Style (tm) of meandering iterative development. For now, however, the library does seem to work well enough for basic tasks and it&amp;#8217;s already proven to be a valuable tool for some tasks. Here I&amp;#8217;m going to introduce the new library and talk about some of the things I changed in Rosella to support it and some of the ways I&amp;#8217;ve already integrated it into the rest of the collection.&lt;/p&gt;

&lt;h3 id='string_formatters'&gt;String Formatters&lt;/h3&gt;

&lt;p&gt;When you use &lt;code&gt;sprintf&lt;/code&gt; to print out an integer (for instance) you can use some basic modifiers to control how the value is printed. &lt;code&gt;%d&lt;/code&gt; prints out the basic version, but you can specify field width, padding, alignment and a few other details by using a format specifier like &lt;code&gt;%-02d&lt;/code&gt;. Or, if you want to print the same value out as hex instead of base-10, you can use &lt;code&gt;%x&lt;/code&gt; or &lt;code&gt;%X&lt;/code&gt;, and use modifiers with those as well.&lt;/p&gt;

&lt;p&gt;The problem with something more complex like a date/time representation is that sprintf is not able to handle them natively and some kind of mapping is needed.&lt;/p&gt;

&lt;p&gt;Rosella has added a new &lt;code&gt;StringFormatter&lt;/code&gt; type to the Core library to help with this problem. A StringFormatter is a type that takes an object and a format string and outputs a new string according to the two. The default StringFormatter uses sprintf internally, but other formatters may use different mechanisms and syntaxes.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var sf = new Rosella.StringFormatter();
string s = sf.format(my_obj, &amp;quot;This is %s&amp;quot;);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As an aside, this does demonstrate the fact that our &lt;code&gt;get_string&lt;/code&gt; vtable really is insufficient for a lot of purposes. I suggest that &lt;code&gt;get_string&lt;/code&gt; should take a parameter for a format string. We could easily incorporate that into the default sprintf implementation like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$S0 = sprintf &amp;quot;%{foobar}p&amp;quot;, $P0&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In that invocation, the &lt;code&gt;get_string&lt;/code&gt; vtable on the first parameter would be called with the string argument &amp;#8220;foobar&amp;#8221;. A normal invocation like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$S0 = sprintf &amp;quot;%p&amp;quot;, $P0&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&amp;#8230;would call &lt;code&gt;get_string&lt;/code&gt; with a null format and the behavior could be whatever the default string representation for that type is. By overriding &lt;code&gt;get_string&lt;/code&gt; in your types to take different formats or to respond to common formats differently, you could have pretty detailed control over stringification at all levels.&lt;/p&gt;

&lt;h3 id='date_library'&gt;Date Library&lt;/h3&gt;

&lt;p&gt;The new Date library provides a Date type for working with dates and times. You can create one in three ways:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var d = new Rosella.Date(t);
var d = new Rosella.Date(year, month, day);
var d = new Rosella.Date(year, month, day, hour, minute, second);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The first uses a system-specific time integer value to represent a time since the system epoch. This is the kind of value you get from the &lt;code&gt;time&lt;/code&gt; opcode, or from &lt;code&gt;stat&lt;/code&gt; calls on filesystem objects, for instance. In the third option, hours are specified on a 24-hour clock.&lt;/p&gt;

&lt;p&gt;There are also a few functions you can call to get particular dates:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var d = Rosella.Date.now();
var n = Rosella.Date.min();
var x = Rosella.Date.max();&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The first value should be the current date/time (as gotten from Parrot&amp;#8217;s &lt;code&gt;time&lt;/code&gt; opcode). The second value is a minimum date object which corresponds to the minimum possible date value to display and is guaranteed to be evaluated as less than any other date. The third one similarly is a maximum date value which corresponds to the maximum possible date and is guaranteed to always be compared greater than any other date.&lt;/p&gt;

&lt;p&gt;Dates are immutable. Once you create them, they cannot be modified in-place. Instead, several operations are provided to perform operations and return new Date objects with the results. Here are some examples:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var d = Rosella.Date.now();
var e = d.add_seconds(20);
e = d.add_hours(15);
e = d.add_months(24);
e = d.add_years(1000);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In each case, the variable &lt;code&gt;e&lt;/code&gt; becomes a new date value and &lt;code&gt;d&lt;/code&gt; is left unmodified. Two other methods let you pick out just the date components or just the time components:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var n = Rosella.Date.now();
var d = n.date();
var t = n.time();&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Finally, you can use a new DateFormatter type to format the date value into a proper stringification:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var d = Rosella.Date.now();
string s = d.format_string(&amp;quot;yyyy - MM - dd and the time is: hh:mm:ss&amp;quot;);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;At the moment the formatter is dirt simple and only supports a few formatting codes such as &lt;code&gt;yyyy&lt;/code&gt;, &lt;code&gt;MM&lt;/code&gt;, &lt;code&gt;dd&lt;/code&gt;, &lt;code&gt;hh&lt;/code&gt;, &lt;code&gt;mm&lt;/code&gt;, and &lt;code&gt;ss&lt;/code&gt;. I will be making it much more useful in the coming days, if I can settle on an algorithm which doesn&amp;#8217;t completely stink.&lt;/p&gt;

&lt;h3 id='filesystem'&gt;FileSystem&lt;/h3&gt;

&lt;p&gt;The FileSystem library now returns Date objects from certain file-time methods:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var f = new Rosella.FileSystem.File(&amp;quot;t/harness&amp;quot;);
var ct = f.change_time();
var at = f.access_time();
var mt = f.modify_time();&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In each case, the value returned from the &lt;code&gt;stat&lt;/code&gt; call on the file is used to create a new Date object.&lt;/p&gt;

&lt;h3 id='query'&gt;Query&lt;/h3&gt;

&lt;p&gt;Dates are completely comparible, so you can sort them and work with them like other values in an iterable:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var a = [
    Rosella.Date.now(),
    Rosella.Date.max(),
    Rosella.Date.min()
];
Rosella.Query.iterable(a)
    .sort()
    .foreach(function(d) { say(d); });&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This toy example sorts the three Date values, with the min first, then now, then max, and prints them out to the console. The stringified versions of the special min/max dates aren&amp;#8217;t particularly instructive, but they do get the point across.&lt;/p&gt;

&lt;p&gt;So that&amp;#8217;s the new Date library. It does need more functionality and definitely needs more tests, but it is working pretty well for me now and has already proven itself useful for a number of purposes. Expect to see more of it in 2012.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/afwknight-parrot/~4/dtnXBYahEKQ" height="1" width="1"/&gt;</content>
    <feedburner:origLink>http://whiteknight.github.com/2012/01/15/rosella_date.html</feedburner:origLink></entry>
    
    <entry>
        <title>Advent 9 - Jaesop</title>
        <link href="http://feedproxy.google.com/~r/afwknight-parrot/~3/USCDfAnl0Ck/advent_9_jaesop.html" />
        <updated>2011-12-29T00:00:00-08:00</updated>
        <id>http://whiteknight.github.com/2011/12/29/advent_9_jaesop</id>
        <content type="html">&lt;p&gt;Over the summer of 2011 we had a GSOC project that was trying to build a JavaScript compiler for Parrot. That project made some interesting progress but ultimately I think the approach ended up being flawed. That project attempted to convert JavaScript into PIR code, which is a pretty big gap in terms of both syntax and semantics.&lt;/p&gt;

&lt;p&gt;Towards the end of the summer, after it was too late to go back and start from the beginning, I had a different idea: What if we tried to generate Winxed code instead of PIR code? Winxed would handle things like register allocation, and the Winxed syntax is so similar in some places that the translation could almost be verbatim copying. I put those ideas together with node.js and the cafe compiler and Jaesop was born.&lt;/p&gt;

&lt;p&gt;Jaesop intends to be a full JavaScript on Parrot compiler. The plan is to write as much of it in JavaScript itself as possible, and bootstrap upwards. The first component, &amp;#8220;stage 0&amp;#8221; uses node.js to run a converter that compiles Javascript code into winxed. That&amp;#8217;s the only part we have working right now, but what we do have is working pretty well.&lt;/p&gt;

&lt;p&gt;In 2012 I want to get the next component, &amp;#8220;stage 1&amp;#8221; working. Stage 1 will use the stage 0 compiler to compile itself. It will be written in JavaScript (translated to Winxed en passant) and will be running entirely on Parrot. My goal for stage 1 is to be generating bytecode directly instead of generating either Winxed or PIR as an intermediary. That&amp;#8217;s going to require some serious help from a compiler toolkit of some variety and I have very high hopes for using PACT for this purpose when it&amp;#8217;s ready.&lt;/p&gt;

&lt;p&gt;Stage 0 works very well and passes a small but interesting test suite I&amp;#8217;ve set up for it. It does not self-compile, but there are only a few relatively small things standing in the way. For instance, regular expression support and pcre bindings are not complete yet, and the grammar currently requires semicolons at the end of statements but the code generated from Jison does not always contain semicolons. I also haven&amp;#8217;t built in the &lt;code&gt;require()&lt;/code&gt; function, which is used by the stage 0 compiler to load in the various code files. These are all small issues and with a small amount of work I expect stage 0 to be able to self-host. Whether I want to expend that effort or focus attention on stage 1 instead is a different question entirely.&lt;/p&gt;

&lt;p&gt;In 2012, once PACT has matured and 6model has been integrated into Parrot I expect to get back to work aggressively on Jaesop. I&amp;#8217;m looking for helpers too, in case anybody reading this wants to get involved in the development of a new JavaScript compiler. I&amp;#8217;ll put out more of a call to arms when the prerequisites are in place and we&amp;#8217;re ready to get the wheels turning on stage 1. I estimate that by spring or early summer we should be ready to get started on the next phase of Jaesop development.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/afwknight-parrot/~4/USCDfAnl0Ck" height="1" width="1"/&gt;</content>
    <feedburner:origLink>http://whiteknight.github.com/2011/12/29/advent_9_jaesop.html</feedburner:origLink></entry>
    
    <entry>
        <title>Advent 8 - Rosella</title>
        <link href="http://feedproxy.google.com/~r/afwknight-parrot/~3/ILg-PHclAQ8/advent_8_rosella.html" />
        <updated>2011-12-28T00:00:00-08:00</updated>
        <id>http://whiteknight.github.com/2011/12/28/advent_8_rosella</id>
        <content type="html">&lt;p&gt;I had already written a post about Rosella for my woefully inadequate advent calendar, but it got lost to the sands of time (I think it&amp;#8217;s on my other computer, committed but not pushed). Considering that I should have been on schedule to be on post #21 or #22 by now but am instead only on #8, I can&amp;#8217;t really afford to not write a post when I have the ideas inside me. This post is also going to be about Rosella, and if I ever find the first one I may post that as well. I clearly haven&amp;#8217;t had enough spare writing time this month to be even remotely picky about what I post or when.&lt;/p&gt;

&lt;p&gt;I&amp;#8217;m thinking I might like to try this little experiment again later in the year, when I&amp;#8217;ve had more time to prepare and have fewer other things in real life demanding my undivided attention. Maybe we&amp;#8217;ll shoot for some kind of christmas-in-July thing. Until then, I&amp;#8217;ll happily conceed the &amp;#8220;best Advent Calendar&amp;#8221; crown back to moritz and the Perl peoples.&lt;/p&gt;

&lt;p&gt;Rosella is a library project I started as a way to let me work on lots of different project ideas I had without having to duplicate build and test infrastructure. The goals from the project were solidified pretty early in the project lifecycle and have remained unchanged ever since: To provide solutions to common developer problems, in pure Parrot, in ways that are portable, reusable, and configurable. I&amp;#8217;ve already talked about three of the oldest and most important libraries in the set: Test, Harness and MockObject on Day 5 of this Advent calendar. My other written-but-not-published post talked about three additional libraries as well: Query, FileSystem and Proxy. To avoid much duplicate effort in case I do ever find that post again, I&amp;#8217;ll write about a few different libraries: Container, Random and Template.&lt;/p&gt;

&lt;p&gt;The Container library is one of the oldest libraries in Rosella. It is an implementation of a dependency injection container for Parrot, and originally lived in it&amp;#8217;s own Parrot-Container repository on github. My other library, for unit testing was also a separate repo. One day I was thinking about how I might like to use the Parrot-Container project to manage dependency injection in the Harness library, and trying to figure out how I wanted to manage the software dependencies. I wanted to use the Container library in the implementation of the unit test Harness, but I wanted to use the Harness and Test libraries to implement the test suite for the Container library repository. Combine that with a bunch of other ideas for new libraries I had brewing in the back of my mind and Rosella was quickly born.&lt;/p&gt;

&lt;p&gt;The Container library is not quite as easy to use as a counter-part in the .NET or JVM environments because things are not always strongly typed, especially not at compile time or early in runtime when the container is initializing but the various bits of initialization logic in the program have not yet run. The Rakudo folks with their fancy object system and notion of gradual typing might have a different idea, but in terms of pure-parrot code, as Parrot exists right now, we can&amp;#8217;t query a Sub PMC and ask it what types of parameters it expects to receive. For people who might be familiar with other dependency injection (or &amp;#8220;inversion of control&amp;#8221;) systems like Unity or Ninject, the syntax for setting up Rosella&amp;#8217;s container type might seem a little verbose. It&amp;#8217;s plenty powerful (and I have plenty of plans to upgrade things as Parrot&amp;#8217;s threading support and object system improve in 2012 and beyond), but it is verbose:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var container = new Rosella.Container();
container
    .register(class Foo)
    .register(class Bar,
        new Rosella.Container.Resolver.TypeConstructor(class Bar, &amp;quot;Bar&amp;quot;, 1, 2, 3)
        new Rosella.Container.LifetimeManager.Permanent()
    )
    .register(class Baz,
        new Rosella.Container.Resolver.TypeConstructor(class Baz, &amp;quot;BUILD&amp;quot;
            new Rosella.Container.Argument.Resolve(class Foo),
            new Rosella.Container.Argument.Resolve(class Bar)
        )
        new Rosella.Container.Option.Attribute(&amp;quot;my_attr&amp;quot;, &amp;quot;value&amp;quot;),
        new Rosella.Container.LifetimeManager.Thread()
    )
    .alias(class Baz, &amp;quot;Baz&amp;quot;);
var b = container.resolve(class Bar);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The part that is significantly more verbose here than you would expect in Unity for example is the part where I have to specify the types of arguments to pass to the constructor. In a platform like .NET, the container can read the type metadata from the constructor object itself and make that decision. In Parrot, since we currently don&amp;#8217;t make that kind of information available, you must specify. In Rosella&amp;#8217;s offering you do have many of the features that you would expect from one of the more well-known containers: the ability to specify registration lifetimes, the ability to initialize objects by making method calls and setting attribute values after construction, the ability to specify global singleton instances, etc. It is a pretty cool tool, but I haven&amp;#8217;t yet made as much use of it as I would have liked. In 2012 I expect to start integrating the container library into some of the other libraries, such as Harness, CommandLine and Template, to make user configuration easier and more straight-forward.&lt;/p&gt;

&lt;p&gt;The Random library is a relative newcomer to Rosella, but is already demonstrating its usefulness in a variety of ways. The Random library was born from a few ideas I had turned into GCI tasks. An intrepid young student wrote an implementation of the Mersenne Twister algorithm for me in Winxed, and I set about writing up several other components such as a Box-Muller transform, a UUID generator, a Fisher-Yates array shuffler and a few other things. Now, you can do cool things with random numbers:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;var prng = Rosella.Random.default_uniform_random()
int r = prng.get_int();

var uuid = Rosella.Random.UUID.new_uuid();
string id = string(uuid);

var a = [1, 2, 3, 4, 5, 6, 7, 8];
Rosella.Random.shuffle_array(a);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Implementations of things aren&amp;#8217;t all perfect and there are a handful of bugs to be worked out (especially bugs resulting from arithmetic differences between 32-bit and 64-bit machines) but it is already very usable and reliable for the most part. If you need a random number generator, or a UUID generator, or other random-related things, this is a very nice tool to have available.&lt;/p&gt;

&lt;p&gt;The Template library is something I wanted to write for a long time, but had to wait until all the prerequisites were in place first. Template is a text-templating engine library. You create an Engine object and feed it two basic pieces of information: The string template to use and the data context object to fill in the blanks. Presto-chango, out comes the complete text.&lt;/p&gt;

&lt;p&gt;The Template engine can execute basic logical operations depending on the values in the data context, it can compile and execute inline snippets of code, it can load and assemble pieces of template from separate files, and do several other things that you would expect a templating library to do. I could write many examples of templates and their use, but I&amp;#8217;ll stick with only one small example here for brevity:&lt;/p&gt;

&lt;p&gt;Template:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Let&amp;#39;s learn about &amp;lt;%
    var bar = context[&amp;quot;animals&amp;quot;];
    return elements(bar);
%&amp;gt; new animals!
&amp;lt;$ for sound in animals $&amp;gt;
    The &amp;lt;# __KEY__ #&amp;gt; says &amp;lt;# sound #&amp;gt;
&amp;lt;$ endfor $&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;string template = ...;
var context = {
    &amp;quot;animals&amp;quot; : {
        &amp;quot;Cow&amp;quot; : &amp;quot;Moo&amp;quot;,
        &amp;quot;Bear&amp;quot; : &amp;quot;Growl!&amp;quot;,
        &amp;quot;Cat&amp;quot; : &amp;quot;I CAN HAZ CHEESEBURGER&amp;quot;
    }
};
var engine = new Rosella.Template.Engine();
string output = engine.generate(template, context);&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And the end result would be something like this (minus hash ordering concerns):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Let&amp;#39;s learn about 3 new animals!
The Cow says Moo
The Bear says Growl!
The Cat says I CAN HAZ CHEESEBURGER&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Rosella eats plenty of it&amp;#8217;s own dogfood here. Several test files in the Rosella test suite and a few boiler-plate source code files are generated using the Rosella Template library. I&amp;#8217;m planning to start using it to generate skeleton files for Rosella documentation as well. In the future, I may also use it to help with generating content for this very blog!&lt;/p&gt;

&lt;p&gt;In 2012 Rosella is going to be adding a bunch of cool new stuff. Considering how far Rosella has come by now and the fact that it&amp;#8217;s less than a year old, it&amp;#8217;s kind of hard to speculate where we will be next year around this time. I&amp;#8217;m planning to add a new Date/Time library within the next few weeks. I&amp;#8217;m also planning a new reflection/packfile library, a benchmarking library, a code assertions library, and rewrites to several of the existing libraries to add new functionality and optimize performance in some key ways. Those are only my plans for the first two or three months of 2012!&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/afwknight-parrot/~4/ILg-PHclAQ8" height="1" width="1"/&gt;</content>
    <feedburner:origLink>http://whiteknight.github.com/2011/12/28/advent_8_rosella.html</feedburner:origLink></entry>
    
    <entry>
        <title>Advent 7 - Google GCI and GSoC</title>
        <link href="http://feedproxy.google.com/~r/afwknight-parrot/~3/v4veMtBkU-A/advent_7_google.html" />
        <updated>2011-12-22T00:00:00-08:00</updated>
        <id>http://whiteknight.github.com/2011/12/22/advent_7_google</id>
        <content type="html">&lt;p&gt;When you want to find something on the internet, Google is a pretty popular tool to use. When you want to get some code written, Google turns out to also be a pretty good idea.&lt;/p&gt;

&lt;p&gt;Google has been doing it&amp;#8217;s Summer Of Code (GSOC) program for several years now and every summer it&amp;#8217;s a smashing success. Last year they started up with a new program called the Google Code In (GCI). That program, while wildly different from GSOC, is also incredibly awesome. Every year Parrot receives a huge amount of code from these programs, and we would not be where we are today without them.&lt;/p&gt;

&lt;p&gt;The Summer of Code program is very straight forward: Every year we receive applications from college age and some highschool-aged students for projects. Over the course of the summer the accepted applicants work diligently and at the end, if they are successful, they get paid. Also there are cool T-shirts. The GCI program is aimed at younger students, mostly those in high school. Instead of one large project spanning several months, the GCI students work on a large number of bite-sized tasks from many organizations. Each task is worth points, and the students who have the most points at the end of the program receive a prize. Also, I think there are monetary rewards for completing a certain number of tasks.&lt;/p&gt;

&lt;p&gt;Last year we had a huge number of tasks completed by several GCI students. We had many bits of code written or re-written, some new documentation written, and many many tests added. Our project-wide test coverage metrics increased dramatically, since we had several tasks devoted to test coverage and several talented young coders who were chasing them down. Once you figure out how to write tests, subsequent tasks go much more quickly. We had some students who, after getting the hang of things, were able to complete several tasks per day at the end.&lt;/p&gt;

&lt;p&gt;I heard one or two accusations that Parrot was inflating point values by offering tasks that could be completed so quickly. I pointed out that many tasks were considered very hard by students at the beginning, but as their familiarity of the code increased the relative difficulty decreased. It wasn&amp;#8217;t a problem of Parrot&amp;#8217;s tasks being too easy, but the students learning and improving much faster than we could keep up with. By the end of the program we were learning that the difficulty really needed to ramp up over the course of GCI, because students would be capable of much more towards the end than they would be at the beginning.&lt;/p&gt;

&lt;p&gt;This year things are going a little bit more slowly. We have fewer of the &amp;#8220;increase test coverage&amp;#8221; tasks, because our test coverage is still so high from last year in the core VM. I&amp;#8217;ve scoured several potential sources of tasks including searching for TODO notes in the Parrot source code, scanning through our myriad of trac tickets, digging into ecosystem projects (Plumage and Rosella especially) and begging other contributors for task ideas. Already we&amp;#8217;ve seen some very useful bits of code contributed to the project, including new features and impressive cleanups and refactorings of old crufty code.&lt;/p&gt;

&lt;p&gt;GCI this year is essentially closed off. There were two opportunities to publish new tasks and those have both passed. Students are slowly working their way through the remaining tasks in the queue until the program ends in a few weeks. However, next year I&amp;#8217;m sure we&amp;#8217;re going to see both another GSOC and another round of GCI (At least, I hope we see them, since these are both so awesome!).&lt;/p&gt;

&lt;p&gt;Despite being months away we&amp;#8217;re already starting to look for project ideas and potential applicants for GSOC. Getting good ideas picked early allows us time to refine them, and getting familiar with potential applications now helps us to learn more about them and be more comfortable with them when time comes to make final selections. GCI doesn&amp;#8217;t require so much preparatory work, but it would be nice if we went into next year with a larger pile of varied tasks for students to work on, instead of needing to scramble to create tasks in sufficient numbers at the last minute.&lt;/p&gt;

&lt;p&gt;GSOC and GCI have both been amazingly successful programs in the past and I am hoping that trend continues into the future. Parrot has benefited from both programs to an amazing degree, and with a little bit of luck and a lot of planning we can keep the train moving into 2012 and beyond.&lt;/p&gt;

&lt;p&gt;If you&amp;#8217;re a highschool or college-aged coder, or know somebody who is, and would like to talk about getting involved with either program in 2012 (especially if you would like to work with Parrot specifically!) please let me know and I can make sure you get pointed in the right direction.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/afwknight-parrot/~4/v4veMtBkU-A" height="1" width="1"/&gt;</content>
    <feedburner:origLink>http://whiteknight.github.com/2011/12/22/advent_7_google.html</feedburner:origLink></entry>
    
    <entry>
        <title>Advent 6 - Embedding</title>
        <link href="http://feedproxy.google.com/~r/afwknight-parrot/~3/NggI1156N9A/advent_6_embedding.html" />
        <updated>2011-12-17T00:00:00-08:00</updated>
        <id>http://whiteknight.github.com/2011/12/17/advent_6_embedding</id>
        <content type="html">&lt;p&gt;In late 2010 and early 2011 we spent a good amount of effort building a new embedding API for Parrot. I would like to say that the new API replaced an older, inferior API but that&amp;#8217;s not really the case. We didn&amp;#8217;t really have an old embedding API per se. We had a mismash of functions in a file called &lt;code&gt;embed.c&lt;/code&gt;, but they hardly represented a consistent API, much less a complete set of things that an embedder would need. If anything the old embedding API was the entirety of all publicly exported functions from libparrot combined with a handful of utility functions that embedders in the past also needed.&lt;/p&gt;

&lt;p&gt;In short, it was a mess. By early 2011 we had a much nicer API around to play with. Now that 2011 is almost over, the new API is considered to be extremely stable and robust.&lt;/p&gt;

&lt;p&gt;Last December I started a project called ParrotSharp which embeds a Parrot Interpreter into the .NET CLI with C#. I haven&amp;#8217;t shown that project too much love in recent months, but as of today it&amp;#8217;s still building and seems to run correctly (although my IDE is telling me it can&amp;#8217;t find NUnit on my system, so it won&amp;#8217;t run my tests). That has to tell you something, when code I wrote months ago with the embedding API still works correctly even though so many things have changed in Parrot since then.&lt;/p&gt;

&lt;p&gt;Parrot&amp;#8217;s embedding API is a little bit verbose but very easy and straight forward to use. Also, all API functions return a true/false status value, so calls can easily be chained together. Here is an example of the embedding API in action:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;int main(int argc, char** argv) {
    Parrot_PMC interp, bytecodepmc, args;
    Parrot_Init_Args *initargs;
    Parrot_String filename;

    GET_INIT_STRUCT(initargs);

    if (!(
        Parrot_api_make_interpreter(NULL, 0, initargs, &amp;amp;interp) &amp;amp;&amp;amp;
        Parrot_api_set_executable_name(interp, argv[0]) &amp;amp;&amp;amp;
        Parrot_api_pmc_wrap_string_array(interp, argc, argv, &amp;amp;args) &amp;amp;&amp;amp;
        Parrot_api_string_import(interp, &amp;quot;foo.pbc&amp;quot;, &amp;amp;filename) &amp;amp;&amp;amp;
        Parrot_api_load_bytecode_file(interp, filename, &amp;amp;bytecodepmc) &amp;amp;&amp;amp;
        Parrot_api_run_bytecode(interp, bytecodepmc, args)
    )) {
        Parrot_String errmsg, backtrace;
        Parrot_Int exit_code, is_error;
        Parrot_PMC exception;

        Parrot_api_get_result(interp, &amp;amp;is_error, &amp;amp;exception, &amp;amp;exit_code, &amp;amp;errmsg);
        if (is_error) {
            Parrot_api_get_exception_backtrace(interp, exception, &amp;amp;backtrace);
            // Print out exception information to the console, or whatever
        }
    }

    Parrot_api_destroy_interpreter(interp);
    exit(exit_code);
 }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is, essentially, a simple program to execute a bytecode file. However, it does show some of the basics of the embedding API. Every function returns a true/false, pass/fail status bit. All data types passed around are properly wrapped Parrot_PMC or Parrot_String types and it almost never uses any other raw pointer types. Also since we&amp;#8217;re using PMC and STRING types, Parrot&amp;#8217;s GC manages all the memory for you and you don&amp;#8217;t need to be freeing things or cleaning things up (except for the interpreter itself).&lt;/p&gt;

&lt;p&gt;This example above only shows a handful of API functions, but there are several dozen of them in the API and more can be easily added. We have API routines for performing a variety of actions on Strings and PMCs. We have API routines for loading, executing and writing bytecode. The API has decent defaults so you can just get an interpreter up and running quickly if you want, but it also have a variety of routines for tweaking and configuring the interpreter too. And, like I said (and I&amp;#8217;ll say it a million times more if I have to) we can always add new methods to the embedding API if there is a need.&lt;/p&gt;

&lt;p&gt;We have at least the basics of a C# wrapper projects in the wings, and I&amp;#8217;ve been planning a proper C++ wrapper for a while too but I haven&amp;#8217;t gotten around to it yet. That would make an excellent, smallish project for an intrepid newcomer to work on, especially one that knows C++. I like to think it should be easy to embed Parrot as a plugin for things like text editors or other pluggable unixy programs, but I haven&amp;#8217;t taken the time to really dig into any of them yet. This might make another great project for an eager new parrot hacker.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/afwknight-parrot/~4/NggI1156N9A" height="1" width="1"/&gt;</content>
    <feedburner:origLink>http://whiteknight.github.com/2011/12/17/advent_6_embedding.html</feedburner:origLink></entry>
    
    <entry>
        <title>Advent 5 - Testing</title>
        <link href="http://feedproxy.google.com/~r/afwknight-parrot/~3/ujz_h0UKi7U/advent_5_testing.html" />
        <updated>2011-12-16T00:00:00-08:00</updated>
        <id>http://whiteknight.github.com/2011/12/16/advent_5_testing</id>
        <content type="html">&lt;p&gt;Ever since I started working with Parrot I&amp;#8217;ve noticed something interesting about the community: They were very interested in unit testing. Parrot alone as a suite with over ten thousand tests (and I still feel like there are some portions of the VM that are heavily under-tested). When I first joined Parrot I had never written a unit test nor did I really understand the value of testing. I was a newbie fresh out of school where such practical aspects as that were never covered. Despite my unfamiliarity with testing, I very quickly decided it was a good idea and that more tests can definitely make software more awesome.&lt;/p&gt;

&lt;p&gt;Writing tests for Parrot and Parrot-related projects is quite easy because we have the infrastructure for it. The easiest way to write tests, in my opinion, is with Rosella, but we have a Test::More library as well that can be used for great effect and is the primary testing tool used in Parrot&amp;#8217;s own test suite.&lt;/p&gt;

&lt;p&gt;Several months ago we had Tapir, a simple TAP harness project written by dukeleto and others in NQP. There was also a project called Kakapo written by Austin Hastings which included several unit test and mock object utilities, also written in NQP. I absorbed a lot of ideas from both projects (and eventually rewrote those ideas in Winxed) for the Rosella Test, Harness and MockObject libraries.&lt;/p&gt;

&lt;p&gt;Writing tests for Parrot is a great way to get involved in the project if you&amp;#8217;re a new user, a great way to get familiarized with the capabilities of the VM, and a very big benefit to the project in any case. First let&amp;#8217;s talk about Test::More as it&amp;#8217;s used in the Parrot test suite and then I&amp;#8217;m going to talk about Rosella&amp;#8217;s test offerings.&lt;/p&gt;

&lt;p&gt;Test::More is a very simple TAP producer library that implements a few standard test functions like &lt;code&gt;plan()&lt;/code&gt;, &lt;code&gt;is()&lt;/code&gt;, &lt;code&gt;ok()&lt;/code&gt;, and a few others. You use Test::More like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;.sub main :main
    .include &amp;#39;test_more.pir&amp;#39;
    plan(5);
    ok(1, &amp;quot;This test passes&amp;quot;);
    ok(0, &amp;quot;This test fails&amp;quot;);
    is(1, 1, &amp;quot;These things are equal&amp;quot;);
    isnt(1, 0, &amp;quot;These things aren&amp;#39;t equal, test passes&amp;quot;);
    is(1, 0, &amp;quot;These things aren&amp;#39;t equal, so the test fails&amp;quot;);
.end&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The test harness reads the TAP output from the test file, checks the plan, checks the results of each individual tests, and gives you a readout of the overall pass/fail status of the test.&lt;/p&gt;

&lt;p&gt;Test::More is very simple, and if you&amp;#8217;ve used a TAP library before the basics of it should be very easy to grasp. Plus, if you look through the Parrot test suite (t/ directory and subdirectories) you&amp;#8217;ll see plenty of examples on usage.&lt;/p&gt;

&lt;p&gt;Rosella offers several test-related libraries as part of it&amp;#8217;s collection: Test (a unit testing library), Harness (a library for building test harnesses) and MockObject (a mock-object testing extension) are all standard parts of the Rosella lineup. There&amp;#8217;s also an experimental Assert library that adds some new testing features as well.&lt;/p&gt;

&lt;p&gt;Rosella ships with a default testing harness called &lt;code&gt;rosella_harness&lt;/code&gt; which is available when you install Rosella. You can run it at the commandline with a list of directories like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$&amp;gt; rosella_harness t/foo t/bar t/baz&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The harness will run through all &lt;code&gt;*.t&lt;/code&gt; files in the given directories, reading the she-bang line in the file and using that program to execute the file. This is the fastest way to get started with testing. Of course, you can also use the Harness library to build your own harness in only a few lines of winxed code:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$include &amp;quot;rosella/harness.winxed&amp;quot;;
function main[main]() {
    var harness = new Rosella.Harness();
    harness
        .add_test_dirs(&amp;quot;Automatic&amp;quot;, &amp;quot;t/foo&amp;quot;, &amp;quot;t/bar&amp;quot;, &amp;quot;t/baz&amp;quot;, 1:[named(&amp;quot;recurse&amp;quot;)])
        .setup_test_run(1:[named(&amp;quot;sort&amp;quot;)]);
    harness.run();
    harness.show_results();
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The listing for a harness written in NQP is almost as short, and I&amp;#8217;ve &lt;a href='http://whiteknight.github.com/Rosella/libraries/harness.html'&gt;shown it several times&lt;/a&gt; on this blog before.&lt;/p&gt;

&lt;p&gt;Writing a unit test file with Rosella Test is similarly easy:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$include &amp;quot;rosella/test.winxed&amp;quot;;
class MyTest {
    function test_one() {
        self.assert.equal(1, 1);
    }
}
function main[main]() { Rosella.Test.test(class MyTest); }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Each method in the MyTest class will be run as a test. Each test method may contain zero or more assertions. If all assertions pass, the test passes. If any assertion fails the whole test method immediately aborts and is marked as having failed. Unlike Test::More above, we don&amp;#8217;t need to explicitly count the number of tests for &lt;code&gt;plan()&lt;/code&gt;. Instead, the library counts the number of methods for us automatically.&lt;/p&gt;

&lt;p&gt;Rosella&amp;#8217;s MockObject library can be used together with the Test library to add MockObject support to your tests. Mock Objects, as I&amp;#8217;ve said before and I&amp;#8217;ll say a million times again in the future, are tools that can do as much harm as good, especially if they are used incorrectly. Here&amp;#8217;s an example of a test using MockObject:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$include &amp;quot;rosella/test.winxed&amp;quot;;
$include &amp;quot;rosella/mockobject.winxed&amp;quot;;
class MyTest {
    function test_one() {
        var c = Rosella.MockObject.default_mock_factory()
            .create_typed(class MyTargetClass);
        c.expect_method(&amp;quot;foo&amp;quot;)
            .once()
            .with_args(1, 2, &amp;quot;test&amp;quot;)
            .will_return(&amp;quot;foobar&amp;quot;);
        var o = c.mock();
        var result = o.foo(1, 2, &amp;quot;test&amp;quot;);
        self.assert.equal(result, &amp;quot;foobar&amp;quot;);
        c.verify();
    }
}
function main[main]() { Rosella.Test.test(class MyTest); }&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You have to do more setup for the test with mockobjects, but you get a lot more flexibility to do black-box testing and unit testing with proper component isolation. I won&amp;#8217;t try to sell mock object testing here in this post, only demonstrating that it is both possible and easy with Rosella.&lt;/p&gt;

&lt;p&gt;Parrot has several thousand tests in its suite. Winxed has a small but growing test suite. Rosella currently runs &amp;#8220;728 tests in 116 files&amp;#8221;. parrot-libgit2 has a growing test suite. Rakudo has a gigantic spectest suite. Jaesop has a growing suite of tests. NQP has tests. PACT is going to have extensive tests, once it has code worth testing. Plumage has tests (though not nearly enough!). PLA has a relatively large suite of tests. Testing is a hugely important part of the Parrot ecosystem, and we currently have several tools to help with testing. Expect the trend to continue, with more tests being written for more projects in 2012.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/afwknight-parrot/~4/ujz_h0UKi7U" height="1" width="1"/&gt;</content>
    <feedburner:origLink>http://whiteknight.github.com/2011/12/16/advent_5_testing.html</feedburner:origLink></entry>
    
    <entry>
        <title>Advent 4 - Threading</title>
        <link href="http://feedproxy.google.com/~r/afwknight-parrot/~3/Mftdw4M1jF0/advent_4_threading.html" />
        <updated>2011-12-15T00:00:00-08:00</updated>
        <id>http://whiteknight.github.com/2011/12/15/advent_4_threading</id>
        <content type="html">&lt;p&gt;In GSOC two years ago my student Chandon started working on a very cool new project for Parrot: Hybrid Threads. I hadn&amp;#8217;t really put too much thought into the future of Parrot&amp;#8217;s concurrency architecture until that point and the idea certainly seemed novel and interesting to me. We went ahead with the project not quite sure how far he would get but very eager to see something happen with our ailing threading system.&lt;/p&gt;

&lt;p&gt;The problem, needless to say, ended up being much larger than a project for a single summer and by the end of it Chandon had most of a green threads implementation set up, but nothing that was quite mergable to master. His branch sat, un-merged and un-molested, for a year before anybody decided to take a second stab at it.&lt;/p&gt;

&lt;p&gt;Several months after Chandon&amp;#8217;s work ended I sat down and started seriously thinking about what I wanted Parrot&amp;#8217;s concurrency system to look like in the future. If we could have anything we wanted, what is it exactly that we would want? I started by reading up on all sorts of other technologies: Erlang, node.js, stackless threads in python, and others. I even read a few tangentially related materials, like some of the &lt;a href='http://www.gotw.ca/publications/optimizations.htm'&gt;problems with common string optimizations&lt;/a&gt;. Ideas in hand, I began writing a short &lt;a href='/2011/04/23/vision_parrot_concurrency.html'&gt;series of blog posts&lt;/a&gt; describing my personal conclusions. My plan for concurrency was very close to Chandon&amp;#8217;s hybrid threads idea but with a few extra details filled in. I suggested that we should stick with the hybrid threads approach, explicitly restrict cross-thread data contention by using messages and read-only proxies, avoid locking as much as possible, and rely on the immutability of certain data to keep things as simple as possible.&lt;/p&gt;

&lt;p&gt;I didn&amp;#8217;t have the time to work on all this myself, at least not until several other projects cleared my TO-DO list. This is where hacker &lt;strong&gt;nine&lt;/strong&gt; comes in. Nine wanted to work on adding threading to Parrot and liked the hybrid threads approach and some of the ideas I had been working on. So he checked out a copy of Chandon&amp;#8217;s green_threads work, updated to master, and started fixing things. A few weeks later green_threads was merged to master with Linux support only. I said I would work to port the system to Windows as well and nine would push forward with the hybrid portion of the hybrid threads design.&lt;/p&gt;

&lt;p&gt;We&amp;#8217;re not ready with that yet, but we are getting painfully close to a working system. I&amp;#8217;ve been stymied by the house hunt and the fact that I don&amp;#8217;t have a windows system at home to play with. Nine has had a few infrastructural difficulties, especially with GC, but he&amp;#8217;s making great progress regardless.&lt;/p&gt;

&lt;p&gt;Here&amp;#8217;s an overview of the threading system we&amp;#8217;re working towards in brief: We will have two layers of concurrency: The first are the Tasks, or &amp;#8220;green threads&amp;#8221;. Each Task represents an individual unit of executing work and multiple Tasks can execute together on a single thread. They do this through preemptive multitasking, the Parrot scheduler occasionally fires alarms and switches Tasks on the current thread if there is more than one in the queue. Since Parrot uses Continuation Passing Style internally, this mechanism is relatively simple to implement (It&amp;#8217;s the alarms that are surprisingly difficult and not cross-platform, but the actual Task switching is quite simple after that).&lt;/p&gt;

&lt;p&gt;Multiple Tasks running on a single thread gives more of an illusion of concurrency than the real thing, because you aren&amp;#8217;t making use of multiple cores in your processor hardware or exploiting any context-switching optimizations at the lowest levels of threads. What we will have to take things to the next level is the OS threads implementation. Internally Parrot will maintain a pool of worker threads. When you create a Task you will have an option about where to dispatch it: On the current thread (useful where we need safe read/write access to PMCs on the same thread without locking), on a specific target thread, or on a completely new thread. Or, if you don&amp;#8217;t want to specify you can let the scheduler dispatch the Task to the best thread.&lt;/p&gt;

&lt;p&gt;When you think about what this kind of system enables, it&amp;#8217;s actually pretty impressive: easy asynchronous IO (schedule an IO request Task on a dedicated IO thread), easy event handling (schedule a new Task on the current thread to keep data locality), easy threading (schedule a new Task, the scheduler will set up the Thread for you), easy eventing loops (main thread reads event sources and schedules Tasks), easy library callbacks (library callback schedules a Task in the owner thread), auto-threaded array operations (schedule tasks for subsets of the data, the scheduler puts the tasks on the threads with lowest latency) and a variety of other modern techniques. This system, once it&amp;#8217;s completed and all the bugs are ironed out, will really be a big boost for Parrot in terms of feature set and usability.&lt;/p&gt;

&lt;p&gt;I don&amp;#8217;t know when we will be ready to merge, but I can&amp;#8217;t imagine it will happen before the 4.0 release. Sometime in early 2012 expect to see the new hybrid threading implementation for Parrot, even if it only works on Linux initially. By the 4.6 release I expect we will have a pretty robust hybrid threading system available on all our target platforms, and several of our HLLs and libraries will be making use of it.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/afwknight-parrot/~4/Mftdw4M1jF0" height="1" width="1"/&gt;</content>
    <feedburner:origLink>http://whiteknight.github.com/2011/12/15/advent_4_threading.html</feedburner:origLink></entry>
    
    <entry>
        <title>Advent 3 - Winxed</title>
        <link href="http://feedproxy.google.com/~r/afwknight-parrot/~3/B5xh58hccuQ/advent_3_winxed.html" />
        <updated>2011-12-14T00:00:00-08:00</updated>
        <id>http://whiteknight.github.com/2011/12/14/advent_3_winxed</id>
        <content type="html">&lt;p&gt;Already this advent series of posts is an unmitigated disaster. The last two days of ommissions can be squarely blamed on my wife and her parents, and last night I will blame on Septa (for eating 5 hours of my day because of &amp;#8220;mechanical problems&amp;#8221;). Here, finally, is day 3 of my miserable failure of an advent calendar.&lt;/p&gt;

&lt;p&gt;For the third post in my almost-advent calendar, I&amp;#8217;m going to talk about &lt;a href='http://github.com/NotFound/winxed'&gt;Winxed&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Winxed, written by long-time Parrot hacker NotFound, is quite an interesting project and is definitely worth learning more about. It serves as something of a counterpoint to NQP, the other lower-level Parrot language in the ecosystem but the two aren&amp;#8217;t competing. I think they are very complimentary. Where NQP is designed to help building compilers (Rakudo Perl6 especially), Winxed seems much more geared towards writing libraries and utilities. It&amp;#8217;s for this reason that I&amp;#8217;ve written my library project, Rosella, in Winxed.&lt;/p&gt;

&lt;p&gt;In return for making such an awesome language, I&amp;#8217;ve turned around and started writing some comprehensive documentation about Winxed on the &lt;a href='http://whiteknight.github.com/Rosella/winxed/index.html'&gt;Rosella website&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Winxed is written in itself using a home-brewed recursive descent parser. To perform the bootstrapping, the stage 0 winxed compiler is written in C++. In stage 0, a paired-down version of the language is used to compile the stage 1 compiler which is written in that subset. Stage 1 is used to compile stage 2, which is a more full-featured version of the language. Stage 2 is used to compile itself into stage 3. Stage 3 is what you and I use when we install Winxed.&lt;/p&gt;

&lt;p&gt;It sounds much more complicated than it is, but the net result is clear and simple: Winxed written in Winxed. If you know Winxed, or are familiar with some of the big languages it is inspired by (C++, Java, C#, JavaScript), you&amp;#8217;ll be able to not only write software for Parrot, but also be able to hack on the Winxed compiler itself. I&amp;#8217;ve submitted a few patches and feature additions, and I&amp;#8217;ve found it to be a pleasure to work on.&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s not anything goes on the compiler, however. NotFound maintains pretty tight editorial control over the software. The consistency of vision and planning does produce refreshingly nice results, though. He&amp;#8217;s very good about taking requests and suggestions, so if you see something that&amp;#8217;s missing definitely drop him a line.&lt;/p&gt;

&lt;p&gt;Since bundling with Parrot in th 3.6.0 release, which incidentally is when NotFound started keeping track of version numbers, the language has come a long way: Various optimizations, a debugging mode with optional asserts and conditionals, several new built-in functions, support for multiple dispatch and most recently a new &amp;#8220;inline&amp;#8221; feature which allows you to inline certain types of code for performance improvements by avoiding extra PCC calls.&lt;/p&gt;

&lt;p&gt;Here is a random-ish sample of Winxed code that I&amp;#8217;ve been playing around with recently, written in Winxed for testing some new ideas I&amp;#8217;m thinking of adding to Rosella:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;function main[main](var args)
{
    var rand = Rosella.Random.default_uniform_random();
    var mutator = new Rosella.Genetic.Mutator(
        function() {
            float value = float(rand.get_float()) * 1000.0;
            return new Data(value);
        }
    );
    var e = new Rosella.Genetic.Engine([10, 3, 0], mutator,
        function(var d) {
            int x = int(d.value) - 200;
            ${ abs x };
            return x;
        }
    );
    var w = e.run(500).first().data();
    say(float(w.value));
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This sample is a driver program for a new Genetic Algorithms library that I&amp;#8217;m playing with. This toy example uses the Genetic Engine type to pick some random numbers at each generation for five hundred generations to try and get a random number which is closest to &lt;code&gt;200.0&lt;/code&gt;. It&amp;#8217;s a trivial example to be sure, and I&amp;#8217;ll write more about that library in the future if anything ever comes of it. That&amp;#8217;s not the important part of this example, however. The important part is the Winxed syntax. The syntax should be immediately familiar to anybody who has used JavaScript or C++ and any of its descendents. Here we can clearly see closures created with the &lt;code&gt;function&lt;/code&gt; keyword, creating objects with &lt;code&gt;new&lt;/code&gt; (Winxed is OO, not prototype-based like JavaScript), low-level types like &lt;code&gt;float&lt;/code&gt; and &lt;code&gt;int&lt;/code&gt;, Parrot Sub flags like &lt;code&gt;[main]&lt;/code&gt; (AKA &lt;code&gt;:main&lt;/code&gt; in PIR) and calling PIR opcodes directly (the &lt;code&gt;${ ... }&lt;/code&gt; syntax). Winxed in a nutshell is an an answer to this question: What would it look like if I took a language like C++ or JavaScript and bent it to work closely with Parrot?&lt;/p&gt;

&lt;p&gt;Winxed creates a nice mix of dynamic behavior with static analysis. There is plenty of syntax and semantics in the compiler that can be used to inline code, propagate constants, statically link functions by name instead of doing named lookups, checking known types at compile time and issuing warnings, and other tools that would be more familiar to a user of statically typed languages.&lt;/p&gt;

&lt;p&gt;I don&amp;#8217;t know where NotFound is planning to take Winxed in 2012. His relentless pursuit of new features, better internals, more optimizations, better diagnostics and other improvements leaves open many potential avenues for him to travel down.&lt;/p&gt;

&lt;p&gt;I have a few ideas for features I might like to propose and provide patches, many of which will be intended to mirror changes that will need to be made in Parrot. I have been thinking about submitting a patch to add some cleaner syntax for parameter and argument flags instead of using PIR flags by name directly. I&amp;#8217;m also keen to get Winxed updated to use some of my new PCC changes as soon as they are available. It will make both a great test case for the new functionality and a good demonstration of any performance benefits to be had. Eventually I would like to get Winxed updated to generate .pbc packfiles directly instead of generating PIR and using IMCC as an intermediary. That&amp;#8217;s a pretty big project, and might have to wait until we get more work done on PACT. Again, this would make an excellent demonstration of the functionality, when we have it ready to test.&lt;/p&gt;

&lt;p&gt;I personally use Winxed to implement my Rosella project and the first stage of my JavaScript compiler &amp;#8220;Jaesop&amp;#8221;. Other people are using it as well. Hacker plobsing has used it for a few projects, including an OMeta parser port. Hacker benabik is using it for PACT, a re-design of the Parrot Compiler Toolkit. Dukeleto is writing bindings for libgit2 in Winxed. NotFound is writing xlib bindings in Winxed called Guitor (and some of the example programs are actually quite impressive already). There are many other examples of projects that are or will be written in Winxed, and I&amp;#8217;m sure the number will only increase in 2012.&lt;/p&gt;

&lt;p&gt;If you&amp;#8217;re doing systems-level library or utility work on Parrot in 2012, Winxed is probably the language you are going to be using. I&amp;#8217;ll talk about compilers and NQP in later posts.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/afwknight-parrot/~4/B5xh58hccuQ" height="1" width="1"/&gt;</content>
    <feedburner:origLink>http://whiteknight.github.com/2011/12/14/advent_3_winxed.html</feedburner:origLink></entry>
    
    <entry>
        <title>Advent 2 - Contributing to Parrot</title>
        <link href="http://feedproxy.google.com/~r/afwknight-parrot/~3/cZ270bLGbEI/advent_2_contributing.html" />
        <updated>2011-12-11T00:00:00-08:00</updated>
        <id>http://whiteknight.github.com/2011/12/11/advent_2_contributing</id>
        <content type="html">&lt;p&gt;I asked for some ideas about topics to write about for this advent calendar thing, and PerlJam (Perl hacker and long-time Parrot ecosystem contributor and well-wisher) suggested I devote a post to how to contribute to Parrot. Say no more! I think it&amp;#8217;s an excellent idea for a second post. This, the second day of my lazy, late pseudo-advent calendar for Parrot will be devoted to contributions.&lt;/p&gt;

&lt;p&gt;In the olden-days of Parrot, like last year, we used SVN. These were dark and dangerous times when new contributors were forced to submit their proposed changes in patch files via email. There was much weeping and gnashing of teeth. However, we finally made the switch not only to version control with git but also hosting with github. Now contributing to Parrot or any of its many ecosystem projects is a snap. Create a fork of the repository you want to contribute to, make your edits and commit them, and then open a github pull request. It&amp;#8217;s so easy, you&amp;#8217;re going to be wishing you started doing it earlier. Seriously, what are you waiting for?&lt;/p&gt;

&lt;p&gt;If you submit enough cool changes, we&amp;#8217;ll probably do one or both of the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Ask you to add your name to CREDITS, so you can get MAD PROPZ for everybody to see.&lt;/li&gt;

&lt;li&gt;Give you a commit bit to the repo because seriously, you&amp;#8217;re doing too much awesome work and we don&amp;#8217;t want to have to play intermediaries between you and the code.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We may also try to rope you into doing other stuff, like being one of our monthly release managers (looks &lt;em&gt;great&lt;/em&gt; on a resume, especially if you&amp;#8217;re entry-level), writing blog posts, mentoring GSoC and GCI students, and writing even more awesome code. If you ever read the book &lt;a href='http://www.amazon.com/Giving-Tree-Sid-Silverstein/dp/B004R64766/ref=sr_1_sc_2?ie=UTF8&amp;amp;qid=1323556649&amp;amp;sr=8-2-spell'&gt;The Giving Tree&lt;/a&gt;, it&amp;#8217;s kind of like that except everybody wins and the ending is much happier.&lt;/p&gt;

&lt;p&gt;Parrot is a relatively rare kind of project because we have so much happening at so many levels of abstraction. If you&amp;#8217;re a nuts and bolts kind of coder and like doing stuff &amp;#8220;on the metal&amp;#8221;, we have plenty of internals work that needs doing: Threading and Concurrency, Garbage Collection, Object Model, and more optimizations than you can shake a stick at. If you&amp;#8217;re more of a middle-ware person we have lots of libraries and infrastructure projects to work on. Then we have HLL compilers like Rakudo that need help and finally end-user code and programs written in those HLLs.&lt;/p&gt;

&lt;p&gt;Want to write games? We have xlib and opengl bindings available. Sure they may need a little bit of love, but we have them and they work very well.&lt;/p&gt;

&lt;p&gt;Like compilers? We have a handful in development and are always looking for more. Winxed is a fun and familiar (For C++ and Java lovers) systems language. I&amp;#8217;m working on a new bootstrapped JavaScript compiler. In the past we&amp;#8217;ve had Ruby, Python and Tcl compilers under development, and things&lt;/p&gt;

&lt;p&gt;Like Perl6? RAKUDO RAKUDO RAKUDO! Also, Rakudo.&lt;/p&gt;

&lt;p&gt;Like writing documentation or making cool new websites to hold our docs? We need you! We&amp;#8217;ve got lots of documentation that we need to expose to the users better, and we are missing lots of documentation that needs to be written. Also, since code is changing at a break-neck pace we need to update all the docs we have.&lt;/p&gt;

&lt;p&gt;If you like solving problems, fixing bugs or implementing cool new code then we have tons of jobs for you! Sign up for a free account on Github if you don&amp;#8217;t have one already. Search around the various &lt;a href='http://github.com/parrot'&gt;Parrot repositories&lt;/a&gt; or search for &amp;#8220;Parrot&amp;#8221; to find a repository you want to hack on. Create a fork and get to work! If you want to contribute to a particular project or particular type of project and aren&amp;#8217;t sure where or how is the best place, come talk to us and we&amp;#8217;ll try to get you pointed in the right direction.&lt;/p&gt;

&lt;p&gt;Speaking of talking to us, there are three good ways to get into contact with us Parrot folks, if you want to chat: IRC (#parrot on irc.parrot.org), The parrot-dev email list (parrot-dev at lists.parrot.org) or in comments and pull requests on github. Leave a comment on this blog too, and I&amp;#8217;ll help you personally.&lt;/p&gt;

&lt;p&gt;Parrot is a big open-source project with lots of work to be done at every level of abstraction. We&amp;#8217;re always looking for new contributions and new contributors. If you&amp;#8217;re interested in getting involved, don&amp;#8217;t hesitate to get in contact with us and start writing some great code.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/afwknight-parrot/~4/cZ270bLGbEI" height="1" width="1"/&gt;</content>
    <feedburner:origLink>http://whiteknight.github.com/2011/12/11/advent_2_contributing.html</feedburner:origLink></entry>
    
    <entry>
        <title>Advent 1 - Parrot</title>
        <link href="http://feedproxy.google.com/~r/afwknight-parrot/~3/lpxmtkqmlhk/advent_1_parrot.html" />
        <updated>2011-12-10T00:00:00-08:00</updated>
        <id>http://whiteknight.github.com/2011/12/10/advent_1_parrot</id>
        <content type="html">&lt;p&gt;I had intended to publish this advent post yesterday, but even though I had it mostly pre-written I couldn&amp;#8217;t find 5 minutes in the day to do it. An inauspicious start to this little sequence! It is already painfully clear that this advent calendar of mine isn&amp;#8217;t going to be nearly as reliable or successful as moritz&amp;#8217;s. To (finally) kick off this &lt;a href='/2011/12/08/advent_calendar.html'&gt;pseudo-advent calendar&lt;/a&gt; I&amp;#8217;m going to jump right in and talk about the main course, the bird, Parrot. This post is going to be a short retrospective about the developments in 2011 and some clues about where we are heading (or, where I hope we are heading) next year. I&amp;#8217;ll very likely post a more in-depth yearly retrospective around the 4.0 release in January.&lt;/p&gt;

&lt;p&gt;Parrot, as we all know, is a virtual machine aimed at running dynamic languages. Originally it was envisioned as the backend VM for the new Perl6 language, but Parrot quickly deviated from that path. The idea quickly became to create a language-agnostic platform for hosting a variety of languages in a common, interoperable way.&lt;/p&gt;

&lt;p&gt;I don&amp;#8217;t want to attempt a complete retelling of the history of the project. I wasn&amp;#8217;t around for most of it and at best I&amp;#8217;m going to give a faulty recount. Regardless, it doesn&amp;#8217;t really matter how we got to where we are now. What matters is what our current trajectory is. Prior to 2011 Parrot had been trying to do many things well but did no single thing well enough to really drive adoption. I think we&amp;#8217;ve made up our minds to refocus our efforts on supporting Perl6 specifically, and many of the biggest developments planned for 2012 are going to be headed in that direction. Almost all of the work I personally am planning to do next year will be directly tied to Rakudo, trying to make their system even better, and to give them a better and more compelling infrastructure to do their thing on.&lt;/p&gt;

&lt;p&gt;2011 has seen a lot of changes to Parrot, though the vast number of them are internal and involve code that is cleaner and more maintainable, even if not much more functional or with better performance. This continues a trend that was happening in 2010 and even earlier: The biggest tasks we&amp;#8217;ve been working on in the last few years involve trying to get some of our older, uglier, and more brittle systems up to a decent level of quality to support additional future work. It&amp;#8217;s the nature of the beast, and as much as I would love to say that the code we had was perfectly pretty to begin with, in many cases it has not been. This is not to disparage any prior contributors. If the history of Parrot tells us anything, it&amp;#8217;s that Parrot hackers historically didn&amp;#8217;t (and honestly, might still not completely) understand exactly what the goals were. So many decisions intellgently made with the best of intentions lead in the exact wrong directions. Such is life. So many systems were prototyped and those prototypes silently became &amp;#8220;the real thing&amp;#8221; without anybody explicitly giving a stamp of approval.&lt;/p&gt;

&lt;p&gt;Starting several years ago we had many subsytems either deleted outright because they were unsalvagable or dramatically rewritten. Some of those events were a little painful, but in all cases we did what was necessary. Because of all the hard work from our development team we are really starting to get to a point where big system improvements and feature additions are not only possible but now very plausible. Things that would have been near impossible to implement in the code base as of 2009 are very reasonable to consider doing in 2011 and 2012. That&amp;#8217;s a big step up, even if many of these changes aren&amp;#8217;t visible to the end user.&lt;/p&gt;

&lt;p&gt;Much of my personal work has been focused on getting some of the essentials into place with regards to the core execution pathway: IMCC, Embedding API, packfiles, etc. The new embedding API was added in January. IMCC&amp;#8217;s new public interface was likewise improved and several bits of related code were cleaned in April. The new PackfileView PMC type, the &lt;code&gt;load_bytecode_p_s&lt;/code&gt; opcode, the new bootstrapping frontend, and the new &lt;code&gt;:tag&lt;/code&gt; syntax for PIR all came in the second half of the year. Starting in early 2012 I&amp;#8217;m going to be ripping out all the old cruft that these things were designed to replace, and we are going to see some improvements in code quality to go along with that.&lt;/p&gt;

&lt;p&gt;Previously, users had the &lt;code&gt;:load&lt;/code&gt;, &lt;code&gt;:init&lt;/code&gt; and &lt;code&gt;:main&lt;/code&gt; flags to try and schedule when certain subroutines should be executed. The rules for all these flags were messy and overlapping, and they still weren&amp;#8217;t considered enough for all necessary use-cases. Some people had suggested adding even more subroutine flags with more special-case semantics throughout the Parrot codebase. This, in my mind, was nonsensical.&lt;/p&gt;

&lt;p&gt;Now in Parrot you can use the new &lt;code&gt;:tag&lt;/code&gt; syntax to tag a sub with any flag you want:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# Almost same as old :load
.sub &amp;#39;foo&amp;#39; :tag(&amp;quot;load&amp;quot;)

# Almost the same as old :init
.sub &amp;#39;bar&amp;#39; :tag(&amp;quot;init&amp;quot;)

# Couldn&amp;#39;t do this before!
.sub &amp;#39;baz&amp;#39; :tag(&amp;quot;SomethingNew&amp;quot;)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And now with the new PackfileView PMC you can find and execute subroutines by tag at any time you want without having to hope and pray that the magical Parrot behavior will do what you need when you need it:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$P0 = load_bytecode &amp;quot;foo.pbc&amp;quot;
$I0 = $P0.&amp;#39;is_initialized&amp;#39;(&amp;quot;SomethingNew&amp;quot;)
if $I0 goto already_initialized
$P1 = $P0.&amp;#39;subs_by_tag&amp;#39;(&amp;quot;SomethingNew&amp;quot;)
...
$P0.&amp;#39;mark_initialized&amp;#39;(&amp;quot;SomethingNew&amp;quot;)
already_initialized:
...&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Yes, it is a little bit more code for the end user (or intrepid HLL developer) to write, but the increase in control and flexibility more than makes up for it, in my mind. Plus, this kind of code is very easy to &lt;a href='https://github.com/Whiteknight/Rosella/blob/master/src/core/Rosella.winxed#L168'&gt;abstract away into a new function&lt;/a&gt;, so you only need to write it once. For the record, and I know I&amp;#8217;ve discussed this topic at length on my blog in the past, the above code snippet actually executes &lt;em&gt;faster&lt;/em&gt; than the old magical semantics, despite the fact that we have more explicit PIR code and more code total running in the runloop instead of at the C level.&lt;/p&gt;

&lt;p&gt;The &lt;a href='/2011/08/17/sub-first-steps.html'&gt;packfile loader&lt;/a&gt;, PIR compilation symantics and the way things like Classes, Namespaces and Multisubs are created will all be changing in 2012 for the better. Expect to see performance improvements and feature additions to these things in 2012. If you&amp;#8217;re being smart and writing your code in Winxed or NQP we will try our best to keep you shielded from almost all of these changes. If you&amp;#8217;re writting code in PIR I feel bad for you, son. I&amp;#8217;ve got 99 problems and PIR is like 97 of them.&lt;/p&gt;

&lt;p&gt;In a related note, a selection of other Packfile-related PMCs were refactored in January and now we have the ability to create usable bytecode from a Parrot-run program. The interface isn&amp;#8217;t great and we haven&amp;#8217;t made much use of this ability yet, but I expect big things to happen in 2012 with &lt;a href='http://github.com/parrot/PACT'&gt;PACT&lt;/a&gt; (benabik&amp;#8217;s rewrite of PCT) and maybe a few details coming to &lt;a href='http://github.com/Whiteknight/Rosella'&gt;Rosella&lt;/a&gt; as well.&lt;/p&gt;

&lt;p&gt;In 2012 I expect that we are going to finish the work we started in 2011 and have IMCC removed as a permanent built-in part of Parrot. It will still be available to people who want it as an optional library, but it will not be a part of the libparrot binary itself. This is going to have some big ramifications throughout some of the oldest and ugliest parts of the code base and will allow us to start pursuing several goals: Decreasing total code size, especially for embedded environments, being much more flexible about compilers and cleaning up the compreg system, divorcing packfile creation from packfile execution, giving us a much cleaner and more usable interface for executing packfiles, and breaking up some of the syntactic quirks of PIR from the inner semantics of Parrot. It really doesn&amp;#8217;t seem like much but trust me when I say that removing IMCC represents more than just some theoretical edification, it opens many doors that we &lt;em&gt;do&lt;/em&gt; want and need to travel through soon.&lt;/p&gt;

&lt;p&gt;In March bacek added the new Generational garbage collector (GMS), and it became the default collector in May. Performance jumped considerably, especially for Rakudo; although I think some other optimizations can push the performance numbers even better. Sometime in 2012 I want to test out an idea I have with cutting out indirect function calls in the GC, and then I want to dig through some profiles to see where else I can squeeze out a few percentage points. I don&amp;#8217;t know if there will be much, but it never hurts to look. With threading on the horizon, I also want to look into a few concurrency-friendly GC algorithms that we can utilize to cut GC overhead and maybe improve GC performance for heavily concurrent workloads.&lt;/p&gt;

&lt;p&gt;Parrot hacker plobsing had been doing a lot of NCI-related work through the year, adding the StructView, Ptr, PtrBuf and PtrObj PMC types to replace some of the older types we had for working with raw pointers. These tools are pretty low-level and don&amp;#8217;t always expose a very friendly interface (as we would expect at such a low level of abstraction), but have a lot of potential that we can definitely build on. Not all of the NCI changes were met with great enthusiasm (especially those that broke some order signature syntaxes), but getting NCI cleaned up and fixed up is a major hurdle for us that we have to leap over in spite of the pain. Being able to share common native library bindings among multiple HLLs is a huge deal for Parrot, and has the potential to become a major selling point if we can get it done correctly. The Rakudo/NQP folks have also been doing some cool-looking NCI work that we may want to copy in whole or in part, so look forward to those kinds of developments in 2012 as well. Hacker NotFound has been working on a new project called &lt;a href='http://github.com/NotFound/Guitor'&gt;Guitor&lt;/a&gt; which uses pure &lt;a href='http://github.com/NotFound/winxed'&gt;Winxed&lt;/a&gt; code to create graphical user interfaces by calling into xlib using Parrot&amp;#8217;s NCI. The work he&amp;#8217;s been doing is pretty fantastic, and serves as a clear demonstration that NCI is working and working reasonably well.&lt;/p&gt;

&lt;p&gt;Adding new native library bindings for Parrot is a very productive and very informative way to get started hacking with Parrot, for anybody who is interested!&lt;/p&gt;

&lt;p&gt;Parrot&amp;#8217;s object model has remained relatively static for a while, despite the fact that the problems and limitations with it are well known and oft-decried. I hard started to port 6model, the new Rakudo object model developed by Jonathan Worthington, over to Parrot in the summer months but put that work on hold while I came up with a better plan. I kept hoping that development on 6model would slow down and become more stable so I could find a good jumping off point to start with, but that never happened. Eventually I am going to need to just put my foot down and start moving code around. Expect 6model to come to Parrot in early 2012, especially if a few other people volunteer to help.&lt;/p&gt;

&lt;p&gt;&lt;a href='http://github.com/NotFound/winxed'&gt;Winxed&lt;/a&gt;, NotFound&amp;#8217;s system-level language for Parrot was added as a snapshot to the Parrot repository in July, and has continued to improve by leaps and bounds in that time. Recently NotFound added syntax for &lt;code&gt;inline&lt;/code&gt; functions, which can help to improve performance in many places. I&amp;#8217;ve got a few syntax ideas I want to play with and try to contribute as well. If we can divorce Winxed a little further from IMCC and PIR syntax (especially in the area of calling conventions and flags), we can be more free to change Parrot&amp;#8217;s internals because Winxed&amp;#8217;s abstracted syntax will create more of a buffer for us. In 2012 I would like to continue the trend of using PIR less and less, and using Winxed and NQP more and more for basic Parrot tasks.&lt;/p&gt;

&lt;p&gt;Specaking of which, NQP-rx, an older variant of the amazingly useful NQP language is showing it&amp;#8217;s age and may be dead in 2012. I would love to see it replaced by the newer 6model-powered &lt;a href='http://github.com/perl6/NQP'&gt;NQP&lt;/a&gt;, especially once we get 6model in Parrot natively. Then we will have two awesome lower-level languages to play with in an interchangable, inter-usable way.&lt;/p&gt;

&lt;p&gt;Late 2011 also brought us nine&amp;#8217;s rewrite of Green Threads. They aren&amp;#8217;t working on Windows yet, but they are working reasonably well on Linux and (I think) Mac although some kinks are still being worked out. In early 2012 expect to start seeing his implementation of full hybrid threads which are already looking awesome. As with the green threads Windows support may come later, especially since we seem to have a relative dearth of hackers working on that platform right now. When I finally get my new laptop I&amp;#8217;ll keep windows around to dual-boot from, and will do my best to get concurrency working as well there as anywhere. As always, help in this endeavor would be appreciated. If you&amp;#8217;re interested in Parrot concurrency, be it implementation of the internals or using it to write cool new programs and libraries, definitely let me know.&lt;/p&gt;

&lt;p&gt;Also in 2012 expect to start seeing some of my proposed changes to PCC start getting integrated into the system. Actually, you probably won&amp;#8217;t see them, most of the changes will be transparent in IMCC or maybe in new optimized code generated by Winxed and NQP. We&amp;#8217;ll definitely see a few percentage points in performance improvement across PCC calls to start, and opportunity for further improvements after that.&lt;/p&gt;

&lt;p&gt;This little retrospective has already grown into a substantial post so I won&amp;#8217;t write too much more. Expect other posts in this advent series to be shorter and sweeter (and hopefully, more on time).&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/afwknight-parrot/~4/lpxmtkqmlhk" height="1" width="1"/&gt;</content>
    <feedburner:origLink>http://whiteknight.github.com/2011/12/10/advent_1_parrot.html</feedburner:origLink></entry>
    
    <entry>
        <title>Parrot Advent Calendar</title>
        <link href="http://feedproxy.google.com/~r/afwknight-parrot/~3/_F0sCzjcSI4/advent_calendar.html" />
        <updated>2011-12-08T00:00:00-08:00</updated>
        <id>http://whiteknight.github.com/2011/12/08/advent_calendar</id>
        <content type="html">&lt;p&gt;Rakudo hacker &lt;strong&gt;moritz&lt;/strong&gt; is auditioning for the role of the Good Idea Fairy, which I personally think he&amp;#8217;s a shoe-in for. A few days ago in the IRC chatroom he had this gem:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;moritz&amp;gt;      hey, have you ever thought of starting a parrot advent calendar?
whiteknight&amp;gt; moritz: sort of, but I wouldn&amp;#39;t want to steal your thunder
moritz&amp;gt;      whiteknight: I wouldn&amp;#39;t worry about that. There&amp;#39;s usually half a
             dozen advent calendars in the Perl community, and we don&amp;#39;t seem
             to suffer from that
...
moritz&amp;gt;      whiteknight: don&amp;#39;t feel obliged, &amp;#39;twas just a quick idea. Maybe
             next year if it&amp;#39;s too much work this year
whiteknight&amp;gt; moritz: If there is one thing I like doing, it&amp;#39;s stealing good
             ideas from the Rakudo folks&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So that&amp;#8217;s what I&amp;#8217;m going to try to do: Do a daily advent-ish sort of calendar for Parrot as similar to the one that &lt;a href='http://perl6advent.wordpress.com/'&gt;moritz does himself&lt;/a&gt; as I can manage. Instead of a normal advent calendar which would be the first 25 days of December, I&amp;#8217;m going to do the remaining days leading up to New Years. Since I&amp;#8217;m already running irrepairably late, I won&amp;#8217;t be able to do 25 daily posts between now and then. All the better, really, since I am having trouble coming up with that many ideas! Give me a break, I&amp;#8217;m new to this whole advent thing.&lt;/p&gt;

&lt;p&gt;What I say I would like to do, and what I am actually able to get done in the coming days are two entirely different things. Add the usual holiday stressors and time-sinks with the cleanups and organizational hassles of moving to a new house (That&amp;#8217;s right, we &lt;a href='/2011/10/25/housing.html'&gt;got the house&lt;/a&gt;!) and suddenly doing a blog post every day seems like a pretty daunting order. Again, I will do my best&lt;/p&gt;

&lt;p&gt;Every day from now till the end of the year, starting tomorrow, I&amp;#8217;m going to try to post at least a short blurb about something cool in the world of Parrot. I&amp;#8217;ll try to focus on things that we&amp;#8217;ve done cool in 2011 or are planning to do come 2012, but if I can find some things worth writing about that are older and are standing the test of time particularly well, I&amp;#8217;ll write about that too.&lt;/p&gt;

&lt;p&gt;If this goes well and I don&amp;#8217;t get all lazy and skip too many days, maybe we can turn this into some sort of regular tradition. If other Parrot hackers want to jump in and write a short guest post here on this blog, or write their own posts elsewhere, let me know. More help is more awesome!&lt;/p&gt;

&lt;p&gt;Obviously I&amp;#8217;m going to mention some of the projects I&amp;#8217;ve been working on because those are the things I know the most about. I&amp;#8217;ll try to include a bunch of cool projects from other people too. Please send me ideas if there is something &lt;strong&gt;you&lt;/strong&gt; want to read about in the coming month.&lt;/p&gt;

&lt;p&gt;Tomorrow the advent calendar officially starts. I&amp;#8217;m going to begin by talking about the main course: the bird.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/afwknight-parrot/~4/_F0sCzjcSI4" height="1" width="1"/&gt;</content>
    <feedburner:origLink>http://whiteknight.github.com/2011/12/08/advent_calendar.html</feedburner:origLink></entry>
    
    <entry>
        <title>Sorting Algorithms from GCI</title>
        <link href="http://feedproxy.google.com/~r/afwknight-parrot/~3/i8MTVU8pCJM/rosella_sorting.html" />
        <updated>2011-11-26T00:00:00-08:00</updated>
        <id>http://whiteknight.github.com/2011/11/26/rosella_sorting</id>
        <content type="html">&lt;p&gt;Since I haven&amp;#8217;t been posting often enough recently, and my schedule is so screwed up because of holidays and the like, and since I happen to have two drafts available I&amp;#8217;m posting them both today.&lt;/p&gt;

&lt;p&gt;A while back I was playing around with some &lt;span&gt;sorting algorithms and benchmarks&lt;/span&gt; &lt;a href='/2011/05/28/sorting.html'&gt;sorting_benchmarks&lt;/a&gt; for Parrot. I had a quicksort hybrid implementation written in winxed that was consistently out-performing the built-in C implementation of quicksort by about 20%. I decided that I wanted to play with a few more algorithms, especially algorithms which were known to have different performance characteristics on different input types.&lt;/p&gt;

&lt;p&gt;For GCI I created two new tasks asking for alternate sort implementations. The first was a &lt;a href='http://en.wikipedia.org/wiki/Timsort'&gt;Timsort&lt;/a&gt; implementation, and the second was for &lt;a href='http://en.wikipedia.org/wiki/Smoothsort'&gt;Smoothsort&lt;/a&gt;. GCI students Yuki&amp;#8217;N and blaise each delivered a winxed implementation of their respective sort, and now I&amp;#8217;m able to do some interesting benchmarks showing how they work on different inputs. Here are some of those results:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;N = 100000
SORT_TRANSITION = 6

FORWARD-SORTED (PRESORTED) BENCHMARKS

sort with .sort BUILTIN (reversed)
    9.872862s - %100.000000
    Number of items out of order: 0
sort with Rosella Query (reversed)
    8.623591s - %87.346416 (-%12.653584 compared to base)
    Number of items out of order: 0
qsort+insertion sort (reversed)
    7.812607s - %79.132142 (-%20.867858 compared to base)
    Number of items out of order: 0
timsort (reversed)
    0.693221s - %7.021481 (-%92.978519 compared to base)
    Number of items out of order: 0
Smoothsort (reversed)
    2.154514s - %21.822589 (-%78.177411 compared to base)
    Number of items out of order: 0

REVERSE-SORTED BENCHMARKS

sort with .sort BUILTIN (reversed)
    10.000436s - %100.000000
    Number of items out of order: 0
sort with Rosella Query (reversed)
    8.891811s - %88.914232 (-%11.085768 compared to base)
    Number of items out of order: 0
qsort+insertion sort (reversed)
    8.555817s - %85.554438 (-%14.445562 compared to base)
    Number of items out of order: 0
timsort (reversed)
    0.812737s - %8.127015 (-%91.872985 compared to base)
    Number of items out of order: 0
Smoothsort (reversed)
    10.521473s - %105.210141 (+%5.210141 compared to base)
    Number of items out of order: 0

RANDOM BENCHMARKS

sort with .sort BUILTIN (random)
    13.536509s - %100.000000
    Number of items out of order: 0
sort with Rosella Query (random)
    12.566452s - %92.833773 (-%7.166227 compared to base)
    Number of items out of order: 0
qsort+insertion sort (random)
    11.859363s - %87.610203 (-%12.389797 compared to base)
    Number of items out of order: 0
Timsort (random)
    14.461384s - %106.832449 (+%6.832449 compared to base)
    Number of items out of order: 0
Smoothsort (random)
    12.764498s - %94.296823 (-%5.703177 compared to base)
    Number of items out of order: 0&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;SORT_TRANSITION&lt;/code&gt; parameter above is the size of the array below which the hybrid sort switches from quicksort to insertion sort. 6 is an arbitrary value, but seems to have reasonably good results. I could spend some time to tune this value, but I haven&amp;#8217;t.&lt;/p&gt;

&lt;p&gt;These benchmarks show some things we already knew: the built-in Quicksort implementation from Parrot is poor across the board. The Quicksort variant that&amp;#8217;s in Rosella is better, and my hybrid quicksort+insertion sort variant is better still. What&amp;#8217;s interesting to see is how Timsort and Smoothsort perform on these workloads.&lt;/p&gt;

&lt;p&gt;Timsort is designed to work well with &amp;#8220;real-world&amp;#8221; data which is already sorted or already partially sorted. It identifies runs in the data that are already mostly sorted and merges subsequent runs together. Timsort also has the nice feature of identifying runs which are already reverse-sorted and does a very fast reverse to get them ready for merging. We see that the Timsort blows all other challengers out of the water when the array is already sorted and already reverse-sorted. In these instances, the analysis stages of Timsort figure out that no sorting is ever necessary.&lt;/p&gt;

&lt;p&gt;Smoothsort constructs a special type of heap from the input data, and uses basic balancing operations on the heap to find the largest value, extracts it, and rebalances the heap. It works very well for the pre-sorted case, but not quite as well as Timsort because it does need to construct this heap first then iterate over it. Smoothsort goes so quick because the heap rebalancing operations when the array is already sorted are almost free. So Smoothsort is quick on a pre-sorted array, but we also see that it&amp;#8217;s terrible when the input array is reverse sorted. Timsort still does very well in this case.&lt;/p&gt;

&lt;p&gt;When the array is completely random, the story is a little bit different. Both Timsort and Smoothsort lose to the quicksort implementations for completely random data. Timsort actually is &lt;em&gt;worse&lt;/em&gt; than Parrot&amp;#8217;s built-in quicksort, one of the few results we measure to be slower. Smoothsort is in the same ballpark as the Rosella quicksort, but is a few percent off of the hybrid sort.&lt;/p&gt;

&lt;p&gt;If I had to put together a small report-card for these algorithms under all these conditions, it would look something like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;algorithm       pre-sorted      reversed        random
------------------------------------------------------
quicksort       B               B               A
hybrid sort     B+              B+              A+
Timsort         A+              A+              C
Smoothsort      A               C               B+&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;At a glance you can really see where each algorithm excels.&lt;/p&gt;

&lt;p&gt;It&amp;#8217;s worth nothing here that all these implementations are relatively naive and unoptimized. So we can say that &amp;#8220;But Algorithm X could be optimized to be even better!&amp;#8221;, but the same can be said about all of them. I&amp;#8217;ll be doing some of that in the coming days, but I don&amp;#8217;t expect any radical changes.&lt;/p&gt;

&lt;p&gt;What I would like to do in the future is provide a default sort implementation but also have the sorting interface take some sort of optional &amp;#8220;hint&amp;#8221; flag that can tell the sorter about certain properties of the data, and select an algorithm specifically tuned for that workload. From the data I have seen so far, I suspect I would like to use by quicksort hybrid as the default, but be able to switch to Timsort if the user hints the input data might already be partially sorted.&lt;/p&gt;

&lt;h3 id='addenum_about_bigo_and_algorithms'&gt;Addenum about Big-O and Algorithms&lt;/h3&gt;

&lt;p&gt;Everybody will tell you that quicksort is &lt;code&gt;O(n log n)&lt;/code&gt; on average, and has a pathological worst-case that&amp;#8217;s &lt;code&gt;O(n^2)&lt;/code&gt;. People will also happily point out that something like Timsort has a best case of &lt;code&gt;O(n)&lt;/code&gt;. What these simple expressions ignore are all the details. The pathological worst case of Quicksort requires a very specific input ordering and an absolute worst selection of the pivot element at each recursion. Even basic modifications to the algorithm or using a hybrid approach completely eliminates these worst-cases. Without such basic modifications the worst-case is certainly possible, but relatively unlikely.&lt;/p&gt;

&lt;p&gt;What people also forget when talking about big-O notation are the coefficients. When I say that quicksort has average complexity of &lt;code&gt;O(n log n)&lt;/code&gt;, what I really mean is that the amount of time it takes is:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;t = c * n * log(n) + f(n) + d&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Where &lt;code&gt;f(n)&lt;/code&gt; is any function that grows more slowly than &lt;code&gt;n * log(n)&lt;/code&gt;, and &lt;code&gt;c&lt;/code&gt; and &lt;code&gt;d&lt;/code&gt; are arbitrary coefficients. The quicksort algorithm, properly implemented and optimized, has very low coefficients. The algorithm requires very little setup (&lt;code&gt;d&lt;/code&gt;) and performs relatively few operations per iteration (&lt;code&gt;c&lt;/code&gt;). The reason why Parrot&amp;#8217;s built-in quicksort performs so poorly is because the &lt;code&gt;c&lt;/code&gt; there involves recursive PCC calls and nested runloops, so &lt;code&gt;c&lt;/code&gt; is unnecessarily large. Just by having it all run in a single runloop we can drop &lt;code&gt;c&lt;/code&gt; enough to beat the original implementation. The two implementations use almost exactly the same algorithm, so it&amp;#8217;s differences in &lt;code&gt;c&lt;/code&gt; (and, to a smaller extent, differences in &lt;code&gt;d&lt;/code&gt;) that result in the timing improvements.&lt;/p&gt;

&lt;p&gt;Insertion sort, and this is why I picked it to be part of my hybrid quicksort, has &lt;code&gt;O(n^2)&lt;/code&gt; complexity, but with very low &lt;code&gt;c&lt;/code&gt;. Below a certain threshold, the quick sort algorithm becomes dominated by recursion calls and stack management, and below that threshold the insertion sort performs better. Basically, there&amp;#8217;s a very narrow window below which insertion sort&amp;#8217;s &lt;code&gt;O(n^2)&lt;/code&gt; is lower than quicksort&amp;#8217;s &lt;code&gt;O(n log n)&lt;/code&gt;. By switching algorithms below that threshold, we can squeeze out a few extra percentage points in performance savings. I could easily have used something else like Bubblesort here for the same kind of effect.&lt;/p&gt;

&lt;p&gt;Timsort, because it does involve ahead-of-time analysis steps to detect pre-sorted runs is always going to be at something of a disadvantage when faced with a purely-random input. Assuming it&amp;#8217;s core sorting algorithm is as efficient on random data as quicksort is (it isn&amp;#8217;t, but we can pretend), Timsort is always going to lose those benchmarks because quicksort will be just as fast during the sorting and wont have a forward analysis phase.&lt;/p&gt;

&lt;p&gt;Smoothsort is very interesting from a mathematical perspective, and it doesn&amp;#8217;t do ahead-of-time analysis like Timsort does. Of course, it does need to construct that special heap, which likewise acts like a damping agent on overall performance results. Smoothsort does very well on pre-sorted data, reasonably well on random data, and completely falls apart when the data is almost exactly reverse-sorted. I suspect we could do some kind of analysis there to detect the worst case and build our heaps backwards, but that would further cut into the performance cost of the common cases.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/afwknight-parrot/~4/i8MTVU8pCJM" height="1" width="1"/&gt;</content>
    <feedburner:origLink>http://whiteknight.github.com/2011/11/26/rosella_sorting.html</feedburner:origLink></entry>
    
    <entry>
        <title>Rosella Random Library and GCI</title>
        <link href="http://feedproxy.google.com/~r/afwknight-parrot/~3/pgs8q0Uw62c/rosella_random.html" />
        <updated>2011-11-26T00:00:00-08:00</updated>
        <id>http://whiteknight.github.com/2011/11/26/rosella_random</id>
        <content type="html">&lt;p&gt;I&amp;#8217;ve never been too happy with the random number generation capabilities of Parrot. Parrot essentially provides a thin wrapper around the system &lt;code&gt;rand&lt;/code&gt; and &lt;code&gt;srand&lt;/code&gt; capabilities, with an unimaginative interface. This is a fine system for most purposes, but sometimes you need something a little bit different, or want to substitute your own version without too much hassle.&lt;/p&gt;

&lt;p&gt;One of the tasks I submitted to GCI was asking for an implementation of the &lt;a href='http://en.wikipedia.org/wiki/Mersenne_Twister'&gt;Mersenne Twister&lt;/a&gt; algorithm, a good and well-known algorithm to generate pseudo-random numbers. GCI student Yuki&amp;#8217;N, who was also a prolific contributor last year for the program, submitted a fine implementation for Rosella to use. Shortly thereafter I wrote up a quick &lt;a href='http://en.wikipedia.org/wiki/Box-Muller'&gt;Box-Muller&lt;/a&gt; implementation to get normally-distributed numbers too. Now, Rosella has a pretty decent start of a random number library.&lt;/p&gt;

&lt;p&gt;For an example I wrote up a short histogram program to display the output. Here are the histograms for the Mersenne Twister and the Box-Muller generators:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Histogram of 500 uniformly-distributed floats:
0: ##########################
1: ####################
2: ##########################
3: ##########################
4: #############################
5: #########################
6: ###########################
7: ########################
8: ####################
9: #######################
10: ############################
11: ##############
12: ###############################
13: #########################
14: ##############
15: ##########################
16: ##########################
17: ##############
18: ##############################
19: #######################

Histogram of 500 normal-distributed floats:
0: #
1: #
2: ####
3: ######
4: ########
5: ########################
6: #######################
7: #########################################################
8: ##########################################################################
9: ###################################################################
10: ################################################################
11: ####################################################
12: #######################################################
13: #########################
14: #################
15: ########
16: ##########
17: #
18:
19: ##&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;These are the two distributions that I wanted to have most, but they are certainly not the only one ones available.&lt;/p&gt;

&lt;p&gt;And random number generation is not the only feature that this library will have, either. The Rosella Query library currently had an implementation of a &lt;span&gt;Fisher-Yates Shuffle&lt;/span&gt; algorithm for shuffling an array. I moved that implementation to the new Random library already and updated it to use the Mersenne Twister as the random number source instead of Parrot&amp;#8217;s built-in &lt;code&gt;rand&lt;/code&gt; opcode.&lt;/p&gt;

&lt;p&gt;I have got a handful of other small features and additions that I would like to add to this library, but considering that it is so straight-forward and algorithmic I expect I can make it stable pretty soon without much headache.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/afwknight-parrot/~4/pgs8q0Uw62c" height="1" width="1"/&gt;</content>
    <feedburner:origLink>http://whiteknight.github.com/2011/11/26/rosella_random.html</feedburner:origLink></entry>
    

</feed>

