整理Lua中忽略的问题
- 1.元表metatable和元方法
- 1.1元方法_index可以设置为table
- 1.2.元方法_index可以设置为函数
- 1.3.元方法_index和_newindex实现只读table
 
- 2.Lua强制GC方法
- 2.1 collectgarbage()
 
- 3.协程和线程的区别
- 3.1协程coroutine.create()是同步执行,不是并行,只是切了一个上下文,然后执行完协程切回
- 3.2线程 lua只能通过lua_newthread()必须依托C/C++其他系统才可以
 
- 4.lua中.和:的区别
- 5.lua优化痛点
- 5.1追加array性能:for循环插入table>table.insert>t[#t+1]
- 5.2table.concat比正常的字符串..拼接性能要好
- 5.3如果用到unity中的c#对象比如vector3,直接传xyz的性能好于装箱拆箱后的vector3
- 5.4少用全局变量,多使用全局的表
- 5.5配表设置默认值
- 5.6 尽量不要在for循环创建表和闭包
- 5.7 场景切换时可以主动调用Lua的GC(包括C#的GC回收内存)
- 5.8 XLua/ToLua/SLua中尽量少直接访问unity的类/对象,包括设置值,把对象拆分为多参数简单类型(string,int,float)
 
 

1.元表metatable和元方法
1.1元方法_index可以设置为table
a = {k = 'va'}
b = {kb = 1,k = 'vb'}
mataTb = { __index=b }
setmetatable(a,mataTb)
print(a.kb,a.k)--输出为 1  va
1.2.元方法_index可以设置为函数
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.元方法_index和_newindex实现只读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
2.Lua强制GC方法
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.协程和线程的区别
3.1协程coroutine.create()是同步执行,不是并行,只是切了一个上下文,然后执行完协程切回
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线程 lua只能通过lua_newthread()必须依托C/C++其他系统才可以
4.lua中.和:的区别
.需要传入obj对象 :是可以用self:/self.拿到对应的属性的
5.lua优化痛点
5.1追加array性能:for循环插入table>table.insert>t[#t+1]
5.2table.concat比正常的字符串…拼接性能要好
5.3如果用到unity中的c#对象比如vector3,直接传xyz的性能好于装箱拆箱后的vector3
5.4少用全局变量,多使用全局的表
http://lua-users.org/wiki/OptimisingUsingLocalVariables
local b = GLOBAL
next(table)判断非空
for 性能比 while 好
5.5配表设置默认值
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 尽量不要在for循环创建表和闭包
local t = {1,2,3,'hi'}
for i=1,n do
    --执行逻辑,但t不更改
    ...
end














![[Lua实战]Skynet-1.如何启动(linux环境启动)[开箱可用]](https://img-blog.csdnimg.cn/9ba41846f0a84d89ac5449bed97b462c.png)




