-
Notifications
You must be signed in to change notification settings - Fork 197
Enable –flto and –flto export options to optimize your application and reduce linking time
You may already know that the -flto flag can be used to generate an optimized build. (For more information, see blog). However, you may have come across a few instances where applications take an inordinate amount of time to build. This article talks about the probable reasons behind a lengthy build and also includes some best practices to optimize linking performance.
You can use sample/09_Pthreads from the FlasCC SDK to understand how the –flto flag works.
Navigate to the 09_Pthreads directory and execute the following command:
time ../../sdk/usr/bin/g++ pthreads.cpp -lAS3++ -lFlash++ -pthread -O4 -emit-swf -o pthreads.swf -flto-api=exports.txt
The real time output would probably take around 40-50 seconds. Now run the following command:
time ../../sdk/usr/bin/g++ pthreads.cpp -lAS3++ -lFlash++ -pthread -O4 -emit-swf -o pthreads.swf
The real time output now takes considerably longer. As you can see, the only difference between the two commands is the “-flto-api=exports.txt“ option.
The -flto-api=exports.txt option is passed to the opt command as the following parameter:
-internalize-public-api-file=exports.txt
Type ../../sdk/usr/bin/opt –help
to see the context-sensitive help for the opt command:
-internalize-public-api-file=<filename> - A file containing list of symbol names to preserve
This means that if the option is not set, all the functions in your application are preserved. If you choose to set this option, all functions that are not preserved or referred by preserved functions recursively, are eliminated.
Type cat exports.txt
to view the exports file. This file lists the functions that need to preserved for FlasCC.
Here’s another example to further illustrate the usage of the –flto option. Copy this file to 01_HelloWorld and navigate 01_HelloWorld directory:
cp exports.txt ../ 01_HelloWorld
cd ../ 01_HelloWorld
Now compile hello.c without –flto-api option:
../../sdk/usr/bin/gcc hello.c -emit-swf -o hello.swf -O4 --save-temps
The -save-temps option tells FlasCC to reserve temporary files. Make note of the size of the temporary files (hello.lto.1.as, hello.lto.2.as, hello.lto.bc, and hello.lto.abc). and the final output, hello.swf.
Now compile hello.c with the –flto-api option:
../../sdk/usr/bin/gcc hello.c -emit-swf -o hello.swf -O4 -flto-api=exports.txt --save-temps
The size of the output file and temporary files are much smaller.
When you use –flto-api, all unused functions from the standard libs are eliminated. This is also the reason for the difference in linking time. FlasCC converts hello.lto.bc to hello.lto.1.as and hello.lto.2.as, and then converts the two .as files to hello.lto.abc. This conversion process takes up most of the build time. Smaller target files therefore result in a shorter build time.
Run the following commands to see the difference in build time:
time ../../sdk/usr/bin/gcc hello.c -emit-swf -o hello.swf -O4 --save-temps
time ../../sdk/usr/bin/gcc hello.c -emit-swf -o hello.swf -O4 -flto-api=exports.txt --save-temps
You will notice that the second command takes considerably less time to execute.
You can also use the swfdump command to understand the –flto option. Open hello.c in an editor and add the following function:
#include <stdio.h>
int myEmptyFunction(){
return 0;
}
int main(int argc, char **argv){
printf("Hello World\n");
}
And then compile it in two ways:
../../sdk/usr/bin/gcc hello.c -emit-swf -o hello_1.swf -O4
../../sdk/usr/bin/gcc hello.c -emit-swf -o hello_2.swf -O4 -flto-api=exports.txt
OK. Now suppose your Flex SDK is located at Flex_SDK, run these two commands:
Flex_SDK/bin/swfdump –asm hello_1.swf > hello_1_symbols.txt
Flex_SDK/bin/swfdump –asm hello_2.swf > hello_2_symbols.txt
Open both the symbol files in a text editor and look for myEmptyFunction. myEmptyFunction is eliminated from hello_2_symbols.txt because of the –flto-api option.
Always remember to explicitly add the functions called by the as3 code, to the exports.txt file.