jpauclair

Because standing still is going backward…

Freezing and unfreezing objects

with 2 comments

Wow.. It’s been a long time since my last post! But don’t worry there is a lot of things I plan to talk about.

So let’s start with: Freezing and unfreezing objects

*Erratum*
When I made all my tests, they were builded into FlashCS3 (which only build Flash 9 SWF)

And when playing the animations, it was really ugly.

But when you build the same animation in Flash CS5 (Flash 10 SWF), the animation is really clean and have absolutly no object waiting for GC. which is GREAT.

And if you start a SWF10 preloader and then load a SWF9 file? working great!

And if you start a SWF9 preloader and then load a SWF10 file? Bad GC!

So this post is still valid only if the first thing you run is a SWF9 animation in FP9/10/10.1
*End erratum*

The problem

A few months ago (before FlashPlayer 10.1) I made some comments on the Adobe “Freeing memory” article about the need of manually stopping MovieClip at the end of a timeline animation in order to optimize CPU and memory.

Thibault pointed me to a very nice article (solution) to this problem.

Now that FP 10.1 is released, I just realized that it’s now even worst.

When a MovieClip playhead hit an empty frame on the timeline or a blank KeyFrame it remove itself (and its child’s) from the screen. But everything continues to play until the GC collect them all.

With FP 10, the ENTER_FRAME event is not dispatched anymore and you cannot instantiate a class after root object has been removed from stage, but the visual is still playing.

In fact, the MC doesn’t just keep playing. It keeps instantiating (allocating memory and processing time) clips and child’s and everything that would be required if the original clip wasn’t removed.

This means that even when you have 1000 DisplayObjects on stage, you might manage and compute 4000 in the background without knowing it.

Performance wise, in FlashPlayer 9 it was bad.
In FlashPlayer 10 it was badder… …and in 10.1 it’s just Badderdest!

One big difference since 10.1 : A timeline that has been removed from stage continue to play and dispatch AddedToStage and RemovedFromStage for object that shouldn’t be accessible, hence giving bad information on the stage current state.

Example

Here is video of how this could impact the DisplayObject hierarchy, CPU, and memory.
In the video, wait for the green squares to appear… they are assets waiting for GC.
Blue square mean MC received a “RemovedFromStage” event, and gold square mean assets received “AddedTOStage” event.

Result in FlashPlayer 9 and 10

Result in FlashPlayer 10.1

As you can see, most the sprite are waiting for collection. You can also look at the stats on the bottom of the screen.

If you want to use my profiling tool: FlashPreloadProfiler

Full documentation is also on that page.

Source

SVN source for the example showed in the videos are available on Google Code

Written by jpauclair

August 4, 2010 at 7:43 pm

jpauclair.net: 6 Months – Articles recap.

with 4 comments

For those who started reading only a few weeks or months ago, here is a recap of the technical articles of my blog:


Tamarin Part I: AS3 Array

private function InitializeArray() : void
{
var i:int = 0, myArray:Array = new Array();
for (i = 0; i < 100000; i++) { myArray[i] = i; }
}
private function InitializeArrayBackward() : void
{
var i:int = 0, myArray:Array = new Array();
for (i = 100000; i >=0; i--) { myArray[i] = i; }
}

When testing those two functions in GSkinner PerformanceTest, I get this:
InitializeArray : 11 ms
InitializeArrayBackward : 44 ms

There is something very important to understand when using the Array class.
If you read the Tamarin source code you will discover that Array is made of multiple things…

Read more


Tamarin part II – More on Array and Vector

Start a flash app with

var a:Vector.<int> = new Vector.<int>(1024 * 1024);

Would result in a total memory use (System.totalMemory: including all the other junk)

System.totalMemory = 7 311 360

So now let’s fill this array

var a:Vector.<int> = new Vector.<int>(1024 * 1024);
for (var i:int = 0; i <= 1024 * 1024; i++) { a[i] = 0 }

System.totalMemory = 8 364 032

WHAT!! That’s a 1 052 652 bytes (1 Mo) hole! ouchh

Read more


Base64 Optimized as3 lib

I went through both lib (as3Crypto and PHPRPC protocol) and optimized what I could in the base64 encoding/decoding. The final version is ~700% faster with almost no heap expansion compare to both libs.

as3Crypto is currently adding the modified version to its core

Read more


Reverse engineering AMF3 Vector for Java

