[英]Does general Lua runtimes optimize table lookups?
Can tables used like namespaces expand their fields before running, to avoid indexing tables ? 像名称空间那样使用的表可以在运行之前扩展其字段,以避免为表建立索引吗? I plan to use Lua's 5.3.3 compiler. 我计划使用Lua的5.3.3编译器。 For example: 例如:
local Types = {
A = 1,
B = 2,
C = 3
};
print(Types.A);
Could this turn into: 这可以变成:
print(1);
or something like (but probably better): 或类似(但可能更好)的东西:
local A = 1;
print(A);
directly? 直?
No, Lua will run the code as is. 不,Lua将按原样运行代码。 That's the cost of compiler's simplicity. 那就是简化编译器的代价。
See the output from luac
: 查看luac
的输出:
main <2.lua:0,0> (8 instructions at 0x235bb10)
0+ params, 3 slots, 1 upvalue, 1 local, 7 constants, 0 functions
1 [1] NEWTABLE 0 0 3
2 [2] SETTABLE 0 -1 -2 ; "A" 1
3 [3] SETTABLE 0 -3 -4 ; "B" 2
4 [4] SETTABLE 0 -5 -6 ; "C" 3
5 [7] GETTABUP 1 0 -7 ; _ENV "print"
6 [7] GETTABLE 2 0 -1 ; "A"
7 [7] CALL 1 2 1
8 [7] RETURN 0 1
You can set Types
as a temporary table to resolve global names: 您可以将“ Types
设置为临时表来解析全局名称:
local Types = {
A = 1,
B = 2,
C = 3
}
do
local print = print
local _ENV = Types
print(A)
end
But do ponder on why you want this. 但是请仔细考虑为什么要这样做。 Note the need for saving print
before setting _ENV
. 注意设置_ENV
之前需要保存print
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.