Very fast byte array with yogda

Starting from Flash Player version 10.1, Adobe has presented new fast memory commands called MOPS. MOPS are designed to provide C like pointer functionality to AVM2. However these op-codes are only utilized by Alchemy compiler ( A C++ compiler for AVM2 platform) and not by Adobe Flash Builder.

To create a flexible and re-usable solution for production environment, we have developed automated inlining functionality for Yogda (v1.0.551). http://www.yogda.com/Yogda.1.0.551.zip

We have utilized code injection & method inlining capabilities in an AS3 memory class , replacing function calls with MOPS bytecodes and archive faster read/write speeds up to 10+ times than standard ByteArray and Vector classes.

Following is the output of test project which uses ByteArray and fast memory classes to read and write 1,000,000 bytes.


Write 1000000 bytes of data to bytearray :104ms
Read 1000000 bytes of data from bytearray :100ms

Write 1000000 bytes of data to fast memory :6ms
Read 1000000 bytes of data from fast memory :8ms

Write 1000000 bytes of data to bytearray :121ms
Read 1000000 bytes of data from bytearray :100ms

Write 1000000 bytes of data to fast memory :7ms
Read 1000000 bytes of data from fast memory :7ms

A test project , memory.as and YogurtFastMemory.swc is included in Demos folder.

Using Fast Memory

Initializing the Array

A byte array must be created and initialized.

After creating ByteArray, byte order property of the ByteArray must be set to LITTLE ENDIAN.

Also size of the ByteArray must be pre set ( multiplied with the size of variable ) , otherwise MOPS may cause range errors at runtime.


testMemory = new ByteArray();
testMemory.endian = Endian.LITTLE_ENDIAN;
testMemory.length = intArraySize*4;

Setting the Target for MOPS

Due to the security, MOPS are not operating globally. In order to use MOPS byte codes, we must set the domainMemory of the current application domain. We have a function called Select in Fast Memory class. Use Select to tell AVM2 which part of memory will be used by MOPS to read/write.


Memory.select( testMemory );

Operating on memory

MOPS are not aware of the variable types. So, the developer should manually calculate the offset for reading/writing the variable in the ByteArray. For example , if you read/write 32 bit floating point values, you must multiply the addressing counter by 4. Same goes for the Word and integer types (2 bytes)

Example :


for (var i:int = 0; i < testBytes; i++) {Memory.writeFloat( i, i *4);  }

Finalizing / Patching the SWF file

Yogda utilizes AS3 metatags to identify which methods are to be inlined. Flash builder compiler ignores custom metatags by default . To enable inlining with yogda , following compiler directive must be added to the project configuration :


-keep-as3-metadata+=Inline

Compile your project with -keep-as3-metadata directive and open the resulting SWF file with Yogda. Yogda will automaticly detect the inlining metatags and open a dialogbox to proceed with inlining . If autodetection fails , verify that you put the compiler directive correctly.

Press “Scan & Process Classes” button , Yogda will perform inlining automaticly.

Finally , Save and run your SWF file.

Warnings

Reading a value using MOPS and not storing it to a variable may cause player to throw stack overflow.


Correct        :  fResult=Memory.readFloat(1);
Stack overflow :  Memory.readFloat(1);

To have more information about AS3 Inlining , visit : http://forum.yogda.com/index.php?topic=16.0