[Lua practice] Sorting out the ignored problems in Lua

Insert image description here

1. Metatable and metamethods

1.1 Meta method _index can be set to table

a = {k = 'va'}
b = {kb = 1,k = 'vb'}
mataTb = { __index=b }
setmetatable(a,mataTb)
print(a.kb,a.k)--输出为 1  va

1.2. Meta method _index can be set as a function

a = {k = 'va'}
mataTb = { __index = function(tableA, key)--将a和键传给该函数
	return 'key:'..key --此处..是拼接两个字符串
end }
setmetatable(a,mataTb)
print(a.kk,a.k)--输出为 key:kk  va

1.3. Meta-methods _index and _newindex implement read-only table

b = {k = 'v1'}
function func(tb, key, val)
	print('你不能修改表')
end
a = {}
setmetatable(a, { __index = b , __newindex = func})
a.k1 = 1
print(a.k, a.k1)
--上述输出为
你不能修改表
v1	nil

1.4. Ignore meta-methods to extract values ​​rawget and rawset

To put it simply, it is to get and set directly without using the metamethod of the metatable.

local tableA = {}
local tableB = {NUM=100}
local tableC = {}

setmetatable(tableA,{__index = tableB, __newindex = tableC})
print(tableA.NUM)
print(rawget(tableA,"NUM"))

tableA.NAME = "AA"
print(tableA.NAME)
print(tableC.NAME)

rawset(tableA,"NAME","I AM AA")
print(tableA.NAME)

-- 输出结果
100
nil
nil
AA
I AM AA

2.Lua forced GC method

2.1 collectgarbage()

function table.count(t)
    if type(t) ~= "table" then
        assert(false)
        return
    end

    local n = 0
    for k, _ in pairs(t) do
        n = n + 1
    end
    return n
end

local t = {
    x1 = "hello",
    x2 = "world",
    [1] = "x1111",
}

print(table.count(t))
t.x1 = nil
collectgarbage()
print(table.count(t))

--上述输出为
3
2

3. The difference between coroutines and threads

3.1 Coroutine coroutine.create() is executed synchronously, not in parallel. It just switches a context, and then switches back after executing the coroutine.

local function run(data)
print("co-body", 1, data.a)
print("co-body", 2, data.a)
print("co-body", 3, data.a)
coroutine.yield()
print("co-body", 4, data.a)
print("co-body", 5, data.a)
coroutine.yield()
end

local co = coroutine.create(run)

local data = {a=1}
coroutine.resume(co, data)

for i=1,5 do
print("main", i)
end
coroutine.resume(co, data)

3.2 Thread lua can only pass lua_newthread() and must rely on other C/C++ systems.

4.The difference between .and: in lua

.You need to pass in the obj object: you can use self:/self. to get the corresponding attributes.

5.lua optimization pain points

5.1 Append array performance: for loop inserts table>table.insert>t[#t+1]

5.2table.concat has better splicing performance than normal string...

5.3 If you use a C# object in Unity such as vector3, the performance of directly transferring xyz is better than the boxed and unboxed vector3

5.4 Use less global variables and more global tables

http://lua-users.org/wiki/OptimisingUsingLocalVariables

local b = GLOBAL
next(table) determines non-empty
for performance is better than while

5.5 Table setting default value

local defaultValues = {
   robotName = "des_3115",
}
 
local ARENA = {
   [1] = { rank = { 1, 1, }, robotGroupId = 5000, },
   [2] = { rank = { 2, 2, }, robotGroupId = 4999, },
   [3] = { rank = { 3, 3, }, robotGroupId = 4998, },
   [4] = { rank = { 4, 4, }, robotGroupId = 4997, },
   [5] = { rank = { 5, 5, }, robotGroupId = 4996, },
   [6] = { rank = { 6, 6, }, robotGroupId = 4995, },
   [7] = { rank = { 7, 7, }, robotGroupId = 4994, },
}
 
do
    local base = {
        __index = defaultValues, --基类,默认值存取
        __newindex = function()
            --禁止写入新的键值
            error("Attempt to modify read-only table")
        end
    }
    for k, v in pairs(ARENA) do
        setmetatable(v, base)
    end
    base.__metatable = false --不让外面获取到元表,防止被无意修改
end
 
return ARENA

5.6 Try not to create tables and closures in for loops

local t = {1,2,3,'hi'}
for i=1,n do
    --执行逻辑,但t不更改
    ...
end

5.7 Lua's GC can be actively called when switching scenes (including C#'s GC to reclaim memory)

5.8 In XLua/ToLua/SLua, try to minimize direct access to unity classes/objects, including setting values, and split the objects into multi-parameter simple types (string, int, float)

おすすめ

転載: blog.csdn.net/aaaadong/article/details/128803702