Flash/as3 custom namespaces and performance: the name qualifier is slow

I was thinking about how to organise the classes of one of the frameworks I’m working on these days. Two options: either dump everything in the same package, and make everything internal, or use namespaces to keep the API clean.

So, yeah, namespaces sounded like the right way to go. A lot of the functionality in the framework involves deep tree tranversal however, so I found myself thinking about the performance implications of using custom namespaces on ‘hot’ properties.

I put up a little benchmark to see whether namespaces slow down code execution. It runs four tests: a for loop that extracts a value from a public property of the object of the calling method, one that accesses a protected property of the object’s superclass, one that accesses a property in a custom namespace after a ‘use namespace’ statement, and one that accesses the property with the :: name qualifier. You can get the sources here.

On my notebook, the test (running on release player 10) output the following:

Accessing a property in the public namespace.
>	Running test @ 1000000 iterations ...
	Execution took  0.003  seconds.
Accessing a property of the superclass in the protected namespace.
>	Running test @ 1000000 iterations ...
	Execution took  0.003  seconds.
Accessing a property in a custom namespace.
>	Running test @ 1000000 iterations ...
	Execution took  0.003  seconds.
Accessing a property in a custom namespace with the :: name qualifier.
>	Running test @ 1000000 iterations ...
	Execution took  0.263  seconds.

Properties in the custom namespace were just as quick to access as those in the ‘built-in’ namespaces when made available with a ‘use namespace’, and ridiculously expensive when accessed with the name qualifier.

Brief, avoid using :: in loops when performance is at stake.

Bookmark and Share

5 Responses to “Flash/as3 custom namespaces and performance: the name qualifier is slow”


  • For the fast case, about 3ns per iteration, is the jit really giving you that or has it managed to optimise away the loop entirely?

  • I don’t think the jit does any lazy execution optimizations; I will look into that again.

    EDIT:
    I reran the test with

    use namespace testns;
    value = value + 1;
    //and
    testns::value = testns::value + 1;

    Now it gets and sets the variable: hence no way for the loop to be jitted-out (even if the avm2 jit WERE doing such heavy optimisation, I have little doubt that it doesn’t.)
    So results, in release fp10 and on the same machine, are as follows:

    Accessing a property in a custom namespace.
    > Running test @ 1000000 iterations …
    Execution took 0.004 seconds.
    Accessing a property in a custom namespace with the :: name qualifier.
    > Running test @ 1000000 iterations …
    Execution took 0.501 seconds.

    Again, you see that the time it takes for the name qualifier to do its job twice is double. It’s true that the other stuff is just suspiciously quick; but hey, that’s very good. Why don’t you play around with the test and see what happens? Share back.

  • What are the results when you implicitly use a namespace by importing it or if you do ‘use namespace “uri”;’?

  • Hi Hristo, I just started adding AS performance tests into a wiki about 3 days ago, would be cool to add this one in as well.

    http://nicolasschudel.name/actionscript-performance-wiki/Main_Page

  • @Nicolas, I’m overwhelmed with stuff right now, so can’t really make it. But you go for it, include any content you want in the wiki. I’ll give it an eyeball one of those days, promised.
    @Mims H. Wright, the “uri” variation would be an interesting thing to check – I bet its just as quick as use namespace namespaceReference, as in as3 two namespace objects with the same url equate … curious though. I’m not sure I can make the check anytime soon though, so if you could pull the sources and run the check for us, and then even post back – well, that would be nice.

Leave a Reply