Unity Performance Optimization Ⅴ: Lua Scripts Optimization
Lua is a powerful, lightweight, scripting language, and is often used in video games due to its fast execution and short learning curve. World of Warcraft, Angry Birds, and Mafia II all use Lua script. However, related performance issues have also emerged. In our GOT Online performance report, we often see Lua-related functions in the Top20 cost list of Scripts. Today, we will discuss the methods to optimize Lua.
![](https://webcf.waybackmachine.org/web/20220316070541/https://habrastorage.org/getpro/habr/upload_files/570/54c/3e8/57054c3e8da2e7f355b8630bb75e6dca.png)
Is there any way to quickly locate the bottleneck of Lua? Let’s open the Lua report of GOT Online to see what great features it has. Here, we will first explain several tags in the report page separately, as shown below, let’s look at them in turn:
Script >> Solve the problem of high CPU Time caused by Lua
On the Script-CPU Cost page, you can see the time consumption on the Lua side.
![](https://webcf.waybackmachine.org/web/20220316070541/https://habrastorage.org/getpro/habr/upload_files/fd7/54c/a81/fd754ca8105b5d9ac0d1a6c6f4d0dccc.png)
By clicking on these functions, we can view the overall time-consuming stack of these functions, the specified scene stack, and the specific time-consuming stack in any frame, and quickly locate the bottleneck function.
![](https://webcf.waybackmachine.org/web/20220316070541/https://habrastorage.org/getpro/habr/upload_files/6e7/fd4/89a/6e7fd489a0e733b55d3f48e8e3ffd714.png)
Note: You can locate the CPU cost bottleneck function and the specific cause of the CPU Cost peak through the Lua file name/line number/function name provided here.
The naming format of Lua functions is X@Y:Z, where X is the name of the function. When it cannot be obtained, X will become the default unknown; Y is the file location of the function definition; Z is the line where the function is defined. It should be noted that when the Lua script is run as bytecode, the value will always be 0, so it is recommended to use Lua source code to run as much as possible during testing.
In particular, stack analysis supports reverse order viewing. In many cases, we need to expand the stack to dozens of layers, but if we switch to reverse order analysis, the original CPU Cost stack will be sorted in reverse order, which will directly highlight the deep sub-function with the largest overhead, and the R&D team can quickly locate the overhead bottleneck.
Taking a project as an example, the following figure is a list of time-consuming functions sorted in top-down order:
![](https://webcf.waybackmachine.org/web/20220316070541/https://habrastorage.org/getpro/habr/upload_files/499/033/abf/499033abf471426a3fa0176c54c92cb8.png)
After clicking on a few layers of stacks, we found that there is a sub-function with a high overhead: ” Instantiate@[string “…”]:10″, but what if we directly view it in reverse order?
![](https://webcf.waybackmachine.org/web/20220316070541/https://habrastorage.org/getpro/habr/upload_files/4ff/ccd/8ab/4ffccd8ab883da9e4a087a6b35602965.png)
Note: You can locate the CPU cost bottleneck function and the specific cause of the CPU Cost peak through the Lua file name/line number/function name provided here.
The naming format of Lua functions is X@Y:Z, where X is the name of the function. When it cannot be obtained, X will become the default unknown; Y is the file location of the function definition; Z is the line where the function is defined. It should be noted that when the Lua script is run as bytecode, the value will always be 0, so it is recommended to use Lua source code to run as much as possible during testing.
In particular, stack analysis supports reverse order viewing. In many cases, we need to expand the stack to dozens of layers, but if we switch to reverse order analysis, the original CPU Cost stack will be sorted in reverse order, which will directly highlight the deep sub-function with the largest overhead, and the R&D team can quickly locate the overhead bottleneck.
Taking a project as an example, the following figure is a list of time-consuming functions sorted in top-down order:
![](https://webcf.waybackmachine.org/web/20220316070541/https://habrastorage.org/getpro/habr/upload_files/620/9c5/f87/6209c5f875b30c3c18f992d399180274.png)
After clicking on a few layers of stacks, we found that there is a sub-function with a high overhead: ” Instantiate@[string “…”]:10″, but what if we directly view it in reverse order?
![](https://webcf.waybackmachine.org/web/20220316070541/https://habrastorage.org/getpro/habr/upload_files/038/632/498/0386324987a0e3e3ed8bf1942bfde1ee.png)
This function with a high CPU Cost will be then listed at the top, and then the team can work on solving the issue related to this function.
![](https://webcf.waybackmachine.org/web/20220316070541/https://habrastorage.org/getpro/habr/upload_files/7c3/ca9/503/7c3ca95036192db3b8a9038a78321160.png)