Monthly Archives: June 2005

Interpreted Languages – Python and ActionScript

As you can tell I am biased, I love interpreted languages. It is a simple decision for me, it is 95% productivity and 5% performance.&

I use Python and ActionScript extensively in my daily work. I get a very high level of productivity from using Python given its solid standard library and its simple readable syntax. Object oriented concepts are not buried in quirky syntax and you can do quite a lot in 5-6 lines of code. ActionScript is also very productive. It has a simple set of intrinsic classes that can be used to quickly build any type of object and integrate with components and external data sources. AS is very malleable and is an excellent language for UI work. Both allow for cross platform and cross device applications. As a client/server pair they are a near perfect solution (NEW BUSINESS,WINK,WINK,NUDGE,NUDGE).

As for performance, you can always scale something after you have working code but rarely before. As many will attest, pre-mature optimization is a hidden killer of so many projects. With a high level language you arrive at a testing phase much faster than with a compiled language, this is very true for Python development.

Python internally is built on C and internal objects are equivalent to their C counterpart(at runtime, they are C objects). Python just provides a light layer atop C and exposes objects in an interactive and interpreted manner. Performance wise, under the hood you are working with C. Second Python has some external libraries to speed up the execution of slower aspects of your application. Below are four more useful performance enhancements for Python:

Python C API – Python can be extended with C. You compile modules in C and you can access them from within Python itself. Ideally slow modules can be compiled and run at C speed.

PSYCO – Psyco is a Python extension module which can massively speed up the execution of any Python code. 2x to 100x speed-ups, typically 4x. Typically requires adding 2 lines of code into your application.

PYREX – Pyrex lets you write code that mixes Python and C data types any way you want, and compiles it into a C extension for Python.

IRONPYTHON – IronPython is a new Python implementation targeting the .NET and Mono platforms. You write code in Python and out comes MSIL.

The other thing to keep in mind is that computers are getting much faster. Languages like Python and ActionScript stand to get exponentially faster over time regardless of changes to the runtimes. I am sure we will see massive performance gains in both Python and ActionScript in the near future.

My 2 cents.


Python CPU Tuning

I was working on a server for a client in Python using SYNC and we were seeing CPU utilization of 100% on WIN32 ( 2000, XP, 2003 ). I was a puzzled as I know Python was just listening to the OS select loop for socket events.

It turns out that Python internal C FrameLoop is fast, actually on my machine it processes 1,407,750 iterations per second. If you are doing anything inside a loop, it can (AND WILL!!!) quickly push CPU to 100%. The trick is to put a very small delay (10-200ms) into the loop to slow it down. With wait(.2) inserted into the server, the CPU went to < 1% CPU during base execution and the app performed identically. Even under heavy load (1000 messages per second), the server would reach 10% CPU at most.

Here is the Python app I wrote to test this assertion:

import time

# time period
timePeriod = 3
t0 = time.time() + timePeriod

while time.time() <= t0 :

    i = i+1

print( ‘iterations/sec : ‘ +str( i / delay ) )

Python is so fast, I had to slow it down. Never thought I would say that.



Flex and URLRewriting >> FlexRewrite

Combined with URLRewriting, Flex and Flash makes an ideal templating solution. It takes apache/mod_rewrite to make this work but it is very clean and solves many problems with using Flash Player as a template in the presentation layer.

Before I explain the solution, you should see this in action. Hopefully the lightbulb will go off for you shortly.

Try these URLS: spider/every/page/and/you/can/change/the/html/content/to/feed/them

First, these paths and directories do not really exist, they are generated via mod_rewrite and apache. Basically any path past the /FlexRewrite/ directory is dynamic via a single php document. You can use any server side language that is supported on apache to generate the HTML output.

Second, the Flex SWF file always has the same URL, thus it caches in the browser and is not downloaded but once. Using one template swf, I can make N pages. With an inner viewstack, I can also change the view depending on the path.

Using this technique, you can make a massive directory of content and have a single template swf file to drive the whole thing. The data changes but you reuse a client site template. For each page you can have unique data on the base HTML page for spiders and search engines. Also as the URL works as expected, both the back button and bookmarks work as expected.

Also the bandwidth math works well. Assume you have 1000 pages of data and 1 SWF file. Using Flex the SWF is 130KB, so the first page is 130KB and each additional is the size of the base HTML or (<2Kb). That beats the bandwidth profile for most HTML sites without images or interactivity.

When you open this ZIP file you are going to laugh at the contents. Yes that is all it takes. The key is the .htaccess file that sets up the dynamic directory.

SOURCE FOR FlexRewrite

Happy Templating with Flex and Flash.



Property binding in Flex

Outside of layout management, property binding is my favorite feature of Flex. It is so drop dead easy that it should be criminal. This features has pulled me out of a jam in several situations.&

Any property in MXML can have a property binding. All you do is put curly braces within the quotes for the property and whatever AS is within will become a function at compile time. It sounds bazaar but it becomes second nature one you try it.

