复制lua函数的开销
我有很多 lua 脚本,其名称与函数名称(用于“入口点”)相同,我想运行它们。但我想让它尽可能快。
经过一番浏览/谷歌搜索/思考后,我有两个解决方案。
1.、我有一个主要的lua_State。我将所有必需的给定和我自己的库/函数“加载”到其中。接下来我 lua_dump() 当前 lua 脚本的 lua_State 函数(使用 chunk 容器的链表),然后我 lua_load() 它到主 lua_State,然后 lua_call() 主 lua_State。使用这个解决方案,我不必加载所有脚本的所有库。所以主要的lua_State是一个“环境”。 :)
2.,我只是为所有 lua_State 加载库。然后 lua_call() 它们。
问题是:第一个逻辑是否正确?如果是,您会使用哪一个?有更好的解决方案吗?
在此先感谢并抱歉我的英语。
(如果第一个确实正确,是否存在一些被忽视的优化可能性?)
I have a lot of lua-script with the same name used for function name (intended for "entry point") and I want to run them. But I want to make it as fast as possible.
After some browsing/googling/thinking I've got two solution.
1., I have a main lua_State. I load the all necessary given and my own libs/functions "into" it. Next I lua_dump() the current lua-script's lua_State's function (with using linked list for chunck container), then I lua_load() it to the main lua_State and then lua_call() the main lua_State. With this solution I don't have to load all the libs for all the scripts. So the main lua_State is an "environment". :)
2., I simply do the libs loading for all lua_State. And then lua_call() them.
The question would be that: Is even the first one logical correct? And if yes, which one would you use? Is there a better solution?
Thanks ahead and sorry for my English.
(And if the first one is really correct, are there some oblivious optimization possibility?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如你所说,我不明白为什么你想要超过 1 个 Lua 状态。如果你只有一个 Lua 状态,那么你需要的所有开销就是加载库(一次),然后从你运行的脚本加载函数(一次,除非你需要从文件中“刷新”它们)。因此只需有 1 个状态,然后
dofile
脚本即可。如果你确实需要多个 lua_States,你只能加载你需要的库,如下所示
在Lua参考手册中,在5.1上面的段落中
还有一个Lua Gems Book 中关于 Lua 代码优化的免费章节。
As you put it, I don't see why you'd want more than 1 Lua state. If you have only one Lua state all overhead you'll have is loading the libs (once) and then loading the functions from the scripts you run (once, unless you need to 'refresh' them from the file). So simply have 1 state, and
dofile
the scripts.If you really need those multiple lua_States, you could only load the libraries you need, as explained
in the Lua Reference Manual, in the paragraph just above 5.1
There's also a nice freely available chapter on optimization of Lua code in Lua Gems Book.
我最近做了类似的事情,并决定使用单个 lua_State 。我通过使用
_ENV
upvalue 将每个脚本文件自定义加载到其自己的环境中(为每个脚本文件生成一个新环境作为全局环境的副本)。这样名称就不会冲突,我相信如果您出于某种原因需要的话,您可以并行运行更多脚本。它适合我的目的,因为我需要基本上随机且随时访问所有已加载脚本中的函数,但如果您需要的只是运行它们一次,那么您只需在同一个 lua_State 中按顺序加载和执行它们即可。
编辑:我注意到我实际上错过了问题的要点。回答:如果需要加载任何标准库,使用单个 lua_State 会更快(开销很明显)。如果您只运行每个脚本一次,则不需要使用
lua_dump/lua_load
,只需执行类似luaL_dofile
的操作,然后在输入函数,然后继续(即加载下一个文件)。I've recently done something similar and decided to use a single
lua_State
. I've custom-loaded every script file into its own environment via the use of the_ENV
upvalue (generating a new environment for each as a copy of the global environment). This way the names won't conflict and I believe you can potentially run more scripts in parallel if you need that for whatever reason.It works for my purposes as I need to access the functions in all the loaded scripts basically at random and at any time, but if all you need is to run them once then you can just load and execute them sequentially in the same lua_State.
Edit: I've noticed I've actually missed the point of the question. To answer: using a single lua_State will be faster if you need to load any standard libraries (the overhead is noticeable). If you are running each script only once you don't need to use
lua_dump/lua_load
, just do something likeluaL_dofile
followed bylua_pcall
on the entry function, then move on (ie load the next file).