Arrays, Flash Vectors and a sawtooth memory pattern

158 views
Skip to first unread message

Rezmason

unread,
May 8, 2013, 11:43:35 AM5/8/13
to haxe...@googlegroups.com
Hey friends,

Last night I hunted down a really weird phenomenon in my NME project's Flash target. I was seeing a sawtooth graph for its memory usage in Adobe Scout, and after some digging I isolated the problem to a Haxe function that doesn't explicitly create objects.

What it does do is manipulate Arrays– although I didn't explicitly type the array variable. And by sampling object allocations with flash.sampler.Api, I discovered that Arrays indeed were the primary type of object being allocated as the project ran.

By replacing those Arrays with Flash Vectors (nme.Vector) and explicitly typing the variables in that function– now necessary, because otherwise Haxe's type inferencing seems to type the objects to Arrays– I managed to get rid of the sawtooth graph entirely. 

But would making this change have this effect? And how does a function that uses simple Array indexing end up creating Array objects when run? Was AS3's Array(obj) casting going on under the hood? That's my only hypothesis...

Drakim

unread,
May 9, 2013, 2:23:54 AM5/9/13
to haxe...@googlegroups.com
That's interesting. Is it possible that the lack of a proper runtime type for the Array (as opposed to the vector) causes some sort of array cloning?

Rezmason

unread,
May 9, 2013, 5:22:39 PM5/9/13
to haxe...@googlegroups.com
That's my latest theory– Arrays, as sparse data structures, might do weird things with memory on Flash compared to Vectors.

David Peek

unread,
May 9, 2013, 7:27:35 PM5/9/13
to haxe...@googlegroups.com
Speaking of vectors, is there currently a way to create a vector directly (ie not via an array)? Does flash.Vector.ofArray([1,2,3]) compile into byte code for a vector literal?

The same goes for haxe.ds.Vector actually... it seems like it is backed by flash.Vector, java.NativeArray etc. but always requires you start with a haxe array. Is my understanding correct here?

David

Cauê Waneck

unread,
May 9, 2013, 7:35:54 PM5/9/13
to haxe...@googlegroups.com
That's a good question, David. I've filed an issue about it
https://code.google.com/p/haxe/issues/detail?id=1801

Sorry for the minor topic derail! About the sawtooth memory pattern, I have no idea ;)

2013/5/9 David Peek <ninja...@gmail.com>

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Nicolas Cannasse

unread,
May 10, 2013, 3:39:18 AM5/10/13
to haxe...@googlegroups.com
Le 10/05/2013 01:27, David Peek a �crit :
> Speaking of vectors, is there currently a way to create a vector
> directly (ie not via an array)? Does flash.Vector.ofArray([1,2,3])
> compile into byte code for a vector literal?

Sadly there's no bytecode for vector literal, so it would be necessary
to translate it to new Vector + several pushes. Not sure how much faster
it is than using the current way, especially for small vectors.

Best,
Nicolas

Cauê Waneck

unread,
May 10, 2013, 7:44:32 AM5/10/13
to haxe...@googlegroups.com
Vector's push method can receive varargs. I don't know how this is implemented in the VM, or if it would be faster, but theoretically, we could just call a single push with all the array's arguments


2013/5/10 Nicolas Cannasse <ncan...@gmail.com>

David Peek

unread,
May 10, 2013, 8:34:01 AM5/10/13
to haxe...@googlegroups.com
I wonder what the beautiful AS3 syntax "new <Int>[1,2,3]" compiles to then?

Nicolas Cannasse

unread,
May 10, 2013, 8:45:22 AM5/10/13
to haxe...@googlegroups.com
Le 10/05/2013 14:34, David Peek a �crit :
> I wonder what the beautiful AS3 syntax "new <Int>[1,2,3]" compiles to then?

I think it will be same that flash.Vector.ofArray([1,2,3]), but I might
be wrong. Don't hesitate to check with a bytecode decompiler and report
it to me otherwise.

Best,
Nicolas
Message has been deleted

Simon Krajewski

unread,
May 10, 2013, 9:57:17 AM5/10/13
to haxe...@googlegroups.com
http://simn.de/haxe/flash_signal.jpg

Let's see if Jan_Flanders notices the signal.

Simon

Simon Richardson

unread,
May 10, 2013, 10:19:33 AM5/10/13
to haxe...@googlegroups.com
Each way is different depending on how you create a vector.
Note: this is built with http://wonderfl.net and Version 4.9.0 build 1425567 with playerglobal.swc for Flash Player 11.5. So could be different depending on which version of the compiler you're using.

  1. var a : Vector.<int> = new <int>[1,2,3]; 
  2. var a : Vector.<int> = Vector.<int>([1,2,3]);
  3. var a : Vector.<int> = new Vector.<int>(); a[0] = 1; a[1] = 2; a[2] = 3;

Number 1.

12   findpropstrict Vector //nameIndex = 91
       14   getproperty   Vector //nameIndex = 92
       16   getlex         int //nameIndex = 5
       18   applytype     (1)
       20   pushbyte       3
       22   construct     (1)
       24   dup          
       25   pushbyte       0
       27   pushbyte       1
       29   setproperty   null //nameIndex = 93
       31   dup          
       32   pushbyte       1
       34   pushbyte       2
       36   setproperty   null //nameIndex = 93
       38   dup          
       39   pushbyte       2
       41   pushbyte       3
       43   setproperty   null //nameIndex = 93
       45   coerce         __AS3__.vec::Vector.<int> //nameIndex = 95
       47   setlocal1    

Number 2.

       12   getlex         Vector //nameIndex = 91
       14   getlex         int //nameIndex = 5
       16   applytype     (1)
       18   getglobalscope
       19   pushbyte       1
       21   pushbyte       2
       23   pushbyte       3
       25   newarray       [3]
       27   call           (1)
       29   coerce         __AS3__.vec::Vector.<int> //nameIndex = 93
       31   setlocal1   

Number 3.

       12   getlex         Vector //nameIndex = 91
       14   getlex         int //nameIndex = 5
       16   applytype     (1)
       18   construct     (0)
       20   coerce         __AS3__.vec::Vector.<int> //nameIndex = 93
       22   setlocal1    
       23   getlocal1    
       24   pushbyte       0
       26   pushbyte       1
       28   setproperty   null //nameIndex = 94
       30   getlocal1    
       31   pushbyte       1
       33   pushbyte       2
       35   setproperty   null //nameIndex = 94
       37   getlocal1    
       38   pushbyte       2
       40   pushbyte       3
       42   setproperty   null //nameIndex = 94



On 10 May 2013, at 14:57, Simon Krajewski <si...@haxe.org> wrote:

Am 10.05.2013 14:45, schrieb Nicolas Cannasse:
Le 10/05/2013 14:34, David Peek a écrit :
I wonder what the beautiful AS3 syntax "new <Int>[1,2,3]" compiles to then?

I think it will be same that  flash.Vector.ofArray([1,2,3]), but I might be wrong. Don't hesitate to check with a bytecode decompiler and report it to me otherwise.

http://simn.de/haxe/flash_signal.jpg

Let's see if Jan_Flanders notices the signal.

Simon

Reply all
Reply to author
Forward
0 new messages