The above MXML will render the current system date/time onto a button. The call the new Date().toString() returns a string of the current time. The code within curly braces is put into a function called when the label value changes.

It also supports multiple calls too:

In this one I assigned the button event “click” to a function defined within curly braces. In this case, when click happens, the code in curly braces will call myFunction or the series of functions.

Another handy use is positioning. Flex masks all movieclip properties to allows bindings to be supported. So if you find that you need to roll your own positioning within a canvas here is how to do it.



So next time you need to code your way out of a layout issue, just think property bindings. You can make some killer layouts using canvas and property bindings.



Code Editors and Invalid State

Text is very powerful and at the same time fragile. One single typo and XML/Code breaks. What I find fascinating is that during editing, text resides in an invalid state a majority of the time.

When we edit code or XML, we must break its logic to enhance it. With each keystroke, an application is either valid or invalid from a compilation or validation standpoint. My question is why do we have to work at this low level? Why can’t we work at a higher level with objects and generate perfectly valid output and minimize/eliminate time in an invalid state.

Now you’re saying to yourself, “Ted, we have tools that do this”. I honestly believe we don’t. If you include the best editors on the planet and the most expensive case tools, in every instance I can find, code is in an invalid state at least 80% the time during development. We have tools that automate and guide text editing, they paste blocks in context, they insert blocks of code, they script the cursor, and sometimes they even pre-compile blocks to test validity inline. In all these cases, a majority of the time text is in an invalid state.

So what would a managed editor look like and how would it work? The goal would be to output a perfectly valid file system of files for compilation. Each project would be fully managed and valid for the target output. The goal would be to shorten the time in an invalid state by adding and removing code elements at the object level. Code output would always be valid so long as your were editing via the object hierarchy.

Take Flex MXML for example:

The user would create a project, add controls, set properties, add methods, save, and output for compilation. During editing each MXML tag would have an editing class based on the MXML schema. Also each ActionScript code block would have an editing class. In moving MXML blocks around, the editor would understand the tag hierarchy and prevent the insertion of certain blocks within non-compatible blocks, the same would be true for editing ActionScript. Ideally the user could grab a method or property and drag it from class to class. Since all code would be generated, you could paint with a much larger canvas and eliminate many low level errors in code development. Ideally you could also unit test code blocks inline. Simply click on a function or class and add a series of unit test cases for validation and press “Unit Test”.

To make this happen, each text format would need a schema, even code. The schema would need to contain code block dependencies and be able to denote child dependencies in a hierarchy. The editor would use this hierarchy to generate editing objects and manage them in context. Ideally if you added a schema for a particular language to the editor, it would provide a rock solid default editing format. It would allow code to be imported into a project and parse the text into a valid object state. Each language could also be extended to support custom dialogs and editors at the tag of code level. Assuming you select a code block, you could see properties in context.

In one sense, we have come a long way in software development, but in another, we are still crawling along by working with code by majority in an invalid state as we enhance it. It is about time we had a higher level editor to do allot of the grunt work and validation.



Primitive Performance – The hidden object on "." Operator use

It turns out primitive values have a hidden performance problem. At runtime, primitive values are changed into Objects when they are accessed with the “.” Operator.&

It seems that when you use the “.” Operator on a primitive value it upgrades the primitive to a hidden object of equivalent type. For example:

a = “Hello World”
a.split() //primitive upgraded to new String(“Hello World”) before “split” is called.

a = true
a.valueOf() //primitive upgraded to new Boolean( true ) before “valueOf” is called.

a = 23
a.valueOf() //primitive upgraded to new Number( 23 ) before “valueOf” is called.

The problem is that for each access to a primitive value (use of “.” Operator), a hidden object is created and destroyed. If you use the object originally, the hidden object is never created.

From what I gather primitives do not provide access to the Class and thus must be turned into an Object to allow for methods or properties usage. The results occur very fast as they are within the player code, but they do impact both AS performance and memory consumption/garbage collection.

Here is a little proof. Run the test twice changing the value of the “a” variable from a primitive to an object.

a = “Hello World”

//a = new String(“Hello World”)


//if a is a primitive, it will be upgraded to an Object
//if a is an object, it will directly execute the method
a.split(” “)

trace( getTimer() )

In my tests:

String Primitive: 310
String Object: 221 //faster AS execution using Object

**Access to String.length is equivalent in speed in object or primitive**

Number Primitive: 140
String Object: 62 //faster AS execution using Object

Number Boolean: 135
String Object: 71 //faster AS execution using Object

The hidden problem is memory consumption and garbage collection. In the Object case, there is only one object created, where as, in the primitive case there are 10,000 objects created and destroyed and ready for garbage collection. Yes, your heard that correctly, there are 10,000 extra useless objects in player memory to be garbage collected because a primitive value was used. This forces the player to do allot more work than it needs to and forces many more objects into memory to be disposed.

Long story short, use the Object version of primitive values in your application and not the primitives themselves especially for method intensive variables. If you use methods or properties on primitive values, the player is forced to create and destroy an object with each access.

I certainly would have thought that the primitives would be faster. Is that weird or what?