Lua performance optimization skills (VI): final tips

Time:2022-4-28

As we said in the preface, optimization is a highly skilled work. Starting from whether the program needs to be optimized, there are several aspects to consider. If the program really has performance problems, we should focus on where and how to optimize it.

The techniques we discuss here are neither the only nor the most important. Here we focus on the discussion specifically forLuaBecause there are many other ways to understand general program optimization techniques.

Before the end of this article, I’d like to introduce two ways to optimize Lua program performance on a larger scale, but they both involve modifications outside Lua code. The first is to use luajit [1], a real-time compiler of lua, developed by Mike Pall. His work is excellent, and luajit is probably the fastest JIT of all dynamic languages. The price of using it is that it can only run on x86 architecture, and you need a non-standard Lua interpreter (luajit) to run your program. The benefit is that you can make the program run five times faster without modifying the code. The second way is to move part of the code to C. The focus of this article is to choose the appropriate granularity for C code. On the one hand, if you move some very simple functions to C, the communication overhead between Lua and C will offset the performance advantage of writing functions in C; On the other hand, if you move too large functions into C, you lose the flexibility provided by Lua. Finally, it should be noted that these two methods are sometimes incompatible. The more code you move into C, the less optimization luajit can bring.

[1] JIT is just in time, which means just in time compiler.

Recommended Today

[angular] single runtime multi project sharing dependency scheme

ngx-center One center, multiple Introduction to use (video) what is it? A solution of angular single runtime multi project sharing dependency Start only one angular platform(PlatformRef)Other subprojects are exported (class ES6 module) and loaded through the resource file list (or separate JS file) function It supports the refresh of code changes during the development of […]