为循环执行优化 Lua [英] Optimizing Lua for cyclic execution

查看:26
本文介绍了为循环执行优化 Lua的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每 10 毫秒的程序周期执行一次我的 Lua 脚本.使用相同的 Lua_state(luaL_newstate 在我的应用中调用一次)

I'm exeucting my Lua script once per program cycle of 10 ms. using the same Lua_state (luaL_newstate called once in my app)

调用 luaL_loadbuffer 肯定会非常快地编译脚本,但似乎没有必要每次执行脚本时都这样做,因为脚本没有改变.

Calling luaL_loadbuffer complies the script very fast for sure, still it seems unneccessary to do this every time the script is executed since the script does not change.

尝试使用 lua_dump() 保存二进制文件然后执行它,但 lua_pcall() 由于某种原因不接受二进制文件.

Tried to save binary using lua_dump() and then execute it, but lua_pcall() didn't accept the binary for some reason.

关于如何优化的任何想法?(不幸的是,LuaJIT 不是这里的一个选项)

Any ideas on how to optimize? (LuaJIT is not an unfortenately an option here)

一月

推荐答案

你说得对,如果代码没有变化,就没有理由重新处理代码.也许您可以执行以下操作:

You're correct, if the code is not changing, there is no reason to reprocess the code. Perhaps you could do something like the following:

luaL_loadbuffer(state, buff, len, name); // TODO:  check return value
while (true) {
    // sleep 10ms
    lua_pushvalue(state, -1); // make another reference to the loaded chunk
    lua_call(state, 0, 0);
}

您会注意到我们只是在堆栈顶部复制了函数引用,因为 lua_call 从堆栈中删除了它调用的函数.这样,您就不会丢失对加载块的引用.

You'll note that we simply duplicate the function reference on the top of the stack, since lua_call removes the function that it calls from the stack. This way, you do not lose a reference to the loaded chunk.

这篇关于为循环执行优化 Lua的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