Lua Performance Optimization

Source: Internet
Author: User
Tags types of filters

Luajit optimizes Lua in many aspects and optimizes many library functions provided by Lua.

Optimization details: http://wiki.luajit.org/NYI

Wiki: http://wiki.luajit.org/Home


Lua optimization details:


1. Frequently used library functions are called using the local method. Note that only one call does not work.

--this is the lowest method    string.rep()--this is a better way    local rep = string.rep

2. Allocate the table size in advance when creating the table.

--allocate the table when you declare it    local tbl = {nil,nil}    table.insert(tbl,1)    table.insert(tbl,2)

The memory allocation of Lua table increases progressively according to the n-power of 2. Therefore, during the continuous table. Insert Process, we will frequently apply for memory space in the early stage. Therefore, applying for memory space in advance can improve performance.


3. Avoid some format operations on long strings. The format operation of long strings is inefficient. For long strings, you can use the... operation to connect strings, which is more efficient than format.

local str = [==[ hello %s ]==]string.format(str,"world")local str = [[hello]]return str.. "world"


4. Use the local method to define functions in a file.

    local plus = function(a,b)        return a+b    end

5. Some structure optimization. Here we define a filter, which has two types of filters: typea and typeB. to traverse these filters, we have three methods to encapsulate the API:

--now we define a filter--we get three ways to define it---all filters listedlocal Filter = {nil,nil}local Filter.typeA = {nil,nil}local Filter.typeB = {nil,nil}Filter.typeA.filter1 = function()endFilter.typeA.filter2 = function()endFilter.typeB.filter1 = function()end Filter.typeB.filter2 = function()end--first wayfunction handler(arg)    if not Filter.typeA.filter1() then        return false    end        if not Filter.typeA.filter2() then        return false    end        if not Filter.typeB.filter1() then        return false    end        if not Filter.typeB.filter2() then        return false    end        return trueend --second wayfunction handler(arg)    for k,func in pairs(Filter.typeA) do        if not func() then            return false    end       for k,func in pairs(Filter.typeB) do        if not func() then            return false        end    end        return trueend--third waylocal func_name_list = { ‘filter1‘,‘filter2‘}function handler(arg)    for _,name in ipairs(func_name_list) do        if not Filter.typeA[name]() then            return false        end                if not Filter.typeB[name]() then            return false        end     end     return trueend--fourth waylocal typeA_func_list = {                             Filter.typeA.filter1(),                             Filter.typeA.filter2()                                           }                        local typeB_func_list = {                             Filter.typeB.filter1(),                             Filter.typeB.filter2()                        }function handler(arg)    for _,func in ipairs(typeA_func_list) do        if not func() then            return false        end    end        for _,func in ipairs(typeB_func_list) do        if not func() then            return false        end    end        return trueend

It is worth noting that luajit has made considerable optimizations for ipairs, but it has not been optimized for pairs and luajit. In some tables, we try to avoid using hash tables, use iparis for traversal.

The first method is the most efficient, but the structure is very bad and it is difficult to maintain the code.

In the second method, the traversal efficiency is extremely low.

The third type is efficient and the code structure is normal.

The fourth is the best Traversal method with a good structure. This method is optimal.


6. Table Traversal


In luajit, ipairs is optimized, but pairs is not optimized. In addition, we recommend that you use for I, # table do end for traversal. The for traversal is also optimized in luajit. Use for to traverse arrays as much as possible. This is the fastest way to traverse arrays.


7. Check the test script for config loading.

-- conf_table.lua      local conf_table = {nil,nil,nil}       conf_table.A = "string A"   conf_table.B = "string B"   conf_table.C = "string C"     return conf_table

-- Conf_notbl.lua

A = "string"

B = "string B"

C = "string C"


-- Conf_test.lua

Require 'conf _ notbl'

Local conf = require 'conf _ table'


Local socket = require 'socket'

Local round = 10000000

Local time = socket. gettime ()

For I = 1, round do

Local x =

Local y = B

Local z = C

End


Print ("conf before:"... (socket. gettime ()-Time) * 1000 .. "Ms ")


Local time = socket. gettime ()

For I = 1, round do

Local x = Conf.

Local y = Conf. B

Local z = Conf. c

End


Print ("conf table:"... (socket. gettime ()-Time) * 1000 .. "Ms ")


Local a1 =

Local a2 = B

Local a3 = C

Local time = socket. gettime ()

For I = 1, round do

Local x = A1

Local y = a2

Local z = A3

End

Print ("Local conf:"... (socket. gettime ()-Time) * 1000 .. "Ms ")

Test results:

Lua:

Conf before: 742.80691146851 Ms

Conf table: 789.02816772461 Ms

Local conf: 403.17010879517 Ms


Luajit:

Conf before: 4.1639804840088 Ms

Conf table: 4.1368007659912 Ms

Local conf: 3.9420127868652 Ms

When using the Lua file to load the config, try to use the local method to save the frequently used conf data. If the usage is small, this is unnecessary.

In addition, it is not necessary to use a table to store the conf data, but the config structure is better.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.