24 Nov 2000 apm   » (Journeyer)

Just added blocks to Suneido. This is a Smalltalk idea. Ruby also has blocks although in a little different way from Smalltalk.

Basically, a block is a chunk of code within a function, that can be called like a function, but that operates within the context of the function call that created it (i.e. shares its local variables). But the cool part is that a block can outlive the function call that created it, and when it does so, it keeps the context (set of local variables) alive. For example:

make_counter = function (next)
    { return { next++; }; };
counter = make_counter(10);
    =>  10
make_counter returns a block. The block returns next++.

At first, it seemed like a tough thing to add. One of the big issues is that function call contexts (Frame's) are kept on a stack because this is much faster than on the heap. But that won't work for blocks that outlive their function call. Smalltalk handles this by allocating contexts on the heap, but this is much slower than a stack. Then I remembered a discussion of this in ``The Design and Evolution of a High Performance System'' by David Unger. The idea was to use a stack for contexts, but to catch the cases where a block outlived its creator, and to move the context to the heap. This is what I did for Suneido. There were only two cases I had to handle - where the block was returned from the function, and where the block was assigned to the member of an object. As far as I can tell, those are the only ways for a value to survive a function invocation. It took about a day to add this, with a little refactoring of existing code, and of course, a test for it.

So now Suneido has blocks. Of course, nothing uses them yet, but I have some ideas.

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!