# Pre-compiled byte code

Sometimes it may be useful to use pre-compiled bytecode instead of re-building the scripts every time a script is loaded. This can for example reduce load times, or if scripts are transferred over the net, it can reduce the transfer time for large scripts.

To save already compiled code you should call the SaveByteCode method on the asIScriptModule, passing in a pointer to the object that implements the asIBinaryStream interface. The script module will then push all the bytecode and related data onto this object with the Write method.

When loading the pre-compiled bytecode you should first get an instance of the asIScriptModule, just as if compiling a new script. Instead of calling AddScriptSection followed by Build you will call the LoadByteCode method, passing it a pointer to the binary stream object. The script module will then pull all the bytecode and related data from that object with the Read method to reconstruct the script module.

## Things to remember

- The engine configuration must be the same when saving and loading the bytecode, otherwise the load will fail as functions, types, or global properties cannot be found.

- The script engine doesn't perform validation of the pre-compiled bytecode, so the application is responsible for authenticating the source. If this authentication isn't performed by the application you'll have potential security risks in your application as the bytecode may have been manually manipulated to perform otherwise illegal tasks.

- If the application that compiles the script code is separate from the application that will execute them, then you may register the functions and methods with null pointers so you don't need to actually implement all of them. In this case you should also set the engine property asEP_INIT_GLOBAL_VARS_AFTER_BUILD to false with asIScriptEngine::SetEngineProperty, so the script engine doesn't attempt to initialize the global variables after the script has been built.

## Platform independence

The saved bytecode is not yet platform independent, but the plan is to make it so. The following are items that can make the bytecode incompatible on different platforms.

- Object property offsets may differ for different compilers and different operating systems due to different byte packing schemes. If possible, avoid registering object property offsets to minimize the risk of mismatches. You can for example use property accessors instead.

- The pre-compiled bytecode is currently not independent of the used CPU architecture, a script that is compiled for a 32-bit target is generally not equal to a script compiled for a 64-bit target. The saved bytecode is the same for big-endian and little-endian CPUs though. The bytecode is also independent of the target Operating System, so a script compiled on Linux is equal to Windows, given that the engine configuration is the same for both.