Pull to refresh

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.

 

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:

  1. 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.

 

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.

 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:

 

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?

 

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:

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?

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.

Tags:
Hubs:
You can’t comment this publication because its author is not yet a full member of the community. You will be able to contact the author only after he or she has been invited by someone in the community. Until then, author’s username will be hidden by an alias.