8

Flash is known to behave in very unpredictable ways ways when it comes to manipulating data. I'm curious that if there is any performance/memory benefit to using Numbers instead of ints aside from values that need precision. I have heard that some basic operations in Flash may convert multiple times between the two type to resolve the expression. I've also heard that Flash runtime, under the hood, actually maps ints to non-precision Numbers/Floats during runtime. Is any of this true?

Jonathan Dunlap
  • 2,581
  • 3
  • 19
  • 22

2 Answers2

14

Flash runtime is a dark place indeed.

  • As you mentioned AVM2 does convert big ints into Number.
  • Whole Numbers are actualy ints.
  • And there's more stuff about ints.
  • Uints used to be slow used in a loop BUT NOW THEY ARE NOT (results in the article seem to be a combination of weird bytecode generation and JIT optimizations).
  • Numbers take more space in memory but this is nothing compared to even a single JPEG file.
  • Logically it feels better to use uints in loops.
  • Numbers are floating point falues you have to be careful comparing them.

Jackson Dunstan does pretty good tests of different AS3 language constructs performance. Of course it's always good to check results yourself. From the series about 10.2 performance you can see that with every new Flash Player version they optimize something but other things might get slower: 1 2 3.

P.S. This answer might get old very soon and might as well be cited in a couple of years later which of course will be wrong.

Community
  • 1
  • 1
Valentin Simonov
  • 1,768
  • 10
  • 14
0

You don't get any real performance benefit with int over Number. So if you're not using a variable for stuff like loop indices or things that require exact increments, Number is fine. In fact, a Number can be NaN if you get an invalid result, which is a nice benefit.

Almo
  • 15,538
  • 13
  • 67
  • 95
  • Well, you *do* get memory benefits, though - an int uses 32 bits, a Number uses 64 bits. – weltraumpirat Mar 01 '12 at 22:49
  • Also, from your own linked source, this is senoculars reply: "`+ - / * %` are all Number operations. If used on an int/uint, they will be converted to a Number by the Flash player in the background and recast back to int/uint after the operation is complete. `| & ^ >> << >>> `(bitwize) retain the int/uint types in the player and are optimized for them for those types." – weltraumpirat Mar 01 '12 at 22:51
  • weltraumpirat, you'd have to use a LOT of variables for the two-byte difference between int and number to be an issue, but you are of course correct. :) – Almo Mar 01 '12 at 22:53
  • 4
    Nice one citing a post from 2006. uints are no longer slow since... since forever. Don't spread misinformation please. – Valentin Simonov Mar 01 '12 at 23:14
  • Good point. I made the assumption that wouldn't have changed, but that IS a long time. Edited. – Almo Mar 02 '12 at 02:05