Saturday, February 21, 2009

Flex's Achilles Heel

When we evaluate rich Internet platforms we tend to focus on the tip of the iceberg, even more so than in conventional software.  After all rich Internet applications are about graphics, animation, and other multimedia right?  By these measures it it is hard to deny that Silverlight and Flex are very evenly matched - for now.  However Flex developers are beginning to notice that Flex lacks a feature that will become increasingly important: Threading

Retrofitting Flex to make it a suitable platform for multithreaded programming is a very hard problem because of its heritage as a vector animation platform .  Assuming that Adobe is willing to go through this painful step the job of making Flex a hospitable platform for multithreaded programing will be far from complete.  Why?

Making it  possible to do threading is a platform problem. Making it easy is a language problem.

Microsoft made a key decision to develop their own programming languages and they've evolved them in ways that make them extremely well-suited to multi-threaded programming.  The results are pretty spectacular.  Here are two examples (coming soon to Silverlight):

1.  Parallel Linq

Parallel Linq is Linq provider that will execute a query in parallel.  In some cases parallelizing a query can be as easy as adding "AsParallel" to it like so:

var q = from x in list.AsParallel() where x < 3300 select x;

It just doesn't get much easier than this.

It is interesting to note that it is the programming language that makes this possible.  Parallel Linq relies on C# and's ability to convert Linq expressions into data.  The Parallel Linq library then analyzes the expression at run-time and determines the best way of executing it.

2.  F#

F# is an extended version of OCaml.  Microsoft research added a feature to the language which makes it incredibly easy to write asynchronous applications.  This is important given that Rich Internet applications spend a whole lot of time waiting on IO.

Let's take a look at an example of F# function which synchronously retrieves the HTML of a web page:

let getHtml(url:string) = let req = WebRequest.Create(url) use resp = req.GetResponseAsync() use stream = resp.GetResponseStream() use reader = new StreamReader(stream) reader.ReadToEnd()

You can think of the "let" key word is equivalent to C#'s "var" and the "use" keyword as equivalent to C#'s "using."  Now let's take a look at what it takes to make this function asynchronous.

let getHtml(url:string) = async { let req = WebRequest.Create(url) use! resp = req.GetResponseAsync() use stream = resp.GetResponseStream() use reader = new StreamReader(stream) return reader.ReadToEnd() }

Can you spot the differences?  The obvious one is the "async."  Less obvious is the "!" in from of the use keyword.  Whenever a "!" is placed in front of a keyword inside of an "async", the operation is executed on another thread and the rest of the computation is suspended until the operation finishes.  When the operation finishes control jumps from one thread to another and the rest of the computation is executed.  No threading code necessary.

Once again it's the language that makes this possible.  How's it done?

In certain contexts F# allows you to overload language keywords and have a custom function execute them instead of the programming language.  This makes it possible to do very complex types of transformations.  In this case the execution of "use!" is handled by a Use method on an instance of the AsyncBuilder class.  It turns out that"async" isn't a keyword, it is actually just an instance of type AsyncBuilder!

This is somewhat similar to implementing the query pattern in C#.The principle difference is that F# supports many more keywords.  To find out more check out Don Syme's talk on asynchronous workflows on Channel 9.

A Blessing in Disguise

A few months ago there was quite a bit of controversy when efforts towards JavaScript standardization fell apart.  In retrospect I believe that this is the best thing that could've happened to Adobe.  They are now free to extend JavaScript to make it a more suitable language for multi-threaded programming.

The real question is: Will they? 

I've been disappointed by the evolution of JavaScript.  Many of the features recently added seem to be inspired by Python, which is not surprising given the similarities between the two languages.  Although Python is a great language for doing dynamic, imperative development it is certainly not my first choice for doing functional programming, and by extension multi-threaded programming.  Unfortunately Javascript seems to have inherited some of Python's mistakes.  For example Javascript adopted Python's iterator model which is inferior to that of .NET's Enumerable model but that's another blog post entirely. :-)

In the interest of making multi-threaded programming (and a host of other problems in different domains) easier I'd add two features to ActionScript:

1.  Code-to-data transformations (which makes Parallel Linq possible)

2. Monad comprehensions (which F# uses to transform synchronous code into asynchronous code)

If Flex stays a one language platform and Actionscript continues to evolve in Python's direction instead of Haskell's direction I see a grim future ahead for the platform.  I hope I'm wrong because competition is a healthy thing :-).


Norman said...

I'm confused about how python's iterator's are inferior to .net's - I use both languages extensively, and while I've written coroutines in python, I can't of how I'd do the same in .net. Could you address coroutines in your follow up post on iterators :-)

Ajai Shankar said...

Hi Jafar

I recently came across your blog and saw the posts on multimethods and monad's.

Really opened my eyes to concepts in functional programming - which I've always wanted to learn

Keep writing for us C# programmers!


Have released the following on codeplex on what somehow relates to the main theme of C# 4.0 dynamic & also async:

Do take a look if you find it interesting.

Howard Richards said...

In your F# example you say the Async and ! are different.. actually you have req.GetResponseAsync()
in BOTH examples.. !

Anonymous said...
This comment has been removed by a blog administrator.
Affordable Luxurious Wedding Dress Blog said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.

About Me

My photo
I'm a software developer who started programming at age 16 and never saw any reason to stop. I'm working on the Presentation Platform Controls team at Microsoft. My primary interests are functional programming, and Rich Internet Applications.