Continuations are the new threads

I’ve always thought that continuations are the right answer to many if no all the network-programming problems.

What are continuations?

A continuation represent the state of execution of a function: all the local variables and the instruction pointer (the last line executed).
(Update: At least: there can be many kind of continuations. Other than the local variables, it’s possible to store the state of the entire thread/process/stack)

What are them used for?

You can interrupt the execution of a function and resume it later.
This can be useful to create “generators“: iterators made simple.

A generator looks like this:

[js]
function next() {
for(x=0; x < arr.lenght; x++) {
yield arr[x];
}
}
[/js]

The function next return the next element in the arr array every time is called.

When you call the function you resume the continuation, so the execution will resume from the yield.
If you want to implement an iterator like that without generators you'll have to explicitly unroll the for loop.

The resulting code will be surely less readabe.
Like:
[js]
function next() {
if(this.current == null) this.current = 0;
this.current++;
if(this.current > arr.lenght) {
this.current = 0;
return null;
}
retrun arr[x];
[/js]
This can get really worse for every non-trivial iterator (eg: tree traversal).

Another nice use of continuations is to generate cooperative threads.
They are different from standard threads because they can't be stopped, they should cooperatively stop.

For example you can do:

[code]
function process() {
sendConnectRequest();
yield;
if(!this.connected) return false;
sendLoginRequest();
yield;
sendMessage();
yield;
trace("msg received: " + this.response);
}
[/code]

The process function return the control to the calling function every time it has to do an asynchronous operation.
Without continuations you'll have to do:

[code]
function process(onComplete) {
connect(function (connected) {
sendLoginRequest( function(logged) {
sendMessage( function (response) {
trace("msg received: " + response);
onComplete();
}
}
}
}
[/code]

As you can see we have used closures in a Continuation Passing Style:
Every time you have to do an asynchronous operation, you'll have to pass a function (a callback) that will be called when the operation is done.

Without closures the code will look even worst, this is why recently there is a great buzz about closures in the java world.

But, what have in common continuations and threads?

A continuations-like style of programming (but without continuations) is possible only using threads.
However, threads are the heavier and most problematic way to handle the I/O waitings. (see: 10Kproblem)
You have not only to deal with concurrency problems with read/write variables, but you have to block an entire thread for every I/O operation.
A better way is to use asynchronous events, like in the last two example. But of the two, which one do you prefer?

If your answer is like mine, take a look to:

  • Jetty servlet engine, it support a limited form of one shot continuations: the function is re-executed from the beginning, what is restored is only the HTTP connection state.
  • RIFE continuations, it's a bytecode-level implementation in Java, these are real continuations
  • NarrativeJS, a precompiler that create a simple form of continuations in Javascript: using the -> operator the execution can be resumed from that point.
  • Update: Torsten Curdt suggest Javaflow, a continuation implementation that seems really interesting, he offers some tutorials on his blog too

Jetty scratched only the surface of what is possible with continuations, while with RIFE it's possible to develop highly concurrent servers using cooperative sessions instead of threads (some >10000 connections instead of 1000). See: Mina Java-NIO library, Perl POE, Mina-Mule.

A server that uses continuations, will allow the simple and linear programming style of blocking-synchronous operations while using non-blocking ones. With a huge increment in performance.

In the client side, the most interesting is NarrativeJs, that use an approach that can be adapted to every language:
it unroll loops and transform all local variables in object variables to generate his quasi-continuations.
[js]
function boo(){
alert("start");
sleep->(30);
alert("end");
}
[/js]

became:
[js]
function boo(){var njf1=njen(this,arguments);nj:while(1){switch(njf1.cp){case 0:
alert("start");njf1.pc(1,null,
sleep,[30]);case 1:with(njf1)if((rv1=f.apply(c,a))==NJSUS){return fh;}
alert("end");break nj;}}}
[/js]

The code looks ugly but it is so to preserve line numbers.
With narrativeJs you can do complex animations in a simple, linear way:
[js]
waitForClick->(theButton);
animate->(theButton, "left", 200, 1000, 20);

theButton.innerHTML = "go left";

// move the button to the left (again note the blocking operations)
waitForClick->(theButton);
animate->(theButton, "left", 0, 1000, 20);
[/js]

or:
[js]
document.getElementById("myElem").innerHTML = fetch->("http://www.url.com/");
[/js]

Really interesting now that Flash and Ajax are pushing asynchronous operations to the masses.
Will we say goodbye to events, and maybe to threads as we know now?

My two cents on Static Typing

While I was wondering if using haXe or Java with my server, I’ve stumbled upon some discussion on Static Typing (Java) vs. Dynamic Typing (Ruby, Php).
In a really interesting blog post (read the comments too) I found this touching phrase:

“Static typing is Communist Bureaucracy”.

In short: since we (should) have pervasive testing, Static Typing is only more testing. However, a comment is enlighting:

I’m finding this logic hard to follow. Testing is good, coverage is good. Static typing is, among other things, a way to have the compiler test your code for type issues. Since we like testing, this makes static typing good, right?

But Static Typing not only prevent you to do mistakes, like typos, it allows your IDE to know what you are doing:

Dynamic Static Typing speeds up development because there is physically less to type since ctrl+space and ctrl+2 write the declarations for you.

Its seems obvious to me that Dynamic Typing is worth the catch only for a quick start or for learning.
Since almost all of the dynamically typed languages (like Actionscript, Perl, Php) have migrated to some kind of Static Typing (eg: Type Inference), there should be a reason.

Periodically, a new Dynamically Typed language spawn, attracting junior/tired programmers with the mirage of the simplicity and the less-to-type-ing, riding the hype until his young audience grows and start doing large projects hoping not to call a function with the wrong arguments.

Only then they realize the big truth: Static Typing is just more informations in the source code.
And more information means less wondering-what-object-was or what-was-the-method-name-and-parameters or who-instantiate-this-object, not to mention static analysis.

And hey, they have invented Javadoc-like documentation and Annotations just to add some more information, why remove one of the most important?

MindFrame javascript templating+framework

The Javascript’s world is spinning faster in the last times. An interesting framework was beta-released today, called MindFrame, it allows to define complex behaviors directly in the HTML source using a special syntax.
The parser used is Zparse.

You have to see the syntax in the demo to understand how cool it is.

The idea of parsing an HTML element as the source of a template language in Javascript to produce a RIA really blow my mind off, may be the sign of Javascript surpassing Flash?

Markdown in Javascript

Cross-site javascript vulnerability re-discovered

Richar Leggett wrote on his blog that someone seems to have (re)discovered a new AJAX applications vulnerability and published a paper about that.

However, this type of vulnerability is already known:

when you do a request with Javascript you can only do that to the same domain of your script, but this doesn’t apply to img and script tags

So if you browse a malicious site, this can do a request to any site trough your browser (with your cookies and credentials).

In Flash this isn’t possible thanks to the crossdomain.xml.
However ever this can lead to vulnerabilities if misconfigured or because of the new loadPolicyFile Flash function.

world’s smallest website & lemmings return

Some quick surfing suggestions:
Have you seen guimp? The world’s smallest website, enjoy it :)
And I’ve re-found Lemmings DHTML, I remember the author was forced to shutdown the site for copyright issue (it uses the original graphics from the game), I wonder how he had solved them…

Some useful Javascript tools

Because I’ve come back from my holidays and I have some spare time, I’m wondering if I can do some nice stuff before the next exam session (olny 2 left!).
First, I’ve start looking for some html/javascript tool, and I’ve found something interesting:


A Mouseover DOM Inspector (try it now clicking here!)
You can save it as a bookmark and start exploring every web site you visit.

There is also a similar tool packed as a Firefox extension: Aardvark, but it has less features.


Then an image crop tool in JS: 12ImageCrop. It work nice with CakePhp (copy it in the webroot/12cropimage folder and add the js where you need it). See the Demo

I’ve found also this one based on Scriptaculous & Prototype, it seems better but I haven’t try it yet: JavaScript Image Cropper UI, see the Demo

Now something on CSS: I’ve found this comparison between css optimizers, and Icey’s CSS Compressor seems to have won the challenge. It can be really useful.

DHTML Timeline

(Found on Protozoo.com)
This is a wonderfull timeline visualization tool, it graphically show the time and date when an event occur.
From the site:

Timeline is a DHTML-based AJAXy widget for visualizing time-based events. It is like Google Maps for time-based information

The event data can be loaded from XML or JSON. Clicking on an event show a box with event’s detail. It is even possible to distort the time in certain period, for example “zooming in” some days richer of events.
This an example of good javascript use, I wish we could see more of that.

Timeline in action:

Timeline DHTML