Why does the use of global variables impact performance?
Global variables are not typed. Whenever it is used, the compiler must generate code that handles any possible data types that it may encounter. Hence, the compiler cannot generate highly-optimized code.
What would be a good alternative to using a global variable when it cannot be replaced by a constant?
We can define a typed global constant as a placeholder. The Ref type may also be used to hold a single value for the variable. Because Ref contains the type of data, the compiler can generate more optimized code.
Why does a struct of arrays perform better than an array of structs?
Modern CPUs can perform many numerical calculations in parallel. When the memory is aligned and packed together as in an array, the hardware cache can quickly look them up. An array of structs may have the objects scattered around in...