Friday, 25 November 2011

Header files

One of the things I had to unlearn when moving from C/C++ to ActionScript was about header files. In C and languages based on C it's common to create header files for constants and definitions used in the program, which are then included in any source file that needs them. Often some or all headers are included in all source files, alongside system headers. The only cost in doing this is compile time, though even this can be mitigated by precompiled headers and incremental compilation, or ignored with a fast enough compiler.



But this doesn't work in ActionScript. If a header of constants is included in an ActionScript .as file it is copied into the compiled bytecode, not optimised away. With large headers across a number of ActionScript files this can significantly impact the file size.


When I discovered this when making Bug Tunnel Defense it was adding over 100k to the file size, or at least that's what I got back when I removed the headers and moved all the constants to classes (mostly to existing classes though I created a new class for colours). And the game was only part done at this point; if I had continued adding constants to headers and including them everywhere the eventual cost would have been two or three times this, making the game without music up to 50% bigger.

No comments:

Post a Comment