
I don’t remember the last time I was as excited about development as I am now, and for discovering old news no less! Enter Adobe Alchemy.
To give you some background on the problem, I am currently working with an excellent team on a huge project (top secret). It is so huge that we are trying to retrieve half a million records per call from the server and process it with complicated algorithms in a browser. A tall order for Flex. Yes, we are using BlazeDS and we’ve added Vector Class from Flex SDK 4.o. We even did some Number to int and back magic (shoutout to Chris and Jeff) to compress the data going over the wire, but the processing of the result is “less than ideal”.
This is where Alchemy steps in. Thanks to a great article by Ralph Hauwert, we have been able to tap into power of C inside Flash Player! To quote Alchemy’s site regarding that: “…performance can be considerably faster than ActionScript 3.0 and anywhere from 2-10x slower than native C/C++ code.”
The combination of C and Flex sounds like a gift from the heavens. We still use Flex as we always have with all its advantages, but now we have something for really heavy lifting – C. I kid you not, Alchemy allows you to include C code into your Flex project and access its power through actionscript in a familiar and comfortable way!
While setting up Alchemy is not immune to snags, once it’s running, it’s a breeze to use. That is, if you know how to code in C. I don’t, I just started learning and it’s not such a bogeyman. The beautiful thing about it is that you are not suppose to use C to develop your application, but rather to offload processor intensive tasks. You don’t need to know a lot of C for that. That gives you the best of both worlds.
Here is a small performance comparison test based on the “stringecho” sample from the Alchemy package so you can see what I’m talking about. Both C and AS3 are doing exactly the same thing, iterating through a very simple loop. Follow the image link and source code will be available on right-click. C code is below.
Special thanks to the Adobe Alchemy team, Mike Chambers for the examples and of course to Ralph Hauwert for his excellent article on this topic!
C code “embeded” in Flex:
//Simple String Echo example //mike chambers //[email protected] #include <stdlib.h> #include <stdio.h> #include <string.h> //Header file for AS3 interop APIs //this is linked in by the compiler (when using flaccon) #include "AS3.h" //Method exposed to ActionScript //Takes a String and echos it static AS3_Val echo(void* self, AS3_Val args) { //initialize int to zero int val = 0; //parse the arguments. Expect 1. //pass in val to hold the first argument, which //should be an int AS3_ArrayValue( args, "IntType", &val ); int i = 0; char buffer[10]; for (i = 0; i < val; i++) { i = i + i - i; } sprintf(buffer,"%d",i); return AS3_String(buffer); } //entry point for code int main() { //define the methods exposed to ActionScript //typed as an ActionScript Function instance AS3_Val echoMethod = AS3_Function( NULL, echo ); // construct an object that holds references to the functions AS3_Val result = AS3_Object( "echo: AS3ValType", echoMethod ); // Release AS3_Release( echoMethod ); // notify that we initialized -- THIS DOES NOT RETURN! AS3_LibInit( result ); // should never get here! return 0; }

