[Shootout-list] Many tests cheat

skaller skaller@users.sourceforge.net
04 Nov 2004 03:19:11 +1100


On Wed, 2004-11-03 at 22:22, Einar Karttunen wrote:

> However this makes error handling nontransparent and adds a 
> fixed cost to the code which is why this technique is not 
> commonly used. 

Hmm.. its continuation passing. The same thing that
makes the microthreading work.

> Is this technique commonly used in Felix? 

Microthreading is based on continuation passing.

Most of the test examples are implemented using
a driver that runs a single thread of control.
This 'emulates' a program. Felix actually isn't
designed to produce programs .. its designed to
produce libraries.

> Does it scale and 

It costs one address for every handler you need to pass
down. No one said you can't pass an array of handlers,
so the cost of passing the handlers down could be low,
depending on your design.

> work with deep call stacks? 

the current implementation unwinds the stack
frame by frame. So it the throw performance
is linear in the depth of the call stack.

Unfortunately, the to kill off a thread of control,
I have to zero out the caller address, to make
the stuff popped off the stack unreachable,
so I can't just jump to the target frame,
even though I know which one it is.

This also terminates the thread if an inactive
frame is yielded (a NULL continuation pointer
marks the end of a thread of control).


> I am not saying that it should
> be banned outright, just that it does not look very well
> in the spirit of the test.

If scheme is allowed to use call/cc it should be allowed
I think. Since both use continuation passing as an alternative
to dynamic exception handling.

It is known now these control structures are semantically
equivalent  -- someone recently proved that exceptions are
just as powerful.

Interestingly -- the continuation passing only works
for procedural code. Functions use the machine stack
and can't yield control. So functional Felix code
must throw a C++ exception, and procedural code
must use continuation passing... :))

Here's an implementation!! I must put this in the
library.. :

	proc throw: cont = "throw $1;";

	proc try[t] (f:unit->t) {
		code "try ";
		f();
		goto endoff;
		code """} catch (con_t *x) { return x; }"""
	endoff:>
		}

well something like that -- it converts the C++ exception which
*throws* the error handler .. the catch clause executes it
by returning it to the driver (the driver then resumes the
thrown continuation). An normal return causes the client
continuation to continue by returning its callers address
to the driver (in the usual way at the end of the 'try'
function).

-- 
John Skaller, mailto:skaller@users.sf.net
voice: 061-2-9660-0850, 
snail: PO BOX 401 Glebe NSW 2037 Australia
Checkout the Felix programming language http://felix.sf.net