克隆Lua状态
各位,有没有办法克隆 Lua 状态?
在我的游戏应用程序中,Lua 虚拟机的初始化过程非常繁重(大约 1 秒,因为许多脚本会同时加载)。我为每个自治代理都有一个单独的 Lua VM,一旦创建了代理,其 Lua 初始化就会非常严重地影响 FPS。
我正在考虑以下模式:如何保持“预分叉”Lua 状态,然后为每个代理简单地克隆?是否可以?
Folks, is there a way to clone a Lua state?
In my game application the initialization procedure of the Lua virtual machine is pretty heavy(about 1 sec, since many scripts are loaded at once). I have a separate Lua VM for each autonomous agent and once the agent is created its Lua initialization affects FPS pretty badly.
I'm thinking about the following schema: what about keeping "preforked" Lua state which is then simply cloned for each agent? Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要考虑为每个自治代理使用 Lua 的协程,而不是完全独立的虚拟机。协程是一种更轻量级的解决方案,但可能适合也可能不适合您的应用程序。
如果你无法改变架构,你可以尝试 LuaJIT。它可能使初始化足够快以满足您的目的。
更多选项:
Rings:“Rings 是一个库,提供它还提供了一种在创建者(主)和创建的(从)状态之间进行通信的简单方法。”
Pluto: "Pluto 是一个允许用户编写的库“Lua 宇宙”的任意大部分写入平面文件,然后将它们读回相同或不同的 Lua 宇宙。”
You want to consider using Lua's coroutines for each autonomous agent, instead of a completely separate VM. Coroutines are a more lightweight solution, but may or may not be suitable for your application.
If you can't change the architecture, you might try LuaJIT. It might make the initialization fast enough for your purposes.
More options:
Rings: "Rings is a library which provides a way to create new Lua states from within Lua. It also offers a simple way to communicate between the creator (master) and the created (slave) states."
Pluto: "Pluto is a library which allows users to write arbitrarily large portions of the "Lua universe" into a flat file, and later read them back into the same or a different Lua universe."
还有 Lanes(下载、docs) 和 与我所知道的所有类似产品的比较。
关于戒指的比较表说:
注意:比较表显示 Lanes 只会编组“非循环表”。它确实执行循环、编组函数、upvalues 等。它在 Lua 状态之间进行复制作为直接复制,不需要对中间的内容进行字符串化。这使得速度很快。
There's also Lanes (download, docs) and within the comparison to all similar products I know.
About Rings the comparison sheet says:
Note: The comparison sheet says Lanes would only marshal 'non-cyclic tables'. It does do cycles, and does marshall functions, upvalues etc. And it does the copies between Lua states as direct copies, not needing to stringify the contents in the middle. This makes it fast.
如果您使用的是 Linux,您可以尝试 lper, Lua 作者之一基于 LPSM 的实验库。
If you're on Linux, you may try lper, LPSM-based experimental library by one of Lua authors.
注意,适用于 Lua 5.2 及更高版本
您只能限制对此虚拟机的访问。创建一个包含所需所有功能的实例,该实例不依赖于 _G(全局 Lua 状态),然后为每个客户端创建单独的表。他们将用作全局命名空间。可以通过 _ENV 将表设置为当前 _G。这是一个很难在一篇文章中解释的话题。简而言之,您为新客户端准备“虚拟”_G,然后只需将 _G 替换为客户端代码即可。 我建议您从这里开始。
这就是重点。
local _ENV = t -- 更改环境。如果没有本地,这将改变整个块的环境
只需删除
local
,您将更改所有进一步代码的_ENV。祝实验顺利!PS不要忘记您可以为_ENV和_G表设置元表并禁止更改该元表。 Lua在这里非常灵活。
Notice, works with Lua 5.2 and above
You can just restrict access to this VM. Create one instance with all functions required, that will not depend on _G (global Lua state) and then create separate table for each client. That they will use as their global namespace. Setting a table as current _G is possible via _ENV. That's quite difficult topic to explain in one post. In short you prepare "virtual" _G for your new client and then just replace _G for the client's code. There is where I advice you to start.
Here's the point.
local _ENV = t -- change the environment. without the local, this would change the environment for the entire chunk
Just remove
local
and you'll change _ENV for all further code. Good luck with experiments!P. S. don't forget that you may set metatable for _ENV and _G tables and forbid changing that metatable. Lua is really flexible here.