Did you ever worked in AS3 with Byte Array and AMF data serialization for transferring object over network ? It really simplify everything. Of course there is a size overhead. But the bottom line is that we can save hundreds of hours by using it.
Since Flash 10, there is one big flaw with AMF: it doesn’t support Vector. Well it’s not true, The FlashPlayer support it, but not what link to it. So you still can serialize Vector object in a byte array and save it in a file for example, but you can’t send it to a web page written in PHP or a server in Java because there is no implementation of those new « flash 10 features (Vector, Dictionary,…)» in the flex framework.

Project Red5 (OSS Socket server) has used it to implement in their engine.

Get around Flash documentation: Read more


AS3 hidden treasure in the mm.cfg file. Revealing and documenting many Flash secrets!

Very simple: 45 undocumented feature of Flash Player!

Read all the secrets


One SWF to rule them all : The Almighty PreloadSWF

This is onw of the “almost hidden” feature of the mm.cfg
Simply said, it enables you to load a SWF before any other running swf and manipulate any as3 online SWF .

Read more


Flash Assembler (not ByteCode) : And you thought it couldn’t get faster

One of the hidden feature of the mm.cfg enables you to output flash JIT-generated assembler to a file.
Here is how to use it for optimization.

Read more


Flash Preload Profiler

Using one of the hidden mm.cfg features again to show how powerful the preloadSWF is.
This “Flash preload Profiler” is a multi-purpose profiler that enables you to diagnose visual performance problems before they get too big.

Read more


