The pit and cool of lua + libuv

Time:2020-11-24

Libuv is the base library of nodejs. C implementation, code is not large, but the five viscera. I prefer its simple API interface to libevent. Compared with libevent, httpserver is less and subprocess function is more. The package is very good, and the traditional fork and pipe are not intuitive.

Lua can be said to be one of the most popular scripting languages: the script language with strong function, least code, high performance and very high force. The design is elegant, with few keywords, which is similar to the idea of golang. There is a strong customizable API interface between C and C. In fact, it completely exposes the interface of the entire Lua virtual machine, and can construct arbitrary Lua code and data structure, which is extremely powerful and easy to use.

The combination of these two projects is very interesting. Essentially equivalent to a small V8, Lua and JavaScript features are quite similar. It does not involve a large number of complex operations, just want to do some rapid development with the characteristics of the script language. This combination is quite cost-effective. Even if it involves a lot of calculation, you can choose luajit, and most of its indicators in the evaluation have exceeded V8.

When the two of them are combined, there are some cool points and pitfalls as follows:

  • Libuv all handle types must call UV_ Close. Otherwise, it will cause memory leak.
  • Lua and C interact through stack operation, which is a bit like assembly. At the beginning, it is easy to get the parameter position wrong. A better way is to use pcall to print traceback and stackeinfo when it crashes.
  • UV on the outermost layer_ When calling Lua in loop, you must start with calling a Lua function. Otherwise, if you make a mistake about the number of parameters, you will have a memory leak. Lua function is protected by default, and there is no problem if you make a mistake.
  • Flexible use of UserData in Lua can do a lot of interesting things. For example, a low-level C module obtains a large buffer and hands it to another low-level C module for processing. You can pass the C struct package to Lua on the upper layer in UserData. In contrast, Python ctypes is bulky and complex, and its functions are simple and inflexible. However, ctypes is well designed. General scripting languages don’t have such a concept at all. It’s just a matter of giving the interface of dynamic library cursory, without careful design and consideration.
  • lua_ Pushcclosure is a very good design. You can bind a C function to several variables in Lua as a closure and pass it to Lua. This is basically the design of killing other scripting languages. With the use of UserData, it can completely solve the various disputes between high-level and low-level callbacks.
  • There is a reason why Lua array subscripts start at 1 by default. It was passed in this way table.maxn You can judge whether a table is a dict or an array. PHP is not good at this point.
  • Uv_ Work will automatically create a thread pool with 4 threads by default, so there is no need to worry about efficiency.
  • Uv_ When a process exits, the process and the pipeline may not be closed first or later. Pay attention to the processing.
  • lua_ Cjson module is the fastest (official website evaluation) JSON module. After analysis, it is fast because it generates various structures in Lua’s virtual machine while parsing. This is also the third-party library of other scripts is impossible to do. It’s probably the fastest JSON module in a scripting language.
  • http_ Parser is a relatively fast and lightweight HTTP header parser. It is also a callback library. It is easy to combine it with libuv to create an HTTP client.
  • Luvit is a good open source project.
  • Both documents are very rich. Libuv can see an introduction to libuv, which is very detailed.
  • Lua’s book is programming in Lua. The free version is not published in 5.2. There are some differences between 5.2 and 5.1. Please refer to the reference manual for Lua 5.2 on the official website.
  • Luajit cross compilation requires a compiler above GCC 4.7. And support large_ File, pay attention to cross compiling tool chain.

Recommended Today

Talking about Java collection

preface Most programming languages provide array to save objects, and array is one of the most important data structures. However, the length of the array has been defined during initialization, which is immutable and troublesome to use. Therefore, Java inJDK 1.2The collection framework is added to the version to save and manipulate objects. The container […]