What’s in the next 6 months?

  • Flash 10.1 is almost released… I guess there is plenty of stuff to discover there!
  • Flash on mobile platform is very interesting and there is so much that could be done in that sector. It opens a whole new interactivity level. I want to be a part of that!
  • I had a lot of fun writing the previous articles, I hope the next 6 months will be as fun and instructive as the ones before!

    Like This!

    Add to Google Buzz

    Add to FacebookAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to Yahoo BuzzAdd to Newsvine

    Written by jpauclair

    May 24, 2010 at 3:00 pm

    Posted in general

    Flash Preload Profiler

    with 6 comments

    In the last few weeks I started to use more and more the mm.cfg feature « preloadSWF »
    This enables you to start a SWF before any SWF launched by FlashPlayer and have much information on the launched SWF. It also enables you to “hook” on the launched SWF and manipulate it’s display list.

    I then decided to try making my own profiler for flash in a way that it could be started for any web site without having an interface too invasive. FlashPreloadProfiler is born.

    FlashPreloadProfiler is an open source multi-purpose profiler designed to help productivity and stability during development by exposing “under the hood” representation of any flash scene.
    The main goal is to help expose and diagnose problems before they get too big.

    It enables developers, artists, designer or testers to see what sometimes “cannot be seen” such as:

  • What is the current FPS and memory statistics of a SWF
  • Where are all mouse event listeners objects
  • What is the global scene overdraw
    • How many sprites are contained in the scene and may be one over the others
  • What is the life cycle of all the display objects on the stage
    • Instanciation, AddedToStage, RemovedFromStage, Garbage Collection

    Want to know more?

    Full description and download here!

    *the profiler is on the top-left corner of the application

    Like This!

    Add to Google Buzz

    Add to FacebookAdd to DiggAdd to Del.icio.usAdd to StumbleuponAdd to RedditAdd to BlinklistAdd to TwitterAdd to TechnoratiAdd to Yahoo BuzzAdd to Newsvine

    Written by jpauclair

    May 15, 2010 at 9:46 pm

    Posted in general

    Tagged with , , , ,

    New Flash Visual Profiler

    with 10 comments

    I frequently use Flex Profiler for my job in order to track slow functions, bad memory usage and other things.

    But when it come to diagnose a problem in a project, it’s often hard to tell what’s going wrong “in the screen”. You can use other live performances analyzer like “Hi-ReS” or even MonsterDebugger… But it’s always detached from what’s in the screen!

    So I decided to make a Visual Profiler that can help diagnose quickly problem with memory allocation, bad assets re-use, and Garbage Collector Overhead.

    *Edit 2010/05/24*
    This post was a pre-release of my tool “Flash Preload Profiler
    If you want something more comlpete: Get it here
    *End edit*

    The tool is VERY simple but quite helpful.
    1: Draw a RED rectangle when an assets is added to the stage for the first time, then it add the object to a HUGE (Weak Key) Dictionary containing all object in the scene.
    2: Draw a GREEN rectangle when an assets is removed from the stage, set the Dictionary object value to false (removed)
    3: Draw a YELLOW rectangle is the sprite is put on the stage but was already in the Dictionary (meaning it’s re-use and hence no memory allocation was done)
    4: At each frame, Draw a BLUE rectangle (alpha .25 because it adds up!) for every object in the Dictionary that are “still on the stage” (.stage != null) but their flags is set to “removed from stage”

    Here is a good example of what it can look like in a real case:
    Custom Shape Particle (3th example)

    Before:

    After:

    In a bad flash application, this is going to blink like a Christmas tree!
    The more red you see, the more memory allocation is done, and you can see what is allocating it.

    The more yellow you see, the more re-use you are doing!

    The last one is tricky. (The blue one)
    It looks like it’s possible for a MovieClip to be removed from stage, but to continue playing and to receive events until the GC collect it.
    I did a lot of test, and in many cases the result was a lot of blue rectangle waiting for collection.

    The flash GC doesn’t guarantee that all “dead” objects will be collected in a single pass. The GC is lazy and safe.
    But having those blue rectangle collected almost randomly is really fun to see.

    To prove that those assets are still animated, you only have to watch the rectangles move around… And to prove that they are really eligible for collection, you can call the famous LocalConnection GC trick and ALL the blue rectangles will be gone next frame. So when you see blue rectangles, you can assume that some CPU is spent managing, updating, animating thoses assets.

    You can add the source-code right into your project but It won’t be very generic and it would be hard to manage.
    Instead I added it to my MicroProfiler using the PreloadSWF trick.
    This way it can also be use on ANY as3 project (debug or not) on the whole internet!

    Everything is on Google Code (Rev. 49), even the “bin/MicroProfiler.swf” for those who don’t want to build it.

    Enjoy the colors!

    I suggest you take a look at this site with the profiler on.
    Most game are very good target too!
    If you find interesting example please share them here.

    add to del.icio.usAdd to Blinkslistadd to furlDigg itadd to ma.gnoliaStumble It!add to simpyseed the vineTailRankpost to facebook

    Written by jpauclair

    April 20, 2010 at 9:50 pm

    Posted in general

    Flash Assembler (not ByteCode) : And you thought it couldn’t get faster

    with 24 comments

    In the last posts, I talked about what you can get from mm.cfg setups but I didn’t give any GREAT example of how to use that in an optimization context.
    Well here it is:

    var a:int = 1;
    var b:int = 2;
    b = a + a + a;
    

    How could something this simple could be optimized? Can it?
    Well yes it can! Not 10% faster… 300% faster!
    Read the rest of this entry »

    Written by jpauclair

    March 15, 2010 at 9:32 pm

    One SWF to rule them all : The Almighty PreloadSWF

    with 34 comments

    *EDIT 2010-08-27*
    this tool = @deprecated
    the NEW (and way better) version can be found at: FlashPreloadProfiler
    *END EDIT*

    In my last article I introduced you to a bunch of features hidden in mm.cfg,

    Many were very happy to see advanced tracing and logging features.
    Many were happy to be able to see the byte code easily.
    But everybody missed the most important feature!

    The Almighty PreloadSWF

  • Would you like to have debugging information (fps, memory, etc.) for you flash app?
  • Would you like to have Debugging information for other people flash app?
  • Would you like to be able to see flash var passed to a live SWF?
  • Would you like to be able to edit live flash app on any website?
  • Would you like to be able to retrieve encrypted files that have been loaded in an unencrypted format?
  • It’s all possible, and more!
    Read the rest of this entry »

    Written by jpauclair

    February 17, 2010 at 11:04 pm

    AS3 hidden treasure in the mm.cfg file. Revealing and documenting many Flash secrets!

    with 65 comments

    When I found this, I just could not believed it.

    In the past few weeks I did many articles greatly appreciated by the community: FlashPlayer Security, FlashPlayer Memory, AS3 Array under the hood, Optimized Base64 Encoding. But this one, I think, will rule them all!

    I knew for a long time now that flash had undocumented features, little part of flash that could help speed up process (like the memory opcodes) or make interaction easy with right click and that kind of things. But I never thought that FlashPlayer would hide data that could help find bugs, or give better knowledge of how flash is interpreted.

    As you may know, The mm.cfg files is located in:

  • Windows; C:\Documents and Settings\username\mm.cfg
  • OSX; /Library/Application Support/Macromedia/mm.cfg
  • Linux; home/username/mm.cfg
  • This file is interpreted when a Flash Player instance launches a SWF and gives indication of what should or shouldn’t be done.
    For example, most people use this file to set tracing parameters:
    ErrorReportingEnable=1
    TraceOutputFileEnable=1
    TraceOutputFileName=c:\logs\flashlogs.txt
    MaxWarnings=50

    Many other options are specified in the Adobe FlashPlayer Admin Guide… but most of is NOT DOCUMENTED!

    There is a LOT of thing to talk about and many cutting edge tools to improve your understanding of flash.

    So let’s get into it
    Read the rest of this entry »

    Written by jpauclair

    February 10, 2010 at 11:55 pm

    Reverse engineering AMF3 Vector for Java

    with 12 comments

    Did you ever worked in AS3 with Byte Array and AMF data serialization for transferring object over network ? It really simplify everything. Of course there is a size overhead. But the bottom line is that we can save hundreds of hours by using it.
    Since Flash 10, there is one big flaw with AMF: it doesn’t support Vector. Well it’s not true, The FlasPlayer support it, but not what link to it. So you still can serialize Vector object in a byte array and save it in a file for example, but you can’t send it to a web page written in php or a server in Java because there is no implementation of those new « flash 10 features (Vector, Dictionary,…)» in the flex framework. The thing you can do is use an Array instead of a vector, and enjoy the conversion in the Java Hash Map server sie. This is done because of the « sparse possibility of the array » (read my Tamarin Array Analysis).

    One more thing: not only it’s not implemented, but it’s not even documented!

    tonight is gonna be a good good night

    I knew that tonight my wife was going to some bar with a friend and that my two kids would be sleeping after 20h00. So I made a bet with my friends at work that I would reverse engineer the Vector serialization in AMF to implement it in the flex messaging framework in Java. And all that before my wife come back at 3h00.

    Well guess what! I made it in only three hours!
    Read the rest of this entry »

    Written by jpauclair

    January 16, 2010 at 2:31 am

    Base64 Optimized as3 lib

    with 25 comments

    A few weeks ago I started a personal project that needed to send binary data to a text (not binary) receiver. This couldn’t be done without encoding the binary data in a format accepted by the receiver. There is a few well know encoder: Base64, yEnc, etc.
    Base64 can have a 30% size overhead (compare to yEnc) but it’s fast and easy to use. I chose Base64.

    While searching the net for some Base64 lib in as3, I found the class inside the as3Crypto package, and one from the PHPRPC protocol.

    So I started with those lib. DAMN it was slow.

    So why after a few years of as3 there is still no optimized library for something that common?

    I went through both lib and optimized what I could. The final version is ~700% faster with almost no heap expansion compare to both libs.
    Read the rest of this entry »

    Written by jpauclair

    January 9, 2010 at 9:39 am

    Tamarin part III – Garbage Collector in Flash (10.0)

    with 6 comments

    I did a lot of very processing intensive application during the last few years. Applications that required both CPU optimization and memory optimization. And I had a LOT of problem with the Garbage Collector of Flash. It was often holding the CPU for too long thus creating visible pause. That kind of behavior was of course unacceptable for real time application. I tried a lot of things in FlashIDE and FlexSDK to optimize what I could but most of that work was based on trial an error or misunderstanding of how GC really worked.

    So I decided to take this one step further on my own time and read the whole Tamarin source.

    When I first started thinking of this article, I had no idea what I was getting into.
    During my first reading of Tamarin a few months ago, I saw some things about a Mark and Sweep Algorithm… Some ZCT algorithm and I thought everything was kind of trivial. No way!

    *everything that I’m going to talk about is based on my own reading of the open-source framework Tamarin and public news from Adobe. I’m not representing Adobe in any way and if my interpretation is bad about anything, please tell me!*

    As you know, Adobe as worked on bringing flash to portable device for some time now and obviously, memory management and performances of flash was kind of an issue for smaller device. In last march Lars Hansen did a GREAT review of what were the actual Issues of the GC and some actions that should be taken to fix theses.

    I also started a discussion recently with Lars about the F10.1 GC . That GC is more focused on small device and less on application that require a LOT of memory. With some test I did the GC was going nuts and taking 1.6Go of memory with visible freeze of a few seconds. I’ll be filling a bug soon, but if F10.1 is released as the official player soon, a lot of games will fail!

    By the way, If anyone have interesting management solution for memory consumed (allocated and freed) by visual assets created on the timeline. Please post your solutions!

    But enough with this… Let’s start!

    Tamarin GC is a mix of:

      1. Conservative collection with incremental Mark/Finalize/Sweep algorithm
      2. Deferred Reference Counting (DRC) with ZeroCountTable (ZCT)

    Read the rest of this entry »

    Written by jpauclair

    December 23, 2009 at 12:53